Expressing One-to-Many and Many-to-Many Relationships
ActiveTable will allow you to express a one-to-many relationship (and, through that, a many-to-many relationship) using the $RELATED array. Related recordsets are one or more rows that correspond to a row in another table. That other table must have an existing ActiveTable-enabled class defined.
This tutorial will expand on the first tutorial and build ActiveTable classes for more Kitto tables.
One-to-many Relationships
Loading the many from the one is easily achieved - one simply defines something similar to a join, creates an ActiveTable-enabled class to represent the other table, and #grabs() the record set. The example below speaks for itself.
Be aware that defining a list of related recordsets does not have a noticeable impact on performance. No queries are run until the grab is performed. The resulting recordset is cached in the object, so any subsequent calls to re-grab a recordset likewise result in no queries being executed.
If you set up objects with reciprocating relations, also be advised that you must specify all of the attributes for a related recordset in at least one class. The way that related recordsets determine the table names and primary key for the opposite class is by creating an instance of the class - which, in turn, checks all of its recordsets and will instantiates the original object to get its table and key. That behavior will go into an infinite loop and break your application, and it can be avoided by specifying all of the parameters in the array.
Many-to-many Relationships
Many-to-many relationships can be represented with a class to represent the table that the other two are joined together through. This example illustrates the RecipeMateiral class that has the capacity to #grab() the original recipe item and the material in question.
grab() and COUNT(*)/Slices
The #grab() method supports counting and retrieving record set slices, just as #findBy() does.