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 Parent Row

If you have a Row object as the result of a query on a dependent table, you can fetch the row in the parent to which the dependent row refers. Use the method:

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

There always should be exactly one row in the parent table referenced by a dependent row, therefore this method returns a Row object, not a Rowset object.

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

Example 334. Fetching the Parent Row

This example shows getting a Row object from the table Bugs (for example one of those bugs with status 'NEW'), and finding the row in the Accounts table for the user who reported the bug.

<?php
$bugsTable 
= new Bugs();
$bugsRowset $bugsTable->fetchAll(array('bug_status = ?' => 'NEW'));
$bug1 $bugsRowset->current();

$reporter $bug1->findParentRow('Accounts');

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 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 335. Fetching a Parent Row By a Specific Rule

This example shows getting a Row object from the table Bugs, and finding the account for the engineer assigned to fix that bug. The rule key string that corresponds to this reference relationship in this example is 'Engineer'.

<?php
$bugsTable 
= new Bugs();
$bugsRowset $bugsTable->fetchAll(array('bug_status = ?''NEW'));
$bug1 $bugsRowset->current();

$engineer $bug1->findParentRow('Accounts''Engineer');

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

  • $row->findParent<TableClass>([Zend_Db_Table_Select $select])

  • $row->findParent<TableClass>By<Rule>([Zend_Db_Table_Select $select])

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

Note

The table identity and the rule key named in the method call must match the spelling of the class and rule key exactly.

Example 336. Fetching the Parent Row using the Magic Method

This example shows finding parent Rows 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
$bugsTable 
= new Bugs();
$bugsRowset $bugsTable->fetchAll(array('bug_status = ?''NEW'));
$bug1 $bugsRowset->current();

// Use the default reference rule
$reporter $bug1->findParentAccounts();

// Specify the reference rule
$engineer $bug1->findParentAccountsByEngineer();

Zend Framework