Zend_Db_Table-Relationships.xml 36 KB

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