Zend_Db_Table-Relationships.xml 36 KB

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