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

Defining Relationships

Define classes for each of your tables, extending the abstract class Zend_Db_Table_Abstract, as described in this chapter. Also see this chapter for a description of the example database for which the following example code is designed.

Below are the PHP class definitions for these tables:

<?php
class Accounts extends Zend_Db_Table_Abstract
{
    protected 
$_name            'accounts';
    protected 
$_dependentTables = array('Bugs');
}

class 
Products extends Zend_Db_Table_Abstract
{
    protected 
$_name            'products';
    protected 
$_dependentTables = array('BugsProducts');
}

class 
Bugs extends Zend_Db_Table_Abstract
{
    protected 
$_name            'bugs';

    protected 
$_dependentTables = array('BugsProducts');

    protected 
$_referenceMap    = array(
        
'Reporter' => array(
            
'columns'           => 'reported_by',
            
'refTableClass'     => 'Accounts',
            
'refColumns'        => 'account_name'
        
),
        
'Engineer' => array(
            
'columns'           => 'assigned_to',
            
'refTableClass'     => 'Accounts',
            
'refColumns'        => 'account_name'
        
),
        
'Verifier' => array(
            
'columns'           => array('verified_by'),
            
'refTableClass'     => 'Accounts',
            
'refColumns'        => array('account_name')
        )
    );
}

class 
BugsProducts extends Zend_Db_Table_Abstract
{
    protected 
$_name 'bugs_products';

    protected 
$_referenceMap    = array(
        
'Bug' => array(
            
'columns'           => array('bug_id'),
            
'refTableClass'     => 'Bugs',
            
'refColumns'        => array('bug_id')
        ),
        
'Product' => array(
            
'columns'           => array('product_id'),
            
'refTableClass'     => 'Products',
            
'refColumns'        => array('product_id')
        )
    );

}

If you use Zend_Db_Table to emulate cascading UPDATE and DELETE operations, declare the $_dependentTables array in the class for the parent table. List the class name for each dependent table. Use the class name, not the physical name of the SQL table.

Note

Skip declaration of $_dependentTables if you use referential integrity constraints in the RDBMS server to implement cascading operations. See this chapter for more information.

Declare the $_referenceMap array in the class for each dependent table. This is an associative array of reference "rules". A reference rule identifies which table is the parent table in the relationship, and also lists which columns in the dependent table reference which columns in the parent table.

The rule key is a string used as an index to the $_referenceMap array. This rule key is used to identify each reference relationship. Choose a descriptive name for this rule key. It's best to use a string that can be part of a PHP method name, as you will see later.

In the example PHP code above, the rule keys in the Bugs table class are: 'Reporter', 'Engineer', 'Verifier', and 'Product'.

The value of each rule entry in the $_referenceMap array is also an associative array. The elements of this rule entry are described below:

  • columns => A string or an array of strings naming the foreign key column names in the dependent table.

    It's common for this to be a single column, but some tables have multi-column keys.

  • refTableClass => The class name of the parent table. Use the class name, not the physical name of the SQL table.

    It's common for a dependent table to have only one reference to its parent table, but some tables have multiple references to the same parent table. In the example database, there is one reference from the bugs table to the products table, but three references from the bugs table to the accounts table. Put each reference in a separate entry in the $_referenceMap array.

  • refColumns => A string or an array of strings naming the primary key column names in the parent table.

    It's common for this to be a single column, but some tables have multi-column keys. If the reference uses a multi-column key, the order of columns in the 'columns' entry must match the order of columns in the 'refColumns' entry.

    Note

    It is recommended that the refColumns element is always declared as cascading operations will not work unless you do so.

  • onDelete => The rule for an action to execute if a row is deleted in the parent table. See this chapter for more information.

  • onUpdate => The rule for an action to execute if values in primary key columns are updated in the parent table. See this chapter for more information.

Zend Framework