Zend_Db_Table-Relationships.xml 36 KB

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