PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Fetching a Dependent Rowset

If you have a Row object as the result of a query on a parent table, you can fetch rows from dependent tables that reference the current row. Use the method:

<?php
$row
->findDependentRowset($table, [$rule]);

This method returns a Zend_Db_Table_Rowset_Abstract object, containing a set of rows from the dependent table $table that refer to the row identified by the $row object.

The first argument $table can be a string that specifies the dependent table by its class name. You can also specify the dependent table by using an object of that table class.

Example 330. Fetching a Dependent Rowset

This example shows getting a Row object from the table Accounts, and finding the Bugs reported by that account.

<?php
$accountsTable 
= new Accounts();
$accountsRowset $accountsTable->find(1234);
$user1234 $accountsRowset->current();

$bugsReportedByUser $user1234->findDependentRowset('Bugs');

The second argument $rule is optional. It is a string that names the rule key in the $_referenceMap array of the dependent table class. If you don't specify a rule, the first rule in the array that references the parent table is used. If you need to use a rule other than the first, you need to specify the key.

In the example code above, the rule key is not specified, so the rule used by default is the first one that matches the parent table. This is the rule 'Reporter'.

Example 331. Fetching a Dependent Rowset By a Specific Rule

This example shows getting a Row object from the table Accounts, and finding the Bugs assigned to be fixed by the user of that account. The rule key string that corresponds to this reference relationship in this example is 'Engineer'.

<?php
$accountsTable 
= new Accounts();
$accountsRowset $accountsTable->find(1234);
$user1234 $accountsRowset->current();

$bugsAssignedToUser $user1234->findDependentRowset('Bugs''Engineer');

You can also add criteria, ordering and limits to your relationships using the parent row's select object.

Example 332. Fetching a Dependent Rowset using a Zend_Db_Table_Select

This example shows getting a Row object from the table Accounts, and finding the Bugs assigned to be fixed by the user of that account, limited only to 3 rows and ordered by name.

<?php
$accountsTable 
= new Accounts();
$accountsRowset $accountsTable->find(1234);
$user1234 $accountsRowset->current();
$select $accountsTable->select()->order('name ASC')
                                  ->
limit(3);

$bugsAssignedToUser $user1234->findDependentRowset('Bugs',
                                                     
'Engineer',
                                                     
$select);

Alternatively, you can query rows from a dependent table using a special mechanism called a "magic method". Zend_Db_Table_Row_Abstract invokes the method: findDependentRowset('<TableClass>', '<Rule>') if you invoke a method on the Row object matching either of the following patterns:

  • $row->find<TableClass>()

  • $row->find<TableClass>By<Rule>()

In the patterns above, <TableClass> and <Rule> are strings that correspond to the class name of the dependent table, and the dependent table's rule key that references the parent table.

Note

Some application frameworks, such as Ruby on Rails, use a mechanism called "inflection" to allow the spelling of identifiers to change depending on usage. For simplicity, Zend_Db_Table_Row does not provide any inflection mechanism. The table identity and the rule key named in the method call must match the spelling of the class and rule key exactly.

Example 333. Fetching Dependent Rowsets using the Magic Method

This example shows finding dependent Rowsets equivalent to those in the previous examples. In this case, the application uses the magic method invocation instead of specifying the table and rule as strings.

<?php
$accountsTable 
= new Accounts();
$accountsRowset $accountsTable->find(1234);
$user1234 $accountsRowset->current();

// Use the default reference rule
$bugsReportedBy $user1234->findBugs();

// Specify the reference rule
$bugsAssignedTo $user1234->findBugsByEngineer();

Zend Framework