Zend_Db_Table_Definition.xml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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
  18. when describing 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 <methodname>findDependentRowset()</methodname> and
  74. <methodname>findManyToManyRowset()</methodname> 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
  84. . ': '
  85. . $author->first_name
  86. . ' '
  87. . $author->last_name
  88. . PHP_EOL;
  89. $books = $author->findDependentRowset('book');
  90. foreach ($books as $book) {
  91. echo ' Book: ' . $book->title . PHP_EOL;
  92. $genreOutputArray = array();
  93. $genres = $book->findManyToManyRowset('genre', 'book_to_genre');
  94. foreach ($genres as $genreRow) {
  95. $genreOutputArray[] = $genreRow->name;
  96. }
  97. echo ' Genre: ' . implode(', ', $genreOutputArray) . PHP_EOL;
  98. }
  99. }
  100. ]]></programlisting>
  101. </example>
  102. </sect2>
  103. <sect2 id="zend.db.table.definition.advanced-usage">
  104. <title>Advanced Usage</title>
  105. <para>
  106. Sometimes you want to use both paradigms for defining and using the
  107. table gateway: both by extension and concrete instantiation. To do this
  108. simply leave out any table configurations out of the definition. This will
  109. allow <classname>Zend_Db_Table</classname> to look for the actual refered class instead
  110. of the definition key.
  111. </para>
  112. <para>
  113. Building on the example above, we will allow for one of the table configurations
  114. to be a <classname>Zend_Db_Table_Abstract</classname> extended class, while keeping the
  115. rest of the tables as part of the definition. We will also show how one would interact
  116. with this new definition.
  117. </para>
  118. <example id="zend.db.table.definition.example3">
  119. <title>Interacting A Mixed Use Zend_Db_Table Definition</title>
  120. <programlisting language="php"><![CDATA[
  121. class MyBook extends Zend_Db_Table_Abstract
  122. {
  123. protected $_name = 'book';
  124. protected $_referenceMap = array(
  125. 'author' => array(
  126. 'columns' => 'author_id',
  127. 'refTableClass' => 'author',
  128. 'refColumns' => 'id'
  129. )
  130. );
  131. }
  132. $definition = new Zend_Db_Table_Definition(array(
  133. 'author' => array(
  134. 'name' => 'author',
  135. 'dependentTables' => array('MyBook')
  136. ),
  137. 'genre' => null,
  138. 'book_to_genre' => array(
  139. 'referenceMap' => array(
  140. 'book' => array(
  141. 'columns' => 'book_id',
  142. 'refTableClass' => 'MyBook',
  143. 'refColumns' => 'id'
  144. ),
  145. 'genre' => array(
  146. 'columns' => 'genre_id',
  147. 'refTableClass' => 'genre',
  148. 'refColumns' => 'id'
  149. )
  150. )
  151. )
  152. ));
  153. $authorTable = new Zend_Db_Table('author', $definition);
  154. $authors = $authorTable->fetchAll();
  155. foreach ($authors as $author) {
  156. echo $author->id
  157. . ': '
  158. . $author->first_name
  159. . ' '
  160. . $author->last_name
  161. . PHP_EOL;
  162. $books = $author->findDependentRowset(new MyBook());
  163. foreach ($books as $book) {
  164. echo ' Book: ' . $book->title . PHP_EOL;
  165. $genreOutputArray = array();
  166. $genres = $book->findManyToManyRowset('genre', 'book_to_genre');
  167. foreach ($genres as $genreRow) {
  168. $genreOutputArray[] = $genreRow->name;
  169. }
  170. echo ' Genre: ' . implode(', ', $genreOutputArray) . PHP_EOL;
  171. }
  172. }
  173. ]]></programlisting>
  174. </example>
  175. </sect2>
  176. </sect1>