For each table in your database that you want to access, define a class that extends
Zend_Db_Table_Abstract.
Declare the database table for which this class is defined, using the protected
variable $_name. This is a string, and must contain the name of
the table spelled as it appears in the database.
Example 260. Declaring a table class with explicit table name
<?php
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
}
If you don't specify the table name, it defaults to the name of the class. If you rely on this default, the class name must match the spelling of the table name as it appears in the database.
Example 261. Declaring a table class with implicit table name
<?php
class bugs extends Zend_Db_Table_Abstract
{
// table name matches class name
}
You can also declare the schema for the table, either with the protected variable
$_schema, or with the schema prepended to the table name in the
$_name property. Any schema specified with the
$_name property takes precedence over a schema specified with the
$_schema property. In some RDBMS brands, the
term for schema is "database" or "tablespace," but it is used similarly.
Example 262. Declaring a table class with schema
<?php
// First alternative:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_schema = 'bug_db';
protected $_name = 'bugs';
}
// Second alternative:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bug_db.bugs';
}
// If schemas are specified in both $_name and $_schema, the one
// specified in $_name takes precedence:
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bug_db.bugs';
protected $_schema = 'ignored';
}
The schema and table names may also be specified via constructor configuration
directives, which override any default values specified with the
$_name and $_schema properties. A schema
specification given with the name directive overrides any value
provided with the schema option.
Example 263. Declaring table and schema names upon instantiation
<?php
class Bugs extends Zend_Db_Table_Abstract
{
}
// First alternative:
$tableBugs = new Bugs(array('name' => 'bugs', 'schema' => 'bug_db'));
// Second alternative:
$tableBugs = new Bugs(array('name' => 'bug_db.bugs'));
// If schemas are specified in both 'name' and 'schema', the one
// specified in 'name' takes precedence:
$tableBugs = new Bugs(array('name' => 'bug_db.bugs',
'schema' => 'ignored'));
If you don't specify the schema name, it defaults to the schema to which your database adapter instance is connected.
Every table must have a primary key. You can declare the column for the primary key
using the protected variable $_primary. This is either a string
that names the single column for the primary key, or else it is an array of column
names if your primary key is a compound key.
Example 264. Example of specifying the primary key
<?php
class Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
protected $_primary = 'bug_id';
}
If you don't specify the primary key, Zend_Db_Table_Abstract
tries to discover the primary key based on the information provided by the
describeTable()´ method.
Note
Every table class must know which columns can be used to address rows
uniquely. If no primary key columns are specified in the table class
definition or the table constructor arguments, or discovered in the table
metadata provided by describeTable(), then the table
cannot be used with Zend_Db_Table.
When you create an instance of a Table class, the constructor calls a set of protected methods that initialize metadata for the table. You can extend any of these methods to define metadata explicitly. Remember to call the method of the same name in the parent class at the end of your method.
Example 265. Example of overriding the _setupTableName() method
<?php
class Bugs extends Zend_Db_Table_Abstract
{
protected function _setupTableName()
{
$this->_name = 'bugs';
parent::_setupTableName();
}
}
The setup methods you can override are the following:
_setupDatabaseAdapter()checks that an adapter has been provided; gets a default adapter from the registry if needed. By overriding this method, you can set a database adapter from some other source._setupTableName()defaults the table name to the name of the class. By overriding this method, you can set the table name before this default behavior runs._setupMetadata()sets the schema if the table name contains the pattern "schema.table"; callsdescribeTable()to get metadata information; defaults the$_colsarray to the columns reported bydescribeTable(). By overriding this method, you can specify the columns._setupPrimaryKey()defaults the primary key columns to those reported bydescribeTable(); checks that the primary key columns are included in the$_colsarray. By overriding this method, you can specify the primary key columns.
If application-specific logic needs to be initialized when a Table class is
constructed, you can select to move your tasks to the
init() method, which is called after all Table metadata has
been processed. This is recommended over the __construct()
method if you do not need to alter the metadata in any programmatic way.
Example 266. Example usage of init() method
<?php
class Bugs extends Zend_Db_Table_Abstract
{
protected $_observer;
public function init()
{
$this->_observer = new MyObserverClass();
}
}




