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. span style="color: #ff0000;">'author''name' => 'author',
  2.         'dependentTables''book')
  3.         ),
  4.     'book''name' => 'book',
  5.         'referenceMap''author''columns' => 'author_id',
  6.                 'refTableClass' => 'author',
  7.                 'refColumns' => 'id'
  8.                 )
  9.             )
  10.         ),
  11.     'genre''book_to_genre''referenceMap''book''columns' => 'book_id',
  12.                 'refTableClass' => 'book',
  13.                 'refColumns' => 'id'
  14.                 ),
  15.             'genre''columns' => 'genre_id',
  16.                 'refTableClass' => 'genre',
  17.                 'refColumns' => 'id'
  18.                 )
  19.             )
  20.         )
  21.     ));

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. span style="color: #ff0000;">'author'': '' ''book''    Book: ''genre', 'book_to_genre''        Genre: '', '

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. span style="color: #ff0000;">'book''author''columns' => 'author_id',
  2.             'refTableClass' => 'author',
  3.             'refColumns' => 'id''author''name' => 'author',
  4.         'dependentTables''MyBook')
  5.         ),
  6.     'genre''book_to_genre''referenceMap''book''columns' => 'book_id',
  7.                 'refTableClass' => 'MyBook',
  8.                 'refColumns' => 'id'
  9.                 ),
  10.             'genre''columns' => 'genre_id',
  11.                 'refTableClass' => 'genre',
  12.                 'refColumns' => 'id''author'': '' ''    Book: ''genre', 'book_to_genre''        Genre: '', '

Zend_Db_Table Relationships