Brief Introduction to Using Table, Relationship and Scope Objects
The examples you've read about thus far haven't required any particular configuration on your part. All the code you have seen is possible by just connecting to a database with SQL Yoga.
Now you know how to use the basic features of SQL Query and SQL Record objects. Let's look at three more objects that can make you even more productive, but which require a little configuration on the part of the developer. The objects are:
1) Table Objects
2) Relationship Objects
3) Scope Objects
Before we look at examples of how to use these objects we need to discuss where you configure them.
The "tables behavior" Property
To configure a SQL Yoga Database object (a Database object is where all other SQL Yoga objects are stored) you need to create a button and assign it to the table objects behavior property of the object (1). In the sample application that button is named Table Objects Behavior (2). It is an invisible button on the same card as all of the other controls.
The dbobject.createTables Message
There are a number of features that the tables behavior script can add to SQL Yoga. For now we are just going to look at the dbobject.createTables message. dbobject.createTables is where you should create any and all objects that you want to access through the SQL Yoga API. You can assume that no objects exist when this message is called so you should create everything from scratch.
SQL Yoga will call this message in two circumstances:
1) You call the command dbobject_createObject and pass in the table objects behavior button as the 2nd parameter.
2) You call tableobjects_reload which essentially erases existing objects and calls dbobject.createTables.
If you decide you need to add objects to your SQL Yoga database object then simply update the script and call tableobjects_reload.
The first thing you want to do in dbobject.createTables is create Table objects for any tables you will be working with (1). Table objects are necessary in order to create Relationship and Scope objects (2).
Creating Relationship Objects
Relationship objects tell SQL Yoga about how database tables are related to each other. This enables SQL Yoga to transform results it gets from the database into multi-dimensional arrays and link records in two tables together among other things. Creating a relationship is relatively straight forward.
1) Provide a name for the relationship.
2) Define the type of relationship. The type can be one-to-one, one-to-many or many-to-many.
3) Configure the left and right tables, along with the columns in each table that are used to "connect" the tables together. It may be helpful to think think of the left table as the parent and the right table as the child in the relationship.
4) Alternatively specify the table and column that SQL Yoga should use to sort records.
Creating Scope Objects
A Scope object is attached to a Table object. It allows you to define a very narrow search condition that can be used with a SQL Query object. Scopes are useful when you have a number of search conditions that you want to dynamically apply to a search. You can easily add in the scopes you need to the SQL Query object based on user input and SQL Yoga will generate a complete search condition for you based on all added Scopes.
When creating a Scope object you:
1) Specify the table the scope is associated with.
2) Give the scope a unique name.
3) Specify the search conditions. Usually you want to keep the condition very narrow in focus. In this example there is one scope pertaining to the department a town is a part of. Another is just for searching on a town name.