Zend_Db_Table-Relationships.xml 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <sect1 id="zend.db.table.relationships">
  2. <title>Zend_Db_Table Relationships</title>
  3. <sect2 id="zend.db.table.relationships.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. Tables have relationships to each other in a relational database. An entity in one
  7. table can be linked to one or more entities in another table by using referential
  8. integrity constraints defined in the database schema.
  9. </para>
  10. <para>
  11. The Zend_Db_Table_Row class has methods for querying related rows in other tables.
  12. </para>
  13. </sect2>
  14. <sect2 id="zend.db.table.relationships.defining">
  15. <title>Defining Relationships</title>
  16. <para>
  17. Define classes for each of your tables, extending the abstract class
  18. Zend_Db_Table_Abstract, as described in <xref linkend="zend.db.table.defining" />. Also
  19. see <xref linkend="zend.db.adapter.example-database" /> for a description of the
  20. example database for which the following example code is designed.
  21. </para>
  22. <para>
  23. Below are the PHP class definitions for these tables:
  24. </para>
  25. <programlisting role="php"><![CDATA[
  26. class Accounts extends Zend_Db_Table_Abstract
  27. {
  28. protected $_name = 'accounts';
  29. protected $_dependentTables = array('Bugs');
  30. }
  31. class Products extends Zend_Db_Table_Abstract
  32. {
  33. protected $_name = 'products';
  34. protected $_dependentTables = array('BugsProducts');
  35. }
  36. class Bugs extends Zend_Db_Table_Abstract
  37. {
  38. protected $_name = 'bugs';
  39. protected $_dependentTables = array('BugsProducts');
  40. protected $_referenceMap = array(
  41. 'Reporter' => array(
  42. 'columns' => 'reported_by',
  43. 'refTableClass' => 'Accounts',
  44. 'refColumns' => 'account_name'
  45. ),
  46. 'Engineer' => array(
  47. 'columns' => 'assigned_to',
  48. 'refTableClass' => 'Accounts',
  49. 'refColumns' => 'account_name'
  50. ),
  51. 'Verifier' => array(
  52. 'columns' => array('verified_by'),
  53. 'refTableClass' => 'Accounts',
  54. 'refColumns' => array('account_name')
  55. )
  56. );
  57. }
  58. class BugsProducts extends Zend_Db_Table_Abstract
  59. {
  60. protected $_name = 'bugs_products';
  61. protected $_referenceMap = array(
  62. 'Bug' => array(
  63. 'columns' => array('bug_id'),
  64. 'refTableClass' => 'Bugs',
  65. 'refColumns' => array('bug_id')
  66. ),
  67. 'Product' => array(
  68. 'columns' => array('product_id'),
  69. 'refTableClass' => 'Products',
  70. 'refColumns' => array('product_id')
  71. )
  72. );
  73. }
  74. ]]>
  75. </programlisting>
  76. <para>
  77. If you use Zend_Db_Table to emulate cascading UPDATE and DELETE operations, declare the
  78. <code>$_dependentTables</code> array in the class for the parent table. List the class
  79. name for each dependent table. Use the class name, not the physical name of the SQL
  80. table.
  81. </para>
  82. <note>
  83. <para>
  84. Skip declaration of <code>$_dependentTables</code> if you use referential integrity
  85. constraints in the RDBMS server to implement cascading operations. See
  86. <xref linkend="zend.db.table.relationships.cascading" /> for more information.
  87. </para>
  88. </note>
  89. <para>
  90. Declare the <code>$_referenceMap</code> array in the class for each dependent table.
  91. This is an associative array of reference "rules". A reference rule identifies which
  92. table is the parent table in the relationship, and also lists which columns in the
  93. dependent table reference which columns in the parent table.
  94. </para>
  95. <para>
  96. The rule key is a string used as an index to the <code>$_referenceMap</code> array.
  97. This rule key is used to identify each reference relationship. Choose a descriptive
  98. name for this rule key. It's best to use a string that can be part of a PHP method
  99. name, as you will see later.
  100. </para>
  101. <para>
  102. In the example PHP code above, the rule keys in the Bugs table class are:
  103. <code>'Reporter'</code>, <code>'Engineer'</code>, <code>'Verifier'</code>, and
  104. <code>'Product'</code>.
  105. </para>
  106. <para>
  107. The value of each rule entry in the <code>$_referenceMap</code> array is also an
  108. associative array. The elements of this rule entry are described below:
  109. </para>
  110. <itemizedlist>
  111. <listitem>
  112. <para>
  113. <emphasis role="strong">columns</emphasis> => A string or an array of strings
  114. naming the foreign key column name(s) in the dependent table.
  115. </para>
  116. <para>
  117. It's common for this to be a single column, but some tables have multi-column
  118. keys.
  119. </para>
  120. </listitem>
  121. <listitem>
  122. <para>
  123. <emphasis role="strong">refTableClass</emphasis> => The class name of the
  124. parent table. Use the class name, not the physical name of the SQL table.
  125. </para>
  126. <para>
  127. It's common for a dependent table to have only one reference to its parent
  128. table, but some tables have multiple references to the same parent table. In
  129. the example database, there is one reference from the <code>bugs</code> table
  130. to the <code>products</code> table, but three references from the
  131. <code>bugs</code> table to the <code>accounts</code> table. Put each reference
  132. in a separate entry in the <code>$_referenceMap</code> array.
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <emphasis role="strong">refColumns</emphasis> => A string or an array of
  138. strings naming the primary key column name(s) in the parent table.
  139. </para>
  140. <para>
  141. It's common for this to be a single column, but some tables have multi-column
  142. keys. If the reference uses a multi-column key, the order of columns in the
  143. <code>'columns'</code> entry must match the order of columns in the
  144. <code>'refColumns'</code> entry.
  145. </para>
  146. <para>
  147. It is optional to specify this element. If you don't specify the
  148. <code>refColumns</code>, the column(s) reported as the primary key columns of
  149. the parent table are used by default.
  150. </para>
  151. </listitem>
  152. <listitem>
  153. <para>
  154. <emphasis role="strong">onDelete</emphasis> => The rule for an action to
  155. execute if a row is deleted in the parent table. See
  156. <xref linkend="zend.db.table.relationships.cascading" /> for more information.
  157. </para>
  158. </listitem>
  159. <listitem>
  160. <para>
  161. <emphasis role="strong">onUpdate</emphasis> => The rule for an action to
  162. execute if values in primary key columns are updated in the parent table. See
  163. <xref linkend="zend.db.table.relationships.cascading" /> for more information.
  164. </para>
  165. </listitem>
  166. </itemizedlist>
  167. </sect2>
  168. <sect2 id="zend.db.table.relationships.fetching.dependent">
  169. <title>Fetching a Dependent Rowset</title>
  170. <para>
  171. If you have a Row object as the result of a query on a parent table, you can fetch rows
  172. from dependent tables that reference the current row. Use the method:
  173. </para>
  174. <programlisting role="php"><![CDATA[
  175. $row->findDependentRowset($table, [$rule]);
  176. ]]>
  177. </programlisting>
  178. <para>
  179. This method returns a Zend_Db_Table_Rowset_Abstract object, containing a set of rows
  180. from the dependent table <code>$table</code> that refer to the row identified by the
  181. <code>$row</code> object.
  182. </para>
  183. <para>
  184. The first argument <code>$table</code> can be a string that specifies the dependent
  185. table by its class name. You can also specify the dependent table by using an object of
  186. that table class.
  187. </para>
  188. <example id="zend.db.table.relationships.fetching.dependent.example">
  189. <title>Fetching a Dependent Rowset</title>
  190. <para>
  191. This example shows getting a Row object from the table <code>Accounts</code>, and
  192. finding the <code>Bugs</code> reported by that account.
  193. </para>
  194. <programlisting role="php"><![CDATA[
  195. $accountsTable = new Accounts();
  196. $accountsRowset = $accountsTable->find(1234);
  197. $user1234 = $accountsRowset->current();
  198. $bugsReportedByUser = $user1234->findDependentRowset('Bugs');
  199. ]]>
  200. </programlisting>
  201. </example>
  202. <para>
  203. The second argument <code>$rule</code> is optional. It is a string that names the rule
  204. key in the <code>$_referenceMap</code> array of the dependent table class. If you don't
  205. specify a rule, the first rule in the array that references the parent table is used.
  206. If you need to use a rule other than the first, you need to specify the key.
  207. </para>
  208. <para>
  209. In the example code above, the rule key is not specified, so the rule used by default
  210. is the first one that matches the parent table. This is the rule
  211. <code>'Reporter'</code>.
  212. </para>
  213. <example id="zend.db.table.relationships.fetching.dependent.example-by">
  214. <title>Fetching a Dependent Rowset By a Specific Rule</title>
  215. <para>
  216. This example shows getting a Row object from the table <code>Accounts</code>, and
  217. finding the <code>Bugs</code> assigned to be fixed by the user of that account. The
  218. rule key string that corresponds to this reference relationship in this example is
  219. <code>'Engineer'</code>.
  220. </para>
  221. <programlisting role="php"><![CDATA[
  222. $accountsTable = new Accounts();
  223. $accountsRowset = $accountsTable->find(1234);
  224. $user1234 = $accountsRowset->current();
  225. $bugsAssignedToUser = $user1234->findDependentRowset('Bugs', 'Engineer');
  226. ]]>
  227. </programlisting>
  228. </example>
  229. <para>
  230. You can also add criteria, ordering and limits to your relationships using the parent
  231. row's select object.
  232. </para>
  233. <para>
  234. <example id="zend.db.table.relationships.fetching.dependent.example-by-select">
  235. <title>Fetching a Dependent Rowset using a Zend_Db_Table_Select</title>
  236. <para>
  237. This example shows getting a Row object from the table <code>Accounts</code>,
  238. and finding the <code>Bugs</code> assigned to be fixed by the user of that
  239. account, limited only to 3 rows and ordered by name.
  240. </para>
  241. <programlisting role="php"><![CDATA[
  242. $accountsTable = new Accounts();
  243. $accountsRowset = $accountsTable->find(1234);
  244. $user1234 = $accountsRowset->current();
  245. $select = $accountsTable->select()->order('name ASC')
  246. ->limit(3);
  247. $bugsAssignedToUser = $user1234->findDependentRowset('Bugs',
  248. 'Engineer',
  249. $select);
  250. ]]>
  251. </programlisting>
  252. </example>
  253. Alternatively, you can query rows from a dependent table using a special mechanism
  254. called a "magic method". Zend_Db_Table_Row_Abstract invokes the method:
  255. <code>findDependentRowset('&lt;TableClass&gt;', '&lt;Rule&gt;')</code> if you invoke a method on
  256. the Row object matching either of the following patterns:
  257. </para>
  258. <itemizedlist>
  259. <listitem>
  260. <para>
  261. <code>$row->find&lt;TableClass&gt;()</code>
  262. </para>
  263. </listitem>
  264. <listitem>
  265. <para>
  266. <code>$row->find&lt;TableClass&gt;By&lt;Rule&gt;()</code>
  267. </para>
  268. </listitem>
  269. </itemizedlist>
  270. <para>
  271. In the patterns above, <code>&lt;TableClass&gt;</code> and <code>&lt;Rule&gt;</code> are strings
  272. that correspond to the class name of the dependent table, and the dependent table's
  273. rule key that references the parent table.
  274. </para>
  275. <note>
  276. <para>
  277. Some application frameworks, such as Ruby on Rails, use a mechanism called
  278. "inflection" to allow the spelling of identifiers to change depending on usage. For
  279. simplicity, Zend_Db_Table_Row does not provide any inflection mechanism. The table
  280. identity and the rule key named in the method call must match the spelling of the
  281. class and rule key exactly.
  282. </para>
  283. </note>
  284. <example id="zend.db.table.relationships.fetching.dependent.example-magic">
  285. <title>Fetching Dependent Rowsets using the Magic Method</title>
  286. <para>
  287. This example shows finding dependent Rowsets equivalent to those in the previous
  288. examples. In this case, the application uses the magic method invocation instead of
  289. specifying the table and rule as strings.
  290. </para>
  291. <programlisting role="php"><![CDATA[
  292. $accountsTable = new Accounts();
  293. $accountsRowset = $accountsTable->find(1234);
  294. $user1234 = $accountsRowset->current();
  295. // Use the default reference rule
  296. $bugsReportedBy = $user1234->findBugs();
  297. // Specify the reference rule
  298. $bugsAssignedTo = $user1234->findBugsByEngineer();
  299. ]]>
  300. </programlisting>
  301. </example>
  302. </sect2>
  303. <sect2 id="zend.db.table.relationships.fetching.parent">
  304. <title>Fetching a Parent Row</title>
  305. <para>
  306. If you have a Row object as the result of a query on a dependent table, you can fetch
  307. the row in the parent to which the dependent row refers. Use the method:
  308. </para>
  309. <programlisting role="php"><![CDATA[
  310. $row->findParentRow($table, [$rule]);
  311. ]]>
  312. </programlisting>
  313. <para>
  314. There always should be exactly one row in the parent table referenced by a dependent
  315. row, therefore this method returns a Row object, not a Rowset object.
  316. </para>
  317. <para>
  318. The first argument <code>$table</code> can be a string that specifies the parent table
  319. by its class name. You can also specify the parent table by using an object of that
  320. table class.
  321. </para>
  322. <example id="zend.db.table.relationships.fetching.parent.example">
  323. <title>Fetching the Parent Row</title>
  324. <para>
  325. This example shows getting a Row object from the table <code>Bugs</code> (for
  326. example one of those bugs with status 'NEW'), and finding the row in the
  327. <code>Accounts</code> table for the user who reported the bug.
  328. </para>
  329. <programlisting role="php"><![CDATA[
  330. $bugsTable = new Bugs();
  331. $bugsRowset = $bugsTable->fetchAll(array('bug_status = ?' => 'NEW'));
  332. $bug1 = $bugsRowset->current();
  333. $reporter = $bug1->findParentRow('Accounts');
  334. ]]>
  335. </programlisting>
  336. </example>
  337. <para>
  338. The second argument <code>$rule</code> is optional. It is a string that names the rule
  339. key in the <code>$_referenceMap</code> array of the dependent table class. If you don't
  340. specify a rule, the first rule in the array that references the parent table is used.
  341. If you need to use a rule other than the first, you need to specify the key.
  342. </para>
  343. <para>
  344. In the example above, the rule key is not specified, so the rule used by default is the
  345. first one that matches the parent table. This is the rule <code>'Reporter'</code>.
  346. </para>
  347. <example id="zend.db.table.relationships.fetching.parent.example-by">
  348. <title>Fetching a Parent Row By a Specific Rule</title>
  349. <para>
  350. This example shows getting a Row object from the table <code>Bugs</code>, and
  351. finding the account for the engineer assigned to fix that bug. The rule key string
  352. that corresponds to this reference relationship in this example is
  353. <code>'Engineer'</code>.
  354. </para>
  355. <programlisting role="php"><![CDATA[
  356. $bugsTable = new Bugs();
  357. $bugsRowset = $bugsTable->fetchAll(array('bug_status = ?', 'NEW'));
  358. $bug1 = $bugsRowset->current();
  359. $engineer = $bug1->findParentRow('Accounts', 'Engineer');
  360. ]]>
  361. </programlisting>
  362. </example>
  363. <para>
  364. Alternatively, you can query rows from a parent table using a "magic method".
  365. Zend_Db_Table_Row_Abstract invokes the method:
  366. <code>findParentRow('&lt;TableClass&gt;', '&lt;Rule&gt;')</code> if you invoke a method
  367. on the Row object matching either of the following patterns:
  368. </para>
  369. <itemizedlist>
  370. <listitem>
  371. <para>
  372. <code>$row->findParent&lt;TableClass&gt;([Zend_Db_Table_Select $select])</code>
  373. </para>
  374. </listitem>
  375. <listitem>
  376. <para>
  377. <code>$row->findParent&lt;TableClass&gt;By&lt;Rule&gt;([Zend_Db_Table_Select
  378. $select])</code>
  379. </para>
  380. </listitem>
  381. </itemizedlist>
  382. <para>
  383. In the patterns above, <code>&lt;TableClass&gt;</code> and <code>&lt;Rule&gt;</code>
  384. are strings that correspond to the class name of the parent table, and the dependent
  385. table's rule key that references the parent table.
  386. </para>
  387. <note>
  388. <para>
  389. The table identity and the rule key named in the method call must match the
  390. spelling of the class and rule key exactly.
  391. </para>
  392. </note>
  393. <example id="zend.db.table.relationships.fetching.parent.example-magic">
  394. <title>Fetching the Parent Row using the Magic Method</title>
  395. <para>
  396. This example shows finding parent Rows equivalent to those in the previous
  397. examples. In this case, the application uses the magic method invocation instead of
  398. specifying the table and rule as strings.
  399. </para>
  400. <programlisting role="php"><![CDATA[
  401. $bugsTable = new Bugs();
  402. $bugsRowset = $bugsTable->fetchAll(array('bug_status = ?', 'NEW'));
  403. $bug1 = $bugsRowset->current();
  404. // Use the default reference rule
  405. $reporter = $bug1->findParentAccounts();
  406. // Specify the reference rule
  407. $engineer = $bug1->findParentAccountsByEngineer();
  408. ]]>
  409. </programlisting>
  410. </example>
  411. </sect2>
  412. <sect2 id="zend.db.table.relationships.fetching.many-to-many">
  413. <title>Fetching a Rowset via a Many-to-many Relationship</title>
  414. <para>
  415. If you have a Row object as the result of a query on one table in a many-to-many
  416. relationship (for purposes of the example, call this the "origin" table), you can
  417. fetch corresponding rows in the other table (call this the "destination" table) via an
  418. intersection table. Use the method:
  419. </para>
  420. <programlisting role="php"><![CDATA[
  421. $row->findManyToManyRowset($table,
  422. $intersectionTable,
  423. [$rule1,
  424. [$rule2,
  425. [Zend_Db_Table_Select $select]
  426. ]
  427. ]);
  428. ]]>
  429. </programlisting>
  430. <para>
  431. This method returns a Zend_Db_Table_Rowset_Abstract containing rows from the table
  432. <code>$table</code>, satisfying the many-to-many relationship. The current Row object
  433. <code>$row</code> from the origin table is used to find rows in the intersection table,
  434. and that is joined to the destination table.
  435. </para>
  436. <para>
  437. The first argument <code>$table</code> can be a string that specifies the destination
  438. table in the many-to-many relationship by its class name. You can also specify the
  439. destination table by using an object of that table class.
  440. </para>
  441. <para>
  442. The second argument <code>$intersectionTable</code> can be a string that specifies the
  443. intersection table between the two tables in the the many-to-many relationship by its
  444. class name. You can also specify the intersection table by using an object of that
  445. table class.
  446. </para>
  447. <example id="zend.db.table.relationships.fetching.many-to-many.example">
  448. <title>Fetching a Rowset with the Many-to-many Method</title>
  449. <para>
  450. This example shows getting a Row object from from the origin table
  451. <code>Bugs</code>, and finding rows from the destination table
  452. <code>Products</code>, representing products related to that bug.
  453. </para>
  454. <programlisting role="php"><![CDATA[
  455. $bugsTable = new Bugs();
  456. $bugsRowset = $bugsTable->find(1234);
  457. $bug1234 = $bugsRowset->current();
  458. $productsRowset = $bug1234->findManyToManyRowset('Products',
  459. 'BugsProducts');
  460. ]]>
  461. </programlisting>
  462. </example>
  463. <para>
  464. The third and fourth arguments <code>$rule1</code> and <code>$rule2</code> are
  465. optional. These are strings that name the rule keys in the <code>$_referenceMap</code>
  466. array of the intersection table.
  467. </para>
  468. <para>
  469. The <code>$rule1</code> key names the rule for the relationship from the intersection
  470. table to the origin table. In this example, this is the relationship from
  471. <code>BugsProducts</code> to <code>Bugs</code>.
  472. </para>
  473. <para>
  474. The <code>$rule2</code> key names the rule for the relationship from the intersection
  475. table to the destination table. In this example, this is the relationship from
  476. <code>Bugs</code> to <code>Products</code>.
  477. </para>
  478. <para>
  479. Similarly to the methods for finding parent and dependent rows, if you don't specify a
  480. rule, the method uses the first rule in the <code>$_referenceMap</code> array that
  481. matches the tables in the relationship. If you need to use a rule other than the first,
  482. you need to specify the key.
  483. </para>
  484. <para>
  485. In the example code above, the rule key is not specified, so the rules used by default
  486. are the first ones that match. In this case, <code>$rule1</code> is
  487. <code>'Reporter'</code> and <code>$rule2</code> is <code>'Product'</code>.
  488. </para>
  489. <example id="zend.db.table.relationships.fetching.many-to-many.example-by">
  490. <title>Fetching a Rowset with the Many-to-many Method By a Specific Rule</title>
  491. <para>
  492. This example shows geting a Row object from from the origin table
  493. <code>Bugs</code>, and finding rows from the destination table
  494. <code>Products</code>, representing products related to that bug.
  495. </para>
  496. <programlisting role="php"><![CDATA[
  497. $bugsTable = new Bugs();
  498. $bugsRowset = $bugsTable->find(1234);
  499. $bug1234 = $bugsRowset->current();
  500. $productsRowset = $bug1234->findManyToManyRowset('Products',
  501. 'BugsProducts',
  502. 'Bug');
  503. ]]>
  504. </programlisting>
  505. </example>
  506. <para>
  507. Alternatively, you can query rows from the destination table in a many-to-many
  508. relationship using a "magic method." Zend_Db_Table_Row_Abstract invokes the method:
  509. <code>findManyToManyRowset('&lt;TableClass&gt;', '&lt;IntersectionTableClass&gt;',
  510. '&lt;Rule1&gt;', '&lt;Rule2&gt;')</code> if you invoke a method matching any of the
  511. following patterns:
  512. </para>
  513. <itemizedlist>
  514. <listitem>
  515. <para>
  516. <code>$row->find&lt;TableClass&gt;Via&lt;IntersectionTableClass&gt;
  517. ([Zend_Db_Table_Select $select])</code>
  518. </para>
  519. </listitem>
  520. <listitem>
  521. <para>
  522. <code>$row->find&lt;TableClass&gt;Via&lt;IntersectionTableClass&gt;By&lt;Rule1&gt;
  523. ([Zend_Db_Table_Select $select])</code>
  524. </para>
  525. </listitem>
  526. <listitem>
  527. <para>
  528. <code>$row->find&lt;TableClass&gt;Via&lt;IntersectionTableClass&gt;By&lt;Rule1&gt;And&lt;Rule2&gt;
  529. ([Zend_Db_Table_Select $select])</code>
  530. </para>
  531. </listitem>
  532. </itemizedlist>
  533. <para>
  534. In the patterns above, <code>&lt;TableClass&gt;</code> and
  535. <code>&lt;IntersectionTableClass&gt;</code> are strings that correspond to the class
  536. names of the destination table and the intersection table, respectively.
  537. <code>&lt;Rule1&gt;</code> and <code>&lt;Rule2&gt;</code> are strings that correspond
  538. to the rule keys in the intersection table that reference the origin table and the
  539. destination table, respectively.
  540. </para>
  541. <note>
  542. <para>
  543. The table identities and the rule keys named in the method call must match the
  544. spelling of the class and rule key exactly.
  545. </para>
  546. </note>
  547. <example id="zend.db.table.relationships.fetching.many-to-many.example-magic">
  548. <title>Fetching Rowsets using the Magic Many-to-many Method</title>
  549. <para>
  550. This example shows finding rows in the destination table of a many-to-many
  551. relationship representing products related to a given bug.
  552. </para>
  553. <programlisting role="php"><![CDATA[
  554. $bugsTable = new Bugs();
  555. $bugsRowset = $bugsTable->find(1234);
  556. $bug1234 = $bugsRowset->current();
  557. // Use the default reference rule
  558. $products = $bug1234->findProductsViaBugsProducts();
  559. // Specify the reference rule
  560. $products = $bug1234->findProductsViaBugsProductsByBug();
  561. ]]>
  562. </programlisting>
  563. </example>
  564. </sect2>
  565. <sect2 id="zend.db.table.relationships.cascading">
  566. <title>Cascading Write Operations</title>
  567. <note>
  568. <title>Declare DRI in the database:</title>
  569. <para>
  570. Declaring cascading operations in Zend_Db_Table is intended
  571. <emphasis role="strong">only</emphasis> for RDBMS brands that do not support
  572. declarative referential integrity (DRI).
  573. </para>
  574. <para>
  575. For example, if you use MySQL's MyISAM storage engine, or SQLite, these solutions
  576. do not support DRI. You may find it helpful to declare the cascading operations
  577. with Zend_Db_Table.
  578. </para>
  579. <para>
  580. If your RDBMS implements DRI and the <code>ON DELETE</code> and
  581. <code>ON UPDATE</code> clauses, you should declare these clauses in your database
  582. schema, instead of using the cascading feature in Zend_Db_Table. Declaring
  583. cascading DRI rules in the RDBMS is better for database performance, consistency,
  584. and integrity.
  585. </para>
  586. <para>
  587. Most importantly, do not declare cascading operations both in the RDBMS and in your
  588. Zend_Db_Table class.
  589. </para>
  590. </note>
  591. <para>
  592. You can declare cascading operations to execute against a dependent table when you
  593. apply an <code>UPDATE</code> or a <code>DELETE</code> to a row in a parent table.
  594. </para>
  595. <example id="zend.db.table.relationships.cascading.example-delete">
  596. <title>Example of a Cascading Delete</title>
  597. <para>
  598. This example shows deleting a row in the <code>Products</code> table, which is
  599. configured to automatically delete dependent rows in the <code>Bugs</code> table.
  600. </para>
  601. <programlisting role="php"><![CDATA[
  602. $productsTable = new Products();
  603. $productsRowset = $productsTable->find(1234);
  604. $product1234 = $productsRowset->current();
  605. $product1234->delete();
  606. // Automatically cascades to Bugs table
  607. // and deletes dependent rows.
  608. ]]>
  609. </programlisting>
  610. </example>
  611. <para>
  612. Similarly, if you use <code>UPDATE</code> to change the value of a primary key in a
  613. parent table, you may want the value in foreign keys of dependent tables to be updated
  614. automatically to match the new value, so that such references are kept up to date.
  615. </para>
  616. <para>
  617. It's usually not necessary to update the value of a primary key that was generated by a
  618. sequence or other mechanism. But if you use a <emphasis>natural key</emphasis> that may
  619. change value occasionally, it is more likely that you need to apply cascading updates
  620. to dependent tables.
  621. </para>
  622. <para>
  623. To declare a cascading relationship in the Zend_Db_Table, edit the rules in the
  624. <code>$_referenceMap</code>. Set the associative array keys <code>'onDelete'</code> and
  625. <code>'onUpdate'</code> to the string 'cascade' (or the constant
  626. <code>self::CASCADE</code>). Before a row is deleted from the parent table, or its
  627. primary key values updated, any rows in the dependent table that refer to the parent's
  628. row are deleted or updated first.
  629. </para>
  630. <example id="zend.db.table.relationships.cascading.example-declaration">
  631. <title>Example Declaration of Cascading Operations</title>
  632. <para>
  633. In the example below, rows in the <code>Bugs</code> table are automatically deleted
  634. if the row in the <code>Products</code> table to which they refer is deleted. The
  635. <code>'onDelete'</code> element of the reference map entry is set to
  636. <code>self::CASCADE</code>.
  637. </para>
  638. <para>
  639. No cascading update is done in the example below if the primary key value in the
  640. parent class is changed. The <code>'onUpdate'</code> element of the reference map
  641. entry is <code>self::RESTRICT</code>. You can get the same result using the value
  642. <code>self::NO_ACTION</code>, or by omitting the <code>'onUpdate'</code> entry.
  643. </para>
  644. <programlisting role="php"><![CDATA[
  645. class BugsProducts extends Zend_Db_Table_Abstract
  646. {
  647. ...
  648. protected $_referenceMap = array(
  649. 'Product' => array(
  650. 'columns' => array('product_id'),
  651. 'refTableClass' => 'Products',
  652. 'refColumns' => array('product_id'),
  653. 'onDelete' => self::CASCADE,
  654. 'onUpdate' => self::RESTRICT
  655. ),
  656. ...
  657. );
  658. }
  659. ]]>
  660. </programlisting>
  661. </example>
  662. <sect3 id="zend.db.table.relationships.cascading.notes">
  663. <title>Notes Regarding Cascading Operations</title>
  664. <para>
  665. <emphasis role="strong">Cascading operations invoked by Zend_Db_Table are not
  666. atomic.</emphasis>
  667. </para>
  668. <para>
  669. This means that if your database implements and enforces referential integrity
  670. constraints, a cascading <code>UPDATE</code> executed by a Zend_Db_Table class
  671. conflicts with the constraint, and results in a referential integrity violation.
  672. You can use cascading <code>UPDATE</code> in Zend_Db_Table
  673. <emphasis>only</emphasis> if your database does not enforce that referential
  674. integrity constraint.
  675. </para>
  676. <para>
  677. Cascading <code>DELETE</code> suffers less from the problem of referential
  678. integrity violations. You can delete dependent rows as a non-atomic action before
  679. deleting the parent row that they reference.
  680. </para>
  681. <para>
  682. However, for both <code>UPDATE</code> and <code>DELETE</code>, changing the
  683. database in a non-atomic way also creates the risk that another database user can
  684. see the data in an inconsistent state. For example, if you delete a row and all its
  685. dependent rows, there is a small chance that another database client program can
  686. query the database after you have deleted the dependent rows, but before you delete
  687. the parent row. That client program may see the parent row with no dependent rows,
  688. and assume this is the intended state of the data. There is no way for that client
  689. to know that its query read the database in the middle of a change.
  690. </para>
  691. <para>
  692. The issue of non-atomic change can be mitigated by using transactions to isolate
  693. your change. But some RDBMS brands don't support transactions, or allow clients to
  694. read "dirty" changes that have not been committed yet.
  695. </para>
  696. <para>
  697. <emphasis role="strong">Cascading operations in Zend_Db_Table are invoked only by
  698. Zend_Db_Table.</emphasis>
  699. </para>
  700. <para>
  701. Cascading deletes and updates defined in your Zend_Db_Table classes are applied if
  702. you execute the <code>save()</code> or <code>delete()</code> methods on the Row
  703. class. However, if you update or delete data using another interface, such as a
  704. query tool or another application, the cascading operations are not applied. Even
  705. when using <code>update()</code> and <code>delete()</code> methods in the
  706. Zend_Db_Adapter class, cascading operations defined in your Zend_Db_Table classes
  707. are not executed.
  708. </para>
  709. <para>
  710. <emphasis role="strong">No Cascading <code>INSERT</code>.</emphasis>
  711. </para>
  712. <para>
  713. There is no support for a cascading <code>INSERT</code>. You must insert a row to a
  714. parent table in one operation, and insert row(s) to a dependent table in a separate
  715. operation.
  716. </para>
  717. </sect3>
  718. </sect2>
  719. </sect1>
  720. <!--
  721. vim:se ts=4 sw=4 et:
  722. -->