Zend_Db_Table-Relationships.xml 37 KB

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