Zend_Db_Table-Relationships.xml 38 KB

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