Zend_Db_Table-Relationships.xml 37 KB

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