Zend_Db_Table Relationships

Zend_Db_Table_Definition

Introduction

Zend_Db_Table_Definition is a class that can be used to describe the relationships and configuration options that should be used when Zend_Db_Table is used via concrete instantiation.

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 #1 Describing the Definition of a Database Data Model

  1. $definition = new Zend_Db_Table_Definition(array(
  2.     'author' => array(
  3.         'name' => 'author',
  4.         'dependentTables' => array('book')
  5.         ),
  6.     'book' => array(
  7.         'name' => 'book',
  8.         'referenceMap' => array(
  9.             'author' => array(
  10.                 'columns' => 'author_id',
  11.                 'refTableClass' => 'author',
  12.                 'refColumns' => 'id'
  13.                 )
  14.             )
  15.         ),
  16.     'genre' => null,
  17.     'book_to_genre' => array(
  18.         'referenceMap' => array(
  19.             'book' => array(
  20.                 'columns' => 'book_id',
  21.                 'refTableClass' => 'book',
  22.                 'refColumns' => 'id'
  23.                 ),
  24.             'genre' => array(
  25.                 'columns' => 'genre_id',
  26.                 'refTableClass' => 'genre',
  27.                 'refColumns' => 'id'
  28.                 )
  29.             )
  30.         )
  31.     ));

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 #2 Interacting with the described definition

  1. $authorTable = new Zend_Db_Table('author', $definition);
  2. $authors = $authorTable->fetchAll();
  3.  
  4. foreach ($authors as $author) {
  5.     echo $author->id
  6.        . ': '
  7.        . $author->first_name
  8.        . ' '
  9.        . $author->last_name
  10.        . PHP_EOL;
  11.     $books = $author->findDependentRowset('book');
  12.     foreach ($books as $book) {
  13.         echo '    Book: ' . $book->title . PHP_EOL;
  14.         $genreOutputArray = array();
  15.         $genres = $book->findManyToManyRowset('genre', 'book_to_genre');
  16.         foreach ($genres as $genreRow) {
  17.             $genreOutputArray[] = $genreRow->name;
  18.         }
  19.         echo '        Genre: ' . implode(', ', $genreOutputArray) . PHP_EOL;
  20.     }
  21. }

Advanced Usage

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 #3 Interacting A Mixed Use Zend_Db_Table Definition

  1. class MyBook extends Zend_Db_Table_Abstract
  2. {
  3.     protected $_name = 'book';
  4.     protected $_referenceMap = array(
  5.         'author' => array(
  6.             'columns' => 'author_id',
  7.             'refTableClass' => 'author',
  8.             'refColumns' => 'id'
  9.             )
  10.         );
  11. }
  12.  
  13. $definition = new Zend_Db_Table_Definition(array(
  14.     'author' => array(
  15.         'name' => 'author',
  16.         'dependentTables' => array('MyBook')
  17.         ),
  18.     'genre' => null,
  19.     'book_to_genre' => array(
  20.         'referenceMap' => array(
  21.             'book' => array(
  22.                 'columns' => 'book_id',
  23.                 'refTableClass' => 'MyBook',
  24.                 'refColumns' => 'id'
  25.                 ),
  26.             'genre' => array(
  27.                 'columns' => 'genre_id',
  28.                 'refTableClass' => 'genre',
  29.                 'refColumns' => 'id'
  30.                 )
  31.             )
  32.         )
  33.     ));
  34.  
  35. $authorTable = new Zend_Db_Table('author', $definition);
  36. $authors = $authorTable->fetchAll();
  37.  
  38. foreach ($authors as $author) {
  39.     echo $author->id
  40.        . ': '
  41.        . $author->first_name
  42.        . ' '
  43.        . $author->last_name
  44.        . PHP_EOL;
  45.     $books = $author->findDependentRowset(new MyBook());
  46.     foreach ($books as $book) {
  47.         echo '    Book: ' . $book->title . PHP_EOL;
  48.         $genreOutputArray = array();
  49.         $genres = $book->findManyToManyRowset('genre', 'book_to_genre');
  50.         foreach ($genres as $genreRow) {
  51.             $genreOutputArray[] = $genreRow->name;
  52.         }
  53.         echo '        Genre: ' . implode(', ', $genreOutputArray) . PHP_EOL;
  54.     }
  55. }

Zend_Db_Table Relationships