Zend_Db_Table_Definition.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 16772 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.db.table.definition">
  5. <title>Zend_Db_Table_Definition</title>
  6. <sect2 id="zend.db.table.definition.introduction">
  7. <title>Introduction</title>
  8. <para>
  9. <classname>Zend_Db_Table_Definition</classname> is a class that can be used to
  10. describe the relationships and configuration options that should be used when
  11. <classname>Zend_Db_Table</classname> is used via concrete instantiation.
  12. </para>
  13. </sect2>
  14. <sect2 id="zend.db.table.definition.usage">
  15. <title>Basic Usage</title>
  16. <para>
  17. For all of the same options that are available when configuring an extended
  18. Zend_Db_Table_Abstract class, those options are also available when describing
  19. a definition file. This definition file should be passed to the class at
  20. instantiation time so that it can know the full definition of all tables
  21. in said definition.
  22. </para>
  23. <para>
  24. Below is a definition that will describe the table names and relationships
  25. between table objects. Note: if 'name' is left out of the definition, it
  26. will be take as the key of the defined table (an example of this is in the
  27. 'genre' section in the example below.)
  28. </para>
  29. <example id="zend.db.table.definition.example1">
  30. <title>Describing the Definition of a Database Data Model</title>
  31. <programlisting language="php"><![CDATA[
  32. $definition = new Zend_Db_Table_Definition(array(
  33. 'author' => array(
  34. 'name' => 'author',
  35. 'dependentTables' => array('book')
  36. ),
  37. 'book' => array(
  38. 'name' => 'book',
  39. 'referenceMap' => array(
  40. 'author' => array(
  41. 'columns' => 'author_id',
  42. 'refTableClass' => 'author',
  43. 'refColumns' => 'id'
  44. )
  45. )
  46. ),
  47. 'genre' => null,
  48. 'book_to_genre' => array(
  49. 'referenceMap' => array(
  50. 'book' => array(
  51. 'columns' => 'book_id',
  52. 'refTableClass' => 'book',
  53. 'refColumns' => 'id'
  54. ),
  55. 'genre' => array(
  56. 'columns' => 'genre_id',
  57. 'refTableClass' => 'genre',
  58. 'refColumns' => 'id'
  59. )
  60. )
  61. )
  62. ));
  63. ]]></programlisting>
  64. </example>
  65. <para>
  66. As you can see, the same options you'd generally see inside of an
  67. extended Zend_Db_Table_Abstract class are documented in this
  68. array as well. When passed into Zend_Db_Table constructor, this
  69. definition is <emphasis>persisted</emphasis> to any tables it will need
  70. to create in order to return the proper rows.
  71. </para>
  72. <para>
  73. Below is an example of the primary table instantiation as well as
  74. the findDependentRowset() and findManyToManyRowset() calls that will
  75. correspond to the data model described above:
  76. </para>
  77. <example id="zend.db.table.definition.example2">
  78. <title>Interacting with the described definition</title>
  79. <programlisting language="php"><![CDATA[
  80. $authorTable = new Zend_Db_Table('author', $definition);
  81. $authors = $authorTable->fetchAll();
  82. foreach ($authors as $author) {
  83. echo $author->id . ': ' . $author->first_name . ' ' . $author->last_name . PHP_EOL;
  84. $books = $author->findDependentRowset('book');
  85. foreach ($books as $book) {
  86. echo ' Book: ' . $book->title . PHP_EOL;
  87. $genreOutputArray = array();
  88. foreach ($book->findManyToManyRowset('genre', 'book_to_genre') as $genreRow) {
  89. $genreOutputArray[] = $genreRow->name;
  90. }
  91. echo ' Genre: ' . implode(', ', $genreOutputArray) . PHP_EOL;
  92. }
  93. }
  94. ]]></programlisting>
  95. </example>
  96. </sect2>
  97. <sect2 id="zend.db.table.definition.advanced-usage">
  98. <title>Advanced Usage</title>
  99. <para>
  100. Sometimes you want to use both paradigms for defining and using the
  101. table gateway: both by extension and concrete instantiation. To do this
  102. simply leave out any table configurations out of the definition. This will
  103. allow Zend_Db_Table to look for the actual refered class instead of the
  104. definition key.
  105. </para>
  106. <para>
  107. Building on the example above, we will allow for one of the table configurations
  108. to be a Zend_Db_Table_Abstract extended class, while keeping the rest of the tables
  109. as part of the definition. We will also show how one would interact with this
  110. new definition.
  111. </para>
  112. <example id="zend.db.table.definition.example3">
  113. <title>Interacting A Mixed Use Zend_Db_Table Definition</title>
  114. <programlisting language="php"><![CDATA[
  115. class MyBook extends Zend_Db_Table_Abstract
  116. {
  117. protected $_name = 'book';
  118. protected $_referenceMap = array(
  119. 'author' => array(
  120. 'columns' => 'author_id',
  121. 'refTableClass' => 'author',
  122. 'refColumns' => 'id'
  123. )
  124. );
  125. }
  126. $definition = new Zend_Db_Table_Definition(array(
  127. 'author' => array(
  128. 'name' => 'author',
  129. 'dependentTables' => array('MyBook')
  130. ),
  131. 'genre' => null,
  132. 'book_to_genre' => array(
  133. 'referenceMap' => array(
  134. 'book' => array(
  135. 'columns' => 'book_id',
  136. 'refTableClass' => 'MyBook',
  137. 'refColumns' => 'id'
  138. ),
  139. 'genre' => array(
  140. 'columns' => 'genre_id',
  141. 'refTableClass' => 'genre',
  142. 'refColumns' => 'id'
  143. )
  144. )
  145. )
  146. ));
  147. $authorTable = new Zend_Db_Table('author', $definition);
  148. $authors = $authorTable->fetchAll();
  149. foreach ($authors as $author) {
  150. echo $author->id . ': ' . $author->first_name . ' ' . $author->last_name . PHP_EOL;
  151. $books = $author->findDependentRowset(new MyBook());
  152. foreach ($books as $book) {
  153. echo ' Book: ' . $book->title . PHP_EOL;
  154. $genreOutputArray = array();
  155. foreach ($book->findManyToManyRowset('genre', 'book_to_genre') as $genreRow) {
  156. $genreOutputArray[] = $genreRow->name;
  157. }
  158. echo ' Genre: ' . implode(', ', $genreOutputArray) . PHP_EOL;
  159. }
  160. }
  161. ]]></programlisting>
  162. </example>
  163. </sect2>
  164. </sect1>