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

Basic Usage

For all of the same options that are available when configuring an extended Zend_Db_Table_Abstract class, those options are also available when describing a definition file. This definition file should be passed to the class at instantiation time so that it can know the full definition of all tables in said definition.

Below is a definition that will describe the table names and relationships between table objects. Note: if 'name' is left out of the definition, it will be taken as the key of the defined table (an example of this is in the 'genre' section in the example below.)

Example 342. Describing the Definition of a Database Data Model

<?php
$definition 
= new Zend_Db_Table_Definition(array(
    
'author' => array(
        
'name' => 'author',
        
'dependentTables' => array('book')
        ),
    
'book' => array(
        
'name' => 'book',
        
'referenceMap' => array(
            
'author' => array(
                
'columns' => 'author_id',
                
'refTableClass' => 'author',
                
'refColumns' => 'id'
                
)
            )
        ),
    
'genre' => null,
    
'book_to_genre' => array(
        
'referenceMap' => array(
            
'book' => array(
                
'columns' => 'book_id',
                
'refTableClass' => 'book',
                
'refColumns' => 'id'
                
),
            
'genre' => array(
                
'columns' => 'genre_id',
                
'refTableClass' => 'genre',
                
'refColumns' => 'id'
                
)
            )
        )
    ));

As you can see, the same options you'd generally see inside of an extended Zend_Db_Table_Abstract class are documented in this array as well. When passed into Zend_Db_Table constructor, this definition is persisted to any tables it will need to create in order to return the proper rows.

Below is an example of the primary table instantiation as well as the findDependentRowset() and findManyToManyRowset() calls that will correspond to the data model described above:

Example 343. Interacting with the described definition

<?php
$authorTable 
= new Zend_Db_Table('author'$definition);
$authors $authorTable->fetchAll();

foreach (
$authors as $author) {
    echo 
$author->id
       
': '
       
$author->first_name
       
' '
       
$author->last_name
       
PHP_EOL;
    
$books $author->findDependentRowset('book');
    foreach (
$books as $book) {
        echo 
'    Book: ' $book->title PHP_EOL;
        
$genreOutputArray = array();
        
$genres $book->findManyToManyRowset('genre''book_to_genre');
        foreach (
$genres as $genreRow) {
            
$genreOutputArray[] = $genreRow->name;
        }
        echo 
'        Genre: ' implode(', '$genreOutputArray) . PHP_EOL;
    }
}

Zend Framework