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 Rowset via a Many-to-many Relationship

If you have a Row object as the result of a query on one table in a many-to-many relationship (for purposes of the example, call this the "origin" table), you can fetch corresponding rows in the other table (call this the "destination" table) via an intersection table. Use the method:

<?php
$row
->findManyToManyRowset($table,
                           
$intersectionTable,
                           [
$rule1,
                               [
$rule2,
                                   [
Zend_Db_Table_Select $select]
                               ]
                           ]);

This method returns a Zend_Db_Table_Rowset_Abstract containing rows from the table $table, satisfying the many-to-many relationship. The current Row object $row from the origin table is used to find rows in the intersection table, and that is joined to the destination table.

The first argument $table can be a string that specifies the destination table in the many-to-many relationship by its class name. You can also specify the destination table by using an object of that table class.

The second argument $intersectionTable can be a string that specifies the intersection table between the two tables in the many-to-many relationship by its class name. You can also specify the intersection table by using an object of that table class.

Example 337. Fetching a Rowset with the Many-to-many Method

This example shows getting a Row object from the origin table Bugs, and finding rows from the destination table Products, representing products related to that bug.

<?php
$bugsTable 
= new Bugs();
$bugsRowset $bugsTable->find(1234);
$bug1234 $bugsRowset->current();

$productsRowset $bug1234->findManyToManyRowset('Products',
                                                 
'BugsProducts');

The third and fourth arguments $rule1 and $rule2 are optional. These are strings that name the rule keys in the $_referenceMap array of the intersection table.

The $rule1 key names the rule for the relationship from the intersection table to the origin table. In this example, this is the relationship from BugsProducts to Bugs.

The $rule2 key names the rule for the relationship from the intersection table to the destination table. In this example, this is the relationship from Bugs to Products.

Similarly to the methods for finding parent and dependent rows, if you don't specify a rule, the method uses the first rule in the $_referenceMap array that matches the tables in the relationship. 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 rules used by default are the first ones that match. In this case, $rule1 is 'Reporter' and $rule2 is 'Product'.

Example 338. Fetching a Rowset with the Many-to-many Method By a Specific Rule

This example shows geting a Row object from the origin table Bugs, and finding rows from the destination table Products, representing products related to that bug.

<?php
$bugsTable 
= new Bugs();
$bugsRowset $bugsTable->find(1234);
$bug1234 $bugsRowset->current();

$productsRowset $bug1234->findManyToManyRowset('Products',
                                                 
'BugsProducts',
                                                 
'Bug');

Alternatively, you can query rows from the destination table in a many-to-many relationship using a "magic method." Zend_Db_Table_Row_Abstract invokes the method: findManyToManyRowset('<TableClass>', '<IntersectionTableClass>', '<Rule1>', '<Rule2>') if you invoke a method matching any of the following patterns:

  • $row->find<TableClass>Via<IntersectionTableClass> ([Zend_Db_Table_Select $select])

  • $row->find<TableClass>Via<IntersectionTableClass>By<Rule1> ([Zend_Db_Table_Select $select])

  • $row->find<TableClass>Via<IntersectionTableClass>By<Rule1>And<Rule2> ([Zend_Db_Table_Select $select])

In the patterns above, <TableClass> and <IntersectionTableClass> are strings that correspond to the class names of the destination table and the intersection table, respectively. <Rule1> and <Rule2> are strings that correspond to the rule keys in the intersection table that reference the origin table and the destination table, respectively.

Note

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

Example 339. Fetching Rowsets using the Magic Many-to-many Method

This example shows finding rows in the destination table of a many-to-many relationship representing products related to a given bug.

<?php
$bugsTable 
= new Bugs();
$bugsRowset $bugsTable->find(1234);
$bug1234 $bugsRowset->current();

// Use the default reference rule
$products $bug1234->findProductsViaBugsProducts();

// Specify the reference rule
$products $bug1234->findProductsViaBugsProductsByBug();

Zend Framework