Zend_Db_Table-Relationships.xml 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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
  15. <classname>Zend_Db_Table_Row</classname>
  16. class has methods for querying related rows
  17. in other tables.
  18. </para>
  19. </sect2>
  20. <sect2 id="zend.db.table.relationships.defining">
  21. <title>Defining Relationships</title>
  22. <para>
  23. Define classes for each of your tables, extending the abstract class
  24. <classname>Zend_Db_Table_Abstract</classname>
  25. , as described in
  26. <xref linkend="zend.db.table.defining"/>
  27. . Also
  28. see
  29. <xref linkend="zend.db.adapter.example-database"/>
  30. for a description of the
  31. example database for which the following example code is
  32. designed.
  33. </para>
  34. <para>
  35. Below are the
  36. <acronym>PHP</acronym>
  37. class definitions for these tables:
  38. </para>
  39. <programlisting language="php"><![CDATA[
  40. class Accounts extends Zend_Db_Table_Abstract
  41. {
  42. protected $_name = 'accounts';
  43. protected $_dependentTables = array('Bugs');
  44. }
  45. class Products extends Zend_Db_Table_Abstract
  46. {
  47. protected $_name = 'products';
  48. protected $_dependentTables = array('BugsProducts');
  49. }
  50. class Bugs extends Zend_Db_Table_Abstract
  51. {
  52. protected $_name = 'bugs';
  53. protected $_dependentTables = array('BugsProducts');
  54. protected $_referenceMap = array(
  55. 'Reporter' => array(
  56. 'columns' => 'reported_by',
  57. 'refTableClass' => 'Accounts',
  58. 'refColumns' => 'account_name'
  59. ),
  60. 'Engineer' => array(
  61. 'columns' => 'assigned_to',
  62. 'refTableClass' => 'Accounts',
  63. 'refColumns' => 'account_name'
  64. ),
  65. 'Verifier' => array(
  66. 'columns' => array('verified_by'),
  67. 'refTableClass' => 'Accounts',
  68. 'refColumns' => array('account_name')
  69. )
  70. );
  71. }
  72. class BugsProducts extends Zend_Db_Table_Abstract
  73. {
  74. protected $_name = 'bugs_products';
  75. protected $_referenceMap = array(
  76. 'Bug' => array(
  77. 'columns' => array('bug_id'),
  78. 'refTableClass' => 'Bugs',
  79. 'refColumns' => array('bug_id')
  80. ),
  81. 'Product' => array(
  82. 'columns' => array('product_id'),
  83. 'refTableClass' => 'Products',
  84. 'refColumns' => array('product_id')
  85. )
  86. );
  87. }
  88. ]]></programlisting>
  89. <para>
  90. If you use
  91. <classname>Zend_Db_Table</classname>
  92. to emulate cascading UPDATE and DELETE
  93. operations, declare the
  94. <varname>$_dependentTables</varname>
  95. array in the class for the
  96. parent table. List the class name for each dependent table.
  97. Use the class name, not the
  98. physical name of the
  99. <acronym>SQL</acronym>
  100. table.
  101. </para>
  102. <note>
  103. <para>
  104. Skip declaration of
  105. <varname>$_dependentTables</varname>
  106. if you use referential
  107. integrity constraints in the
  108. <acronym>RDBMS</acronym>
  109. server to implement cascading
  110. operations. See
  111. <xref linkend="zend.db.table.relationships.cascading"/>
  112. for more
  113. information.
  114. </para>
  115. </note>
  116. <para>
  117. Declare the
  118. <varname>$_referenceMap</varname>
  119. array in the class for each dependent
  120. table. This is an associative array of reference
  121. "rules". A reference rule identifies
  122. which table is the parent table in the relationship,
  123. and also lists which columns in the
  124. dependent table reference which columns in the parent
  125. table.
  126. </para>
  127. <para>
  128. The rule key is a string used as an index to the
  129. <varname>$_referenceMap</varname>
  130. array. This rule key is used to identify each reference relationship. Choose a
  131. descriptive name for this rule key. It's best to use a string that can be part of a
  132. <acronym>PHP</acronym>
  133. method name, as you will see later.
  134. </para>
  135. <para>
  136. In the example
  137. <acronym>PHP</acronym>
  138. code above, the rule keys in the Bugs table class
  139. are:
  140. <code>'Reporter'</code>
  141. ,
  142. <code>'Engineer'</code>
  143. ,
  144. <code>'Verifier'</code>
  145. , and
  146. <code>'Product'</code>
  147. .
  148. </para>
  149. <para>
  150. The value of each rule entry in the
  151. <varname>$_referenceMap</varname>
  152. array is also an
  153. associative array. The elements of this rule entry are described below:
  154. </para>
  155. <itemizedlist>
  156. <listitem>
  157. <para>
  158. <emphasis>columns</emphasis>
  159. => A string or an array of strings
  160. naming the foreign key column name(s) in the
  161. dependent table.
  162. </para>
  163. <para>
  164. It's common for this to be a single column, but some tables have multi-column
  165. keys.
  166. </para>
  167. </listitem>
  168. <listitem>
  169. <para>
  170. <emphasis>refTableClass</emphasis>
  171. => The class name of the parent table. Use
  172. the class name, not the physical name
  173. of the
  174. <acronym>SQL</acronym>
  175. table.
  176. </para>
  177. <para>
  178. It's common for a dependent table to have only one reference to its parent
  179. table, but some tables have multiple references to the same parent table. In
  180. the
  181. example database, there is one reference from the
  182. <code>bugs</code>
  183. table
  184. to the
  185. <code>products</code>
  186. table, but three references from the
  187. <code>bugs</code>
  188. table to the
  189. <code>accounts</code>
  190. table. Put each reference
  191. in a separate entry in the
  192. <varname>$_referenceMap</varname>
  193. array.
  194. </para>
  195. </listitem>
  196. <listitem>
  197. <para>
  198. <emphasis>refColumns</emphasis>
  199. => A string or an array of
  200. strings naming the primary key column name(s) in the
  201. parent table.
  202. </para>
  203. <para>
  204. It's common for this to be a single column, but some tables have multi-column
  205. keys. If the reference uses a multi-column key, the order of columns in the
  206. <code>'columns'</code>
  207. entry must match the order of columns in the
  208. <code>'refColumns'</code>
  209. entry.
  210. </para>
  211. <para>
  212. It is optional to specify this element. If you don't specify the
  213. <code>refColumns</code>
  214. , the column(s) reported as the primary key columns of
  215. the parent table are used
  216. by default.
  217. </para>
  218. </listitem>
  219. <listitem>
  220. <para>
  221. <emphasis>onDelete</emphasis>
  222. => The rule for an action to
  223. execute if a row is deleted in the parent table. See
  224. <xref linkend="zend.db.table.relationships.cascading"/>
  225. for more information.
  226. </para>
  227. </listitem>
  228. <listitem>
  229. <para>
  230. <emphasis>onUpdate</emphasis>
  231. => The rule for an action to
  232. execute if values in primary key columns are updated
  233. in the parent table. See
  234. <xref linkend="zend.db.table.relationships.cascading"/>
  235. for more information.
  236. </para>
  237. </listitem>
  238. </itemizedlist>
  239. </sect2>
  240. <sect2 id="zend.db.table.relationships.fetching.dependent">
  241. <title>Fetching a Dependent Rowset</title>
  242. <para>
  243. If you have a Row object as the result of a query on a parent table, you can fetch
  244. rows
  245. from dependent tables that reference the current row. Use the method:
  246. </para>
  247. <programlisting language="php"><![CDATA[
  248. $row->findDependentRowset($table, [$rule]);
  249. ]]></programlisting>
  250. <para>
  251. This method returns a
  252. <classname>Zend_Db_Table_Rowset_Abstract</classname>
  253. object,
  254. containing a set of rows from the dependent table
  255. <varname>$table</varname>
  256. that refer
  257. to the row identified by the
  258. <varname>$row</varname>
  259. object.
  260. </para>
  261. <para>
  262. The first argument
  263. <varname>$table</varname>
  264. can be a string that specifies the
  265. dependent table by its class name. You can also
  266. specify the dependent table by using an
  267. object of that table class.
  268. </para>
  269. <example id="zend.db.table.relationships.fetching.dependent.example">
  270. <title>Fetching a Dependent Rowset</title>
  271. <para>
  272. This example shows getting a Row object from the table
  273. <code>Accounts</code>
  274. , and
  275. finding the
  276. <code>Bugs</code>
  277. reported by that account.
  278. </para>
  279. <programlisting language="php"><![CDATA[
  280. $accountsTable = new Accounts();
  281. $accountsRowset = $accountsTable->find(1234);
  282. $user1234 = $accountsRowset->current();
  283. $bugsReportedByUser = $user1234->findDependentRowset('Bugs');
  284. ]]></programlisting>
  285. </example>
  286. <para>
  287. The second argument
  288. <varname>$rule</varname>
  289. is optional. It is a string that names the
  290. rule key in the
  291. <varname>$_referenceMap</varname>
  292. array of the dependent table class. If
  293. you don't specify a rule, the first rule in the
  294. array that references the parent table
  295. is used. If you need to use a rule other than the
  296. first, you need to specify the key.
  297. </para>
  298. <para>
  299. In the example code above, the rule key is not specified, so the rule used by default
  300. is
  301. the first one that matches the parent table. This is the rule
  302. <code>'Reporter'</code>
  303. .
  304. </para>
  305. <example id="zend.db.table.relationships.fetching.dependent.example-by">
  306. <title>Fetching a Dependent Rowset By a Specific Rule</title>
  307. <para>
  308. This example shows getting a Row object from the table
  309. <code>Accounts</code>
  310. , and
  311. finding the
  312. <code>Bugs</code>
  313. assigned to be fixed by the user of that account. The
  314. rule key string that
  315. corresponds to this reference relationship in this example is
  316. <code>'Engineer'</code>
  317. .
  318. </para>
  319. <programlisting language="php"><![CDATA[
  320. $accountsTable = new Accounts();
  321. $accountsRowset = $accountsTable->find(1234);
  322. $user1234 = $accountsRowset->current();
  323. $bugsAssignedToUser = $user1234->findDependentRowset('Bugs', 'Engineer');
  324. ]]></programlisting>
  325. </example>
  326. <para>
  327. You can also add criteria, ordering and limits to your relationships using the parent
  328. row's select object.
  329. </para>
  330. <para>
  331. <example id="zend.db.table.relationships.fetching.dependent.example-by-select">
  332. <title>Fetching a Dependent Rowset using a Zend_Db_Table_Select</title>
  333. <para>
  334. This example shows getting a Row object from the table
  335. <code>Accounts</code>
  336. ,
  337. and finding the
  338. <code>Bugs</code>
  339. assigned to be fixed by the user of that
  340. account, limited only to 3 rows and
  341. ordered by name.
  342. </para>
  343. <programlisting language="php"><![CDATA[
  344. $accountsTable = new Accounts();
  345. $accountsRowset = $accountsTable->find(1234);
  346. $user1234 = $accountsRowset->current();
  347. $select = $accountsTable->select()->order('name ASC')
  348. ->limit(3);
  349. $bugsAssignedToUser = $user1234->findDependentRowset('Bugs',
  350. 'Engineer',
  351. $select);
  352. ]]></programlisting>
  353. </example>
  354. Alternatively, you can query rows from a dependent table using a special mechanism
  355. called a "magic method".
  356. <classname>Zend_Db_Table_Row_Abstract</classname>
  357. invokes the
  358. method:
  359. <methodname>findDependentRowset('&lt;TableClass&gt;',
  360. '&lt;Rule&gt;')</methodname>
  361. if you invoke a method on the Row object matching
  362. either of the following patterns:
  363. </para>
  364. <itemizedlist>
  365. <listitem>
  366. <para>
  367. <code>$row->find&lt;TableClass&gt;()</code>
  368. </para>
  369. </listitem>
  370. <listitem>
  371. <para>
  372. <code>$row->find&lt;TableClass&gt;By&lt;Rule&gt;()</code>
  373. </para>
  374. </listitem>
  375. </itemizedlist>
  376. <para>
  377. In the patterns above,
  378. <code>&lt;TableClass&gt;</code>
  379. and
  380. <code>&lt;Rule&gt;</code>
  381. are
  382. strings that correspond to the class name of the dependent table, and the dependent
  383. table's rule key that references the parent table.
  384. </para>
  385. <note>
  386. <para>
  387. Some application frameworks, such as Ruby on Rails, use a mechanism called
  388. "inflection" to allow the spelling of identifiers to change depending on usage. For
  389. simplicity,
  390. <classname>Zend_Db_Table_Row</classname>
  391. does not provide any inflection
  392. mechanism. The table identity and the rule key named
  393. in the method call must match
  394. the spelling of the class and rule key exactly.
  395. </para>
  396. </note>
  397. <example id="zend.db.table.relationships.fetching.dependent.example-magic">
  398. <title>Fetching Dependent Rowsets using the Magic Method</title>
  399. <para>
  400. This example shows finding dependent Rowsets equivalent to those in the previous
  401. examples. In this case, the application uses the magic method invocation instead of
  402. specifying the table and rule as strings.
  403. </para>
  404. <programlisting language="php"><![CDATA[
  405. $accountsTable = new Accounts();
  406. $accountsRowset = $accountsTable->find(1234);
  407. $user1234 = $accountsRowset->current();
  408. // Use the default reference rule
  409. $bugsReportedBy = $user1234->findBugs();
  410. // Specify the reference rule
  411. $bugsAssignedTo = $user1234->findBugsByEngineer();
  412. ]]></programlisting>
  413. </example>
  414. </sect2>
  415. <sect2 id="zend.db.table.relationships.fetching.parent">
  416. <title>Fetching a Parent Row</title>
  417. <para>
  418. If you have a Row object as the result of a query on a dependent table, you can fetch
  419. the row in the parent to which the dependent row refers. Use the method:
  420. </para>
  421. <programlisting language="php"><![CDATA[
  422. $row->findParentRow($table, [$rule]);
  423. ]]></programlisting>
  424. <para>
  425. There always should be exactly one row in the parent table referenced by a dependent
  426. row, therefore this method returns a Row object, not a Rowset object.
  427. </para>
  428. <para>
  429. The first argument
  430. <varname>$table</varname>
  431. can be a string that specifies the parent
  432. table by its class name. You can also specify
  433. the parent table by using an object of
  434. that table class.
  435. </para>
  436. <example id="zend.db.table.relationships.fetching.parent.example">
  437. <title>Fetching the Parent Row</title>
  438. <para>
  439. This example shows getting a Row object from the table
  440. <code>Bugs</code>
  441. (for
  442. example one of those bugs with status 'NEW'), and finding the row in the
  443. <code>Accounts</code>
  444. table for the user who reported the bug.
  445. </para>
  446. <programlisting language="php"><![CDATA[
  447. $bugsTable = new Bugs();
  448. $bugsRowset = $bugsTable->fetchAll(array('bug_status = ?' => 'NEW'));
  449. $bug1 = $bugsRowset->current();
  450. $reporter = $bug1->findParentRow('Accounts');
  451. ]]></programlisting>
  452. </example>
  453. <para>
  454. The second argument
  455. <varname>$rule</varname>
  456. is optional. It is a string that names the
  457. rule key in the
  458. <varname>$_referenceMap</varname>
  459. array of the dependent table class. If
  460. you don't specify a rule, the first rule in the
  461. array that references the parent table
  462. is used. If you need to use a rule other than the
  463. first, you need to specify the key.
  464. </para>
  465. <para>
  466. In the example above, the rule key is not specified, so the rule used by default is the
  467. first one that matches the parent table. This is the rule
  468. <code>'Reporter'</code>
  469. .
  470. </para>
  471. <example id="zend.db.table.relationships.fetching.parent.example-by">
  472. <title>Fetching a Parent Row By a Specific Rule</title>
  473. <para>
  474. This example shows getting a Row object from the table
  475. <code>Bugs</code>
  476. , and
  477. finding the account for the engineer assigned to fix that bug. The rule key
  478. string
  479. that corresponds to this reference relationship in this example is
  480. <code>'Engineer'</code>
  481. .
  482. </para>
  483. <programlisting language="php"><![CDATA[
  484. $bugsTable = new Bugs();
  485. $bugsRowset = $bugsTable->fetchAll(array('bug_status = ?', 'NEW'));
  486. $bug1 = $bugsRowset->current();
  487. $engineer = $bug1->findParentRow('Accounts', 'Engineer');
  488. ]]></programlisting>
  489. </example>
  490. <para>
  491. Alternatively, you can query rows from a parent table using a "magic method".
  492. <classname>Zend_Db_Table_Row_Abstract</classname>
  493. invokes the method:
  494. <methodname>findParentRow('&lt;TableClass&gt;', '&lt;Rule&gt;')</methodname>
  495. if you
  496. invoke a method on the Row object matching either of the following patterns:
  497. </para>
  498. <itemizedlist>
  499. <listitem>
  500. <para>
  501. <code>$row->findParent&lt;TableClass&gt;([Zend_Db_Table_Select $select])</code>
  502. </para>
  503. </listitem>
  504. <listitem>
  505. <para>
  506. <code>$row->findParent&lt;TableClass&gt;By&lt;Rule&gt;([Zend_Db_Table_Select
  507. $select])</code>
  508. </para>
  509. </listitem>
  510. </itemizedlist>
  511. <para>
  512. In the patterns above,
  513. <code>&lt;TableClass&gt;</code>
  514. and
  515. <code>&lt;Rule&gt;</code>
  516. are strings that correspond to the class name of the parent table, and the dependent
  517. table's rule key that references the parent table.
  518. </para>
  519. <note>
  520. <para>
  521. The table identity and the rule key named in the method call must match the
  522. spelling of the class and rule key exactly.
  523. </para>
  524. </note>
  525. <example id="zend.db.table.relationships.fetching.parent.example-magic">
  526. <title>Fetching the Parent Row using the Magic Method</title>
  527. <para>
  528. This example shows finding parent Rows equivalent to those in the previous
  529. examples. In this case, the application uses the magic method invocation instead of
  530. specifying the table and rule as strings.
  531. </para>
  532. <programlisting language="php"><![CDATA[
  533. $bugsTable = new Bugs();
  534. $bugsRowset = $bugsTable->fetchAll(array('bug_status = ?', 'NEW'));
  535. $bug1 = $bugsRowset->current();
  536. // Use the default reference rule
  537. $reporter = $bug1->findParentAccounts();
  538. // Specify the reference rule
  539. $engineer = $bug1->findParentAccountsByEngineer();
  540. ]]></programlisting>
  541. </example>
  542. </sect2>
  543. <sect2 id="zend.db.table.relationships.fetching.many-to-many">
  544. <title>Fetching a Rowset via a Many-to-many Relationship</title>
  545. <para>
  546. If you have a Row object as the result of a query on one table in a many-to-many
  547. relationship (for purposes of the example, call this the "origin" table), you can
  548. fetch
  549. corresponding rows in the other table (call this the "destination" table) via an
  550. intersection table. Use the method:
  551. </para>
  552. <programlisting language="php"><![CDATA[
  553. $row->findManyToManyRowset($table,
  554. $intersectionTable,
  555. [$rule1,
  556. [$rule2,
  557. [Zend_Db_Table_Select $select]
  558. ]
  559. ]);
  560. ]]></programlisting>
  561. <para>
  562. This method returns a
  563. <classname>Zend_Db_Table_Rowset_Abstract</classname>
  564. containing
  565. rows from the table
  566. <varname>$table</varname>
  567. , satisfying the many-to-many relationship.
  568. The current Row object
  569. <varname>$row</varname>
  570. from the origin table is used to find
  571. rows in the intersection table, and that is joined
  572. to the destination table.
  573. </para>
  574. <para>
  575. The first argument
  576. <varname>$table</varname>
  577. can be a string that specifies the
  578. destination table in the many-to-many relationship by
  579. its class name. You can also
  580. specify the destination table by using an object of that
  581. table class.
  582. </para>
  583. <para>
  584. The second argument
  585. <varname>$intersectionTable</varname>
  586. can be a string that specifies
  587. the intersection table between the two tables in the
  588. many-to-many relationship by
  589. its class name. You can also specify the intersection table
  590. by using an object of that
  591. table class.
  592. </para>
  593. <example id="zend.db.table.relationships.fetching.many-to-many.example">
  594. <title>Fetching a Rowset with the Many-to-many Method</title>
  595. <para>
  596. This example shows getting a Row object from the origin table
  597. <code>Bugs</code>
  598. , and finding rows from the destination table
  599. <code>Products</code>
  600. , representing products related to that bug.
  601. </para>
  602. <programlisting language="php"><![CDATA[
  603. $bugsTable = new Bugs();
  604. $bugsRowset = $bugsTable->find(1234);
  605. $bug1234 = $bugsRowset->current();
  606. $productsRowset = $bug1234->findManyToManyRowset('Products',
  607. 'BugsProducts');
  608. ]]></programlisting>
  609. </example>
  610. <para>
  611. The third and fourth arguments
  612. <varname>$rule1</varname>
  613. and
  614. <varname>$rule2</varname>
  615. are optional. These are strings that name the rule keys in the
  616. <varname>$_referenceMap</varname>
  617. array of the intersection table.
  618. </para>
  619. <para>
  620. The
  621. <varname>$rule1</varname>
  622. key names the rule for the relationship from the
  623. intersection table to the origin table.
  624. In this example, this is the relationship from
  625. <code>BugsProducts</code>
  626. to
  627. <code>Bugs</code>
  628. .
  629. </para>
  630. <para>
  631. The
  632. <varname>$rule2</varname>
  633. key names the rule for the relationship from the
  634. intersection table to the destination
  635. table. In this example, this is the relationship
  636. from
  637. <code>Bugs</code>
  638. to
  639. <code>Products</code>
  640. .
  641. </para>
  642. <para>
  643. Similarly to the methods for finding parent and dependent rows, if you don't specify a
  644. rule, the method uses the first rule in the
  645. <varname>$_referenceMap</varname>
  646. array that
  647. matches the tables in the relationship. If you need to use a rule other than
  648. the first,
  649. you need to specify the key.
  650. </para>
  651. <para>
  652. In the example code above, the rule key is not specified, so the rules used by default
  653. are the first ones that match. In this case,
  654. <varname>$rule1</varname>
  655. is
  656. <code>'Reporter'</code>
  657. and
  658. <varname>$rule2</varname>
  659. is
  660. <code>'Product'</code>
  661. .
  662. </para>
  663. <example id="zend.db.table.relationships.fetching.many-to-many.example-by">
  664. <title>Fetching a Rowset with the Many-to-many Method By a Specific Rule</title>
  665. <para>
  666. This example shows geting a Row object from the origin table
  667. <code>Bugs</code>
  668. , and finding rows from the destination table
  669. <code>Products</code>
  670. , representing products related to that bug.
  671. </para>
  672. <programlisting language="php"><![CDATA[
  673. $bugsTable = new Bugs();
  674. $bugsRowset = $bugsTable->find(1234);
  675. $bug1234 = $bugsRowset->current();
  676. $productsRowset = $bug1234->findManyToManyRowset('Products',
  677. 'BugsProducts',
  678. 'Bug');
  679. ]]></programlisting>
  680. </example>
  681. <para>
  682. Alternatively, you can query rows from the destination table in a many-to-many
  683. relationship using a "magic method."
  684. <classname>Zend_Db_Table_Row_Abstract</classname>
  685. invokes the method:
  686. <code>findManyToManyRowset('&lt;TableClass&gt;',
  687. '&lt;IntersectionTableClass&gt;', '&lt;Rule1&gt;', '&lt;Rule2&gt;')</code>
  688. if you invoke
  689. a method matching any of the following patterns:
  690. </para>
  691. <itemizedlist>
  692. <listitem>
  693. <para>
  694. <code>$row->find&lt;TableClass&gt;Via&lt;IntersectionTableClass&gt;
  695. ([Zend_Db_Table_Select $select])</code>
  696. </para>
  697. </listitem>
  698. <listitem>
  699. <para>
  700. <code>$row->find&lt;TableClass&gt;Via&lt;IntersectionTableClass&gt;By&lt;Rule1&gt;
  701. ([Zend_Db_Table_Select $select])</code>
  702. </para>
  703. </listitem>
  704. <listitem>
  705. <para>
  706. <code>$row->find&lt;TableClass&gt;Via&lt;IntersectionTableClass&gt;By&lt;Rule1&gt;And&lt;Rule2&gt;
  707. ([Zend_Db_Table_Select $select])</code>
  708. </para>
  709. </listitem>
  710. </itemizedlist>
  711. <para>
  712. In the patterns above,
  713. <code>&lt;TableClass&gt;</code>
  714. and
  715. <code>&lt;IntersectionTableClass&gt;</code>
  716. are strings that correspond to the class
  717. names of the destination table and the
  718. intersection table, respectively.
  719. <code>&lt;Rule1&gt;</code>
  720. and
  721. <code>&lt;Rule2&gt;</code>
  722. are strings that correspond
  723. to the rule keys in the intersection table that reference the
  724. origin table and the
  725. destination table, respectively.
  726. </para>
  727. <note>
  728. <para>
  729. The table identities and the rule keys named in the method call must match the
  730. spelling of the class and rule key exactly.
  731. </para>
  732. </note>
  733. <example id="zend.db.table.relationships.fetching.many-to-many.example-magic">
  734. <title>Fetching Rowsets using the Magic Many-to-many Method</title>
  735. <para>
  736. This example shows finding rows in the destination table of a many-to-many
  737. relationship representing products related to a given bug.
  738. </para>
  739. <programlisting language="php"><![CDATA[
  740. $bugsTable = new Bugs();
  741. $bugsRowset = $bugsTable->find(1234);
  742. $bug1234 = $bugsRowset->current();
  743. // Use the default reference rule
  744. $products = $bug1234->findProductsViaBugsProducts();
  745. // Specify the reference rule
  746. $products = $bug1234->findProductsViaBugsProductsByBug();
  747. ]]></programlisting>
  748. </example>
  749. </sect2>
  750. <sect2 id="zend.db.table.relationships.cascading">
  751. <title>Cascading Write Operations</title>
  752. <note>
  753. <title>Declare DRI in the database:</title>
  754. <para>
  755. Declaring cascading operations in
  756. <classname>Zend_Db_Table</classname>
  757. is intended
  758. <emphasis>only</emphasis>
  759. for
  760. <acronym>RDBMS</acronym>
  761. brands that do not support
  762. declarative referential integrity (DRI).
  763. </para>
  764. <para>
  765. For example, if you use MySQL's MyISAM storage engine, or SQLite, these solutions
  766. do
  767. not support DRI. You may find it helpful to declare the cascading operations
  768. with
  769. <classname>Zend_Db_Table</classname>
  770. .
  771. </para>
  772. <para>
  773. If your
  774. <acronym>RDBMS</acronym>
  775. implements DRI and the
  776. <code>ON DELETE</code>
  777. and
  778. <code>ON UPDATE</code>
  779. clauses, you should declare these clauses in your database
  780. schema, instead of using
  781. the cascading feature in
  782. <classname>Zend_Db_Table</classname>
  783. . Declaring cascading DRI rules in the
  784. <acronym>RDBMS</acronym>
  785. is better for database performance, consistency, and
  786. integrity.
  787. </para>
  788. <para>
  789. Most importantly, do not declare cascading operations both in the
  790. <acronym>RDBMS</acronym>
  791. and in your
  792. <classname>Zend_Db_Table</classname>
  793. class.
  794. </para>
  795. </note>
  796. <para>
  797. You can declare cascading operations to execute against a dependent table when you
  798. apply
  799. an
  800. <constant>UPDATE</constant>
  801. or a
  802. <constant>DELETE</constant>
  803. to a row in a
  804. parent table.
  805. </para>
  806. <example id="zend.db.table.relationships.cascading.example-delete">
  807. <title>Example of a Cascading Delete</title>
  808. <para>
  809. This example shows deleting a row in the
  810. <code>Products</code>
  811. table, which is
  812. configured to automatically delete dependent rows in the
  813. <code>Bugs</code>
  814. table.
  815. </para>
  816. <programlisting language="php"><![CDATA[
  817. $productsTable = new Products();
  818. $productsRowset = $productsTable->find(1234);
  819. $product1234 = $productsRowset->current();
  820. $product1234->delete();
  821. // Automatically cascades to Bugs table
  822. // and deletes dependent rows.
  823. ]]></programlisting>
  824. </example>
  825. <para>
  826. Similarly, if you use
  827. <constant>UPDATE</constant>
  828. to change the value of a primary key
  829. in a parent table, you may want the value in foreign
  830. keys of dependent tables to be
  831. updated automatically to match the new value, so that such
  832. references are kept up to
  833. date.
  834. </para>
  835. <para>
  836. It's usually not necessary to update the value of a primary key that was generated by a
  837. sequence or other mechanism. But if you use a
  838. <emphasis>natural key</emphasis>
  839. that may
  840. change value occasionally, it is more likely that you need to apply cascading
  841. updates
  842. to dependent tables.
  843. </para>
  844. <para>
  845. To declare a cascading relationship in the
  846. <classname>Zend_Db_Table</classname>
  847. , edit
  848. the rules in the
  849. <varname>$_referenceMap</varname>
  850. . Set the associative array keys
  851. <code>'onDelete'</code>
  852. and
  853. <code>'onUpdate'</code>
  854. to the string 'cascade' (or the
  855. constant
  856. <constant>self::CASCADE</constant>
  857. ). Before a row is deleted from the parent
  858. table, or its primary key values updated, any
  859. rows in the dependent table that refer to
  860. the parent's row are deleted or updated first.
  861. </para>
  862. <example id="zend.db.table.relationships.cascading.example-declaration">
  863. <title>Example Declaration of Cascading Operations</title>
  864. <para>
  865. In the example below, rows in the
  866. <code>Bugs</code>
  867. table are automatically deleted
  868. if the row in the
  869. <code>Products</code>
  870. table to which they refer is deleted. The
  871. <code>'onDelete'</code>
  872. element of the reference map entry is set to
  873. <constant>self::CASCADE</constant>
  874. .
  875. </para>
  876. <para>
  877. No cascading update is done in the example below if the primary key value in the
  878. parent class is changed. The
  879. <code>'onUpdate'</code>
  880. element of the reference map
  881. entry is
  882. <constant>self::RESTRICT</constant>
  883. . You can get the same result by
  884. omitting the
  885. <code>'onUpdate'</code>
  886. entry.
  887. </para>
  888. <programlisting language="php"><![CDATA[
  889. class BugsProducts extends Zend_Db_Table_Abstract
  890. {
  891. ...
  892. protected $_referenceMap = array(
  893. 'Product' => array(
  894. 'columns' => array('product_id'),
  895. 'refTableClass' => 'Products',
  896. 'refColumns' => array('product_id'),
  897. 'onDelete' => self::CASCADE,
  898. 'onUpdate' => self::RESTRICT
  899. ),
  900. ...
  901. );
  902. }
  903. ]]></programlisting>
  904. </example>
  905. <sect3 id="zend.db.table.relationships.cascading.notes">
  906. <title>Notes Regarding Cascading Operations</title>
  907. <para>
  908. <emphasis>
  909. Cascading operations invoked by
  910. <classname>Zend_Db_Table</classname>
  911. are
  912. not atomic.
  913. </emphasis>
  914. </para>
  915. <para>
  916. This means that if your database implements and enforces referential integrity
  917. constraints, a cascading
  918. <constant>UPDATE</constant>
  919. executed by a
  920. <classname>Zend_Db_Table</classname>
  921. class conflicts with the constraint, and
  922. results in a referential integrity
  923. violation. You can use cascading
  924. <constant>UPDATE</constant>
  925. in
  926. <classname>Zend_Db_Table</classname>
  927. <emphasis>only</emphasis>
  928. if your database does not enforce that referential
  929. integrity constraint.
  930. </para>
  931. <para>
  932. Cascading
  933. <constant>DELETE</constant>
  934. suffers less from the problem of referential
  935. integrity violations. You can delete
  936. dependent rows as a non-atomic action before
  937. deleting the parent row that they
  938. reference.
  939. </para>
  940. <para>
  941. However, for both
  942. <constant>UPDATE</constant>
  943. and
  944. <constant>DELETE</constant>
  945. ,
  946. changing the database in a non-atomic way also creates the risk that another
  947. database user can see the data in an inconsistent state. For example, if you delete
  948. a row and all its dependent rows, there is a small chance that another database
  949. client program can query the database after you have deleted the dependent rows, but
  950. before you delete the parent row. That client program may see the parent row with no
  951. dependent rows, and assume this is the intended state of the data. There is no way
  952. for that client to know that its query read the database in the middle of a change.
  953. </para>
  954. <para>
  955. The issue of non-atomic change can be mitigated by using transactions to isolate
  956. your change. But some
  957. <acronym>RDBMS</acronym>
  958. brands don't support transactions, or
  959. allow clients to read "dirty" changes that have
  960. not been committed yet.
  961. </para>
  962. <para>
  963. <emphasis>
  964. Cascading operations in
  965. <classname>Zend_Db_Table</classname>
  966. are invoked
  967. only by
  968. <classname>Zend_Db_Table</classname>
  969. .
  970. </emphasis>
  971. </para>
  972. <para>
  973. Cascading deletes and updates defined in your
  974. <classname>Zend_Db_Table</classname>
  975. classes are applied if you execute the
  976. <methodname>save()</methodname>
  977. or
  978. <methodname>delete()</methodname>
  979. methods on the Row class. However, if you update
  980. or delete data using another
  981. interface, such as a query tool or another application,
  982. the cascading operations are
  983. not applied. Even when using
  984. <methodname>update()</methodname>
  985. and
  986. <methodname>delete()</methodname>
  987. methods
  988. in the
  989. <classname>Zend_Db_Adapter</classname>
  990. class, cascading operations defined in
  991. your
  992. <classname>Zend_Db_Table</classname>
  993. classes are not executed.
  994. </para>
  995. <para>
  996. <emphasis>
  997. No Cascading
  998. <constant>INSERT</constant>
  999. .
  1000. </emphasis>
  1001. </para>
  1002. <para>
  1003. There is no support for a cascading
  1004. <constant>INSERT</constant>
  1005. . You must insert a
  1006. row to a parent table in one operation, and insert row(s) to a
  1007. dependent table in a
  1008. separate operation.
  1009. </para>
  1010. </sect3>
  1011. </sect2>
  1012. </sect1>
  1013. <!--
  1014. vim:se ts=4 sw=4 et:
  1015. -->