Creating a Relationship Object

Relationship Objects are created in the dbobject.createTables handler of the button object script that is assigned to the table objects behavior property of the Database object. If you are not familiar with the table objects behavior property please see this lesson.

Create A Table Object

Here is an example how to create a Relationship objects between three tables: "projects", "todo_items" and "people".

"projects" has a one-to-many relationship with "todo_items". "todo_items" has a many-to-many relationship with "people".

on dbobject.createTables

## Create Table Objects

tableobj_createObject "projects"

tableobj_createObject "todo_items"

tableobj_createObject "people"

## Now that table objects exist create relationships

_CreateRelationships

end dbobject.createTables

private command _CreateRelationships

----------

## Define one-to-many relationship between projects and to-do items

tblrelation_createObject "projects to todo items"

tblrelation_set "projects to todo items", "type", "one-to-many"

## Tell SQL Yoga which fields link the tables together

tblrelation_set "projects to todo items", "left table", "projects"

tblrelation_set "projects to todo items", "left table key", "id"

tblrelation_set "projects to todo items", "right table", "todo_items"

tblrelation_set "projects to todo items", "right table key", "project_id"

## Tell SQL Yoga how to order the results when retrieving todo items

## that are linked to projects.

tblrelation_set "projects to todo items", "order by", "todo_items.sequence"

----------

----------

## Define relationship between people and to-do items

tblrelation_createObject "people to todo items"

tblrelation_set "people to todo items", "type", "many-to-many"

## Tell SQL Yoga which fields link the tables together

tblrelation_set "people to todo items", "left table", "people"

tblrelation_set "people to todo items", "left table key", "id"

## many-to-many relationships need to go through a 3rd table. Tell SQL Yoga about it.

tblrelation_set "people to todo items", "cross-reference table", "people_todo"

tblrelation_set "people to todo items", "cross-reference table key for left table", "people_id"

tblrelation_set "people to todo items", "cross-reference table key for right table", "todo_id"

tblrelation_set "people to todo items", "right table", "todo_items"

tblrelation_set "people to todo items", "right table key", "id"

## Tell SQL Yoga how to order the results when retrieving people

## that are linked to todo items.

tblrelation_set "people to todo items", "order by", "todo_items.name"

----------

end _CreateRelationships

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.