Zend_Db_Table_Definition.xml 6.7 KB

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