程序员人生 网站导航

Zend Framework教程-Zend_Db-数据库操作6-Zend_Db_Table_Definition翻译

栏目:ZendFramework时间:2013-11-14 02:02:44

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.

Zend_Db_Table_Definition  用于定义关联关系和对实例化Zend_Db_Table的相关选项配置的类。

Basic Usage

For all of the same options that are available when configuring an extendedZend_Db_Table_Abstract class, those options are also available when describing a definition file. 

 所有继承实现Zend_Db_Table_Abstract 类的配置选项,都是可以使用定义描述文件的。

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.)

注:如果“name”的定义被省略了,将采用表定义的key值(例如下面的“genre”部分)。

Example #1 Describing the Definition of a Database Data Model  数据库的数据模型定义描述


$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. 


正如你看到的,继承实现Zend_Db_Table_Abstract类时相同的选项,也要在这个数组中定义。


When passed into  Zend_Db_Table constructor, this definition ispersisted to any tables it will need to create in order to return the proper rows.

当传入  Zend_Db_Table 构造函数时, 这个定义会被持久化到所有表中。它必须被创建目的是为了返回对应的行 。

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

下面是通过上面定义的主表实例化的例子,然后调用 findDependentRowset()和 findManyToManyRowset()方法:


Example #2 Interacting with the described definition 描述定义使用

   

$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;    }}


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. 

有时可能会同时使用Zend_Db_Table和 Zend_Db_Table_Definition。要做到这一点,只需实现所有表的定义,即可。

This will allow Zend_Db_Table to look for the actual refered class instead of the definition key.

允许采用Zend_Db_Table的具体类代替定义的KEY

Building on the example above, we will allow for one of the table configurations to be aZend_Db_Table_Abstract extended class, while keeping the rest of the tables as part of the definition. 

基于上面的例子, 我们可以配置其中一个 实例化Zend_Db_Table_Abstract 的类,   而其它的表保持原有定义。

We will also show how one would interact with this new definition.

以下是具体的定义。


Example #3 Interacting A Mixed Use Zend_Db_Table Definition 混合使用Zend_Db_Table定义


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;    }}





-----------

对译文与原文在含义上的差异而造成的误解不承担任何责任。仅供参考。 




------分隔线----------------------------
------分隔线----------------------------

最新技术推荐