Sometimes you want to use both paradigms for defining and using the
table gateway: both by extension and concrete instantiation. To do this
simply leave out any table configurations out of the definition. This will
allow Zend_Db_Table to look for the actual refered class instead
of the definition key.
Building on the example above, we will allow for one of the table configurations
to be a Zend_Db_Table_Abstract extended class, while keeping the
rest of the tables as part of the definition. We will also show how one would interact
with this new definition.
Example 338. Interacting A Mixed Use Zend_Db_Table Definition
<?php
class MyBook extends Zend_Db_Table_Abstract
{
protected $_name = 'book';
protected $_referenceMap = array(
'author' => array(
'columns' => 'author_id',
'refTableClass' => 'author',
'refColumns' => 'id'
)
);
}
$definition = new Zend_Db_Table_Definition(array(
'author' => array(
'name' => 'author',
'dependentTables' => array('MyBook')
),
'genre' => null,
'book_to_genre' => array(
'referenceMap' => array(
'book' => array(
'columns' => 'book_id',
'refTableClass' => 'MyBook',
'refColumns' => 'id'
),
'genre' => array(
'columns' => 'genre_id',
'refTableClass' => 'genre',
'refColumns' => 'id'
)
)
)
));
$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(new MyBook());
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;
}
}




