TestCommon.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Table_TestSetup
  24. */
  25. require_once 'Zend/Db/Table/TestSetup.php';
  26. /**
  27. * @see Zend_Loader
  28. */
  29. require_once 'Zend/Loader.php';
  30. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  31. /**
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Db
  38. * @group Zend_Db_Table
  39. * @group Zend_Db_Table_Relationships
  40. */
  41. abstract class Zend_Db_Table_Relationships_TestCommon extends Zend_Db_Table_TestSetup
  42. {
  43. public function testTableRelationshipFindParentRow()
  44. {
  45. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  46. $account_name = $this->_db->foldCase('account_name');
  47. $table = $this->_table['bugs'];
  48. $childRows = $table->fetchAll("$bug_id = 1");
  49. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  50. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  51. $childRow1 = $childRows->current();
  52. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  53. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  54. $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts');
  55. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  56. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  57. $this->assertEquals('goofy', $parentRow->$account_name);
  58. }
  59. public function testTableRelationshipFindParentRowSelect()
  60. {
  61. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  62. $account_name = $this->_db->foldCase('account_name');
  63. $account_name_column = $this->_db->quoteIdentifier('account_name', true);
  64. $table = $this->_table['bugs'];
  65. $select = $table->select()->where($account_name_column . ' = ?', 'goofy');
  66. $childRows = $table->fetchAll("$bug_id = 1");
  67. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  68. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  69. $childRow1 = $childRows->current();
  70. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  71. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  72. $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts', null, $select);
  73. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  74. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  75. $this->assertEquals('goofy', $parentRow->$account_name);
  76. }
  77. public function testTableRelationshipMagicFindParentRow()
  78. {
  79. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  80. $account_name = $this->_db->foldCase('account_name');
  81. $table = $this->_table['bugs'];
  82. $childRows = $table->fetchAll("$bug_id = 1");
  83. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  84. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  85. $childRow1 = $childRows->current();
  86. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  87. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  88. $parentRow = $childRow1->findParentMy_ZendDbTable_TableAccounts();
  89. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  90. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  91. $this->assertEquals('goofy', $parentRow->$account_name);
  92. }
  93. public function testTableRelationshipMagicFindParentRowSelect()
  94. {
  95. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  96. $account_name = $this->_db->foldCase('account_name');
  97. $account_name_column = $this->_db->quoteIdentifier('account_name', true);
  98. $table = $this->_table['bugs'];
  99. $select = $table->select()->where($account_name_column . ' = ?', 'goofy');
  100. $childRows = $table->fetchAll("$bug_id = 1");
  101. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  102. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  103. $childRow1 = $childRows->current();
  104. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  105. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  106. $parentRow = $childRow1->findParentMy_ZendDbTable_TableAccounts($select);
  107. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  108. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  109. $this->assertEquals('goofy', $parentRow->$account_name);
  110. }
  111. public function testTableRelationshipMagicException()
  112. {
  113. $table = $this->_table['bugs'];
  114. $parentRows = $table->find(1);
  115. $parentRow1 = $parentRows->current();
  116. // Completely bogus method
  117. try {
  118. $result = $parentRow1->nonExistantMethod();
  119. $this->fail('Expected to catch Zend_Db_Table_Row_Exception');
  120. } catch (Zend_Exception $e) {
  121. $this->assertType('Zend_Db_Table_Row_Exception', $e,
  122. 'Expecting object of type Zend_Db_Table_Row_Exception got '.get_class($e));
  123. $this->assertEquals("Unrecognized method 'nonExistantMethod()'", $e->getMessage());
  124. }
  125. }
  126. /**
  127. * @expectedException PHPUnit_Framework_Error
  128. */
  129. public function testTableRelationshipFindParentRowErrorOnBadString()
  130. {
  131. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  132. $table = $this->_table['bugs'];
  133. $childRows = $table->fetchAll("$bug_id = 1");
  134. $childRow1 = $childRows->current();
  135. $parentRow = $childRow1->findParentRow('nonexistant_class');
  136. }
  137. /**
  138. * @expectedException Zend_Db_Table_Exception
  139. */
  140. public function testTableRelationshipFindParentRowExceptionOnBadClass()
  141. {
  142. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  143. $table = $this->_table['bugs'];
  144. $childRows = $table->fetchAll("$bug_id = 1");
  145. $childRow1 = $childRows->current();
  146. $parentRow = $childRow1->findParentRow(new stdClass());
  147. }
  148. public function testTableRelationshipFindManyToManyRowset()
  149. {
  150. $table = $this->_table['bugs'];
  151. $originRows = $table->find(1);
  152. $originRow1 = $originRows->current();
  153. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts');
  154. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  155. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  156. $this->assertEquals(3, $destRows->count());
  157. }
  158. public function testTableRelationshipFindManyToManyRowsetSelect()
  159. {
  160. $product_name = $this->_db->foldCase('product_name');
  161. $bug_id = $this->_db->foldCase('bug_id');
  162. $bug_id_column = $this->_db->quoteIdentifier('bug_id', true);
  163. $table = $this->_table['bugs'];
  164. $select = $table->select()->where($bug_id_column . ' = ?', 1)
  165. ->limit(2)
  166. ->order($product_name . ' ASC');
  167. $originRows = $table->find(1);
  168. $originRow1 = $originRows->current();
  169. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts',
  170. null, null, $select);
  171. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  172. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  173. $this->assertEquals(2, $destRows->count());
  174. $childRow = $destRows->current();
  175. $this->assertEquals('Linux', $childRow->$product_name);
  176. }
  177. public function testTableRelationshipMagicFindManyToManyRowset()
  178. {
  179. $table = $this->_table['bugs'];
  180. $originRows = $table->find(1);
  181. $originRow1 = $originRows->current();
  182. $destRows = $originRow1->findMy_ZendDbTable_TableProductsViaMy_ZendDbTable_TableBugsProducts();
  183. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  184. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  185. $this->assertEquals(3, $destRows->count());
  186. }
  187. public function testTableRelationshipMagicFindManyToManyRowsetSelect()
  188. {
  189. $product_name = $this->_db->foldCase('product_name');
  190. $bug_id = $this->_db->foldCase('bug_id');
  191. $bug_id_column = $this->_db->quoteIdentifier('bug_id', true);
  192. $table = $this->_table['bugs'];
  193. $select = $table->select()->where($bug_id_column . ' = ?', 1)
  194. ->limit(2)
  195. ->order($product_name . ' ASC');
  196. $originRows = $table->find(1);
  197. $originRow1 = $originRows->current();
  198. $destRows = $originRow1->findMy_ZendDbTable_TableProductsViaMy_ZendDbTable_TableBugsProducts($select);
  199. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  200. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  201. $this->assertEquals(2, $destRows->count());
  202. $childRow = $destRows->current();
  203. $this->assertEquals('Linux', $childRow->$product_name);
  204. }
  205. /**
  206. * @expectedException PHPUnit_Framework_Error
  207. */
  208. public function testTableRelationshipFindManyToManyRowsetErrorOnBadClassNameAsString()
  209. {
  210. $table = $this->_table['bugs'];
  211. $originRows = $table->find(1);
  212. $originRow1 = $originRows->current();
  213. // Use nonexistant class for destination table
  214. $destRows = $originRow1->findManyToManyRowset('nonexistant_class', 'My_ZendDbTable_TableBugsProducts');
  215. }
  216. /**
  217. * @expectedException PHPUnit_Framework_Error
  218. */
  219. public function testTableRelationshipFindManyToManyRowsetErrorOnBadClassNameAsStringForIntersection()
  220. {
  221. $table = $this->_table['bugs'];
  222. $originRows = $table->find(1);
  223. $originRow1 = $originRows->current();
  224. // Use nonexistant class for intersection table
  225. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'nonexistant_class');
  226. }
  227. /**
  228. * @expectedException Zend_Db_Table_Exception
  229. */
  230. public function testTableRelationshipFindManyToManyRowsetExceptionOnBadClassAsString()
  231. {
  232. $table = $this->_table['bugs'];
  233. $originRows = $table->find(1);
  234. $originRow1 = $originRows->current();
  235. // Use stdClass instead of table class for destination table
  236. $destRows = $originRow1->findManyToManyRowset(new stdClass(), 'My_ZendDbTable_TableBugsProducts');
  237. }
  238. /**
  239. * @expectedException Zend_Db_Table_Exception
  240. */
  241. public function testTableRelationshipFindManyToManyRowsetExceptionOnBadClassAsStringForIntersection()
  242. {
  243. $table = $this->_table['bugs'];
  244. $originRows = $table->find(1);
  245. $originRow1 = $originRows->current();
  246. // Use stdClass instead of table class for intersection table
  247. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', new stdClass());
  248. }
  249. public function testTableRelationshipFindDependentRowset()
  250. {
  251. $table = $this->_table['bugs'];
  252. $bug_id = $this->_db->foldCase('bug_id');
  253. $product_id = $this->_db->foldCase('product_id');
  254. $parentRows = $table->find(1);
  255. $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
  256. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
  257. $parentRow1 = $parentRows->current();
  258. $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugsProducts');
  259. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  260. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  261. $this->assertEquals(3, $childRows->count());
  262. $childRow1 = $childRows->current();
  263. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  264. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  265. $this->assertEquals(1, $childRow1->$bug_id);
  266. $this->assertEquals(1, $childRow1->$product_id);
  267. }
  268. public function testTableRelationshipFindDependentRowsetSelect()
  269. {
  270. $table = $this->_table['bugs'];
  271. $bug_id = $this->_db->foldCase('bug_id');
  272. $product_id = $this->_db->foldCase('product_id');
  273. $select = $table->select()->limit(2)
  274. ->order($product_id . ' DESC');
  275. $parentRows = $table->find(1);
  276. $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
  277. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
  278. $parentRow1 = $parentRows->current();
  279. $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugsProducts', null, $select);
  280. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  281. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  282. $childRow1 = $childRows->current();
  283. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  284. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  285. $this->assertEquals(1, $childRow1->$bug_id);
  286. $this->assertEquals(3, $childRow1->$product_id);
  287. }
  288. public function testTableRelationshipMagicFindDependentRowset()
  289. {
  290. $table = $this->_table['bugs'];
  291. $bug_id = $this->_db->foldCase('bug_id');
  292. $product_id = $this->_db->foldCase('product_id');
  293. $parentRows = $table->find(1);
  294. $parentRow1 = $parentRows->current();
  295. $childRows = $parentRow1->findMy_ZendDbTable_TableBugsProducts();
  296. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  297. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  298. $this->assertEquals(3, $childRows->count());
  299. $childRow1 = $childRows->current();
  300. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  301. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  302. $this->assertEquals(1, $childRow1->$bug_id);
  303. $this->assertEquals(1, $childRow1->$product_id);
  304. }
  305. public function testTableRelationshipMagicFindDependentRowsetSelect()
  306. {
  307. $table = $this->_table['bugs'];
  308. $bug_id = $this->_db->foldCase('bug_id');
  309. $product_id = $this->_db->foldCase('product_id');
  310. $select = $table->select()->limit(2)
  311. ->order($product_id . ' DESC');
  312. $parentRows = $table->find(1);
  313. $parentRow1 = $parentRows->current();
  314. $childRows = $parentRow1->findMy_ZendDbTable_TableBugsProducts($select);
  315. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  316. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  317. $this->assertEquals(2, $childRows->count());
  318. $childRow1 = $childRows->current();
  319. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  320. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  321. $this->assertEquals(1, $childRow1->$bug_id);
  322. $this->assertEquals(3, $childRow1->$product_id);
  323. }
  324. /**
  325. * @expectedException PHPUnit_Framework_Error
  326. */
  327. public function testTableRelationshipFindDependentRowsetPhpError()
  328. {
  329. $table = $this->_table['bugs'];
  330. $parentRows = $table->find(1);
  331. $parentRow1 = $parentRows->current();
  332. $childRows = $parentRow1->findDependentRowset('nonexistant_class');
  333. }
  334. /**
  335. * Ensures that basic cascading update functionality succeeds using strings for single columns
  336. *
  337. * @return void
  338. */
  339. public function testTableRelationshipCascadingUpdateUsageBasicString()
  340. {
  341. $bug = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  342. ->find(1)
  343. ->current();
  344. $bug_id = $this->_db->foldCase('bug_id');
  345. $this->assertEquals(
  346. 3,
  347. count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  348. 'Expecting to find three dependent rows'
  349. );
  350. $bug->$bug_id = 333;
  351. $bug->save();
  352. $this->assertEquals(
  353. 3,
  354. count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  355. 'Expecting to find three dependent rows'
  356. );
  357. foreach ($bugProducts as $bugProduct) {
  358. $this->assertEquals(333, $bugProduct->$bug_id);
  359. }
  360. $bug->$bug_id = 1;
  361. $bug->save();
  362. $this->assertEquals(
  363. 3,
  364. count($bugProducts = $bug->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  365. 'Expecting to find three dependent rows'
  366. );
  367. foreach ($bugProducts as $bugProduct) {
  368. $this->assertEquals(1, $bugProduct->$bug_id);
  369. }
  370. }
  371. /**
  372. * Ensures that basic cascading update functionality succeeds using arrays for single columns
  373. *
  374. * @return void
  375. */
  376. public function testTableRelationshipCascadingUpdateUsageBasicArray()
  377. {
  378. $account1 = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
  379. ->find('mmouse')
  380. ->current();
  381. $account_name = $this->_db->foldCase('account_name');
  382. $reported_by = $this->_db->foldCase('reported_by');
  383. $this->assertEquals(
  384. 1,
  385. count($account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  386. 'Expecting to find one dependent row'
  387. );
  388. $account1->$account_name = 'daisy';
  389. $account1->save();
  390. $this->assertEquals(
  391. 1,
  392. count($account1Bugs = $account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  393. 'Expecting to find one dependent row'
  394. );
  395. foreach ($account1Bugs as $account1Bug) {
  396. $this->assertEquals('daisy', $account1Bug->$reported_by);
  397. }
  398. $account1->$account_name = 'mmouse';
  399. $account1->save();
  400. $this->assertEquals(
  401. 1,
  402. count($account1Bugs = $account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  403. 'Expecting to find one dependent row'
  404. );
  405. foreach ($account1Bugs as $account1Bug) {
  406. $this->assertEquals('mmouse', $account1Bug->$reported_by);
  407. }
  408. }
  409. /**
  410. * Ensures that cascading update functionality is not run when onUpdate != self::CASCADE
  411. *
  412. * @return void
  413. */
  414. public function testTableRelationshipCascadingUpdateUsageInvalidNoop()
  415. {
  416. $product1 = $this->_getTable('My_ZendDbTable_TableProductsCustom')
  417. ->find(1)
  418. ->current();
  419. $this->assertEquals(
  420. 1,
  421. count($product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  422. 'Expecting to find one dependent row'
  423. );
  424. $product_id = $this->_db->foldCase('product_id');
  425. $product1->$product_id = 333;
  426. $product1->save();
  427. $this->assertEquals(
  428. 0,
  429. count($product1BugsProducts = $product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  430. 'Expecting to find one dependent row'
  431. );
  432. $product1->$product_id = 1;
  433. $product1->save();
  434. $this->assertEquals(
  435. 1,
  436. count($product1BugsProducts = $product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  437. 'Expecting to find one dependent row'
  438. );
  439. foreach ($product1BugsProducts as $product1BugsProduct) {
  440. $this->assertEquals(1, $product1BugsProduct->$product_id);
  441. }
  442. }
  443. /**
  444. * Ensures that basic cascading delete functionality succeeds using strings for single columns
  445. *
  446. * @return void
  447. */
  448. public function testTableRelationshipCascadingDeleteUsageBasicString()
  449. {
  450. $bug1 = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  451. ->find(1)
  452. ->current();
  453. $this->assertEquals(
  454. 3,
  455. count($bug1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  456. 'Expecting to find three dependent rows'
  457. );
  458. $bug1->delete();
  459. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  460. $this->assertEquals(
  461. 0,
  462. count($this->_getTable('My_ZendDbTable_TableBugsProductsCustom')->fetchAll("$bug_id = 1")),
  463. 'Expecting cascading delete to have reduced dependent rows to zero'
  464. );
  465. }
  466. /**
  467. * Ensures that basic cascading delete functionality succeeds using arrays for single columns
  468. *
  469. * @return void
  470. */
  471. public function testTableRelationshipCascadingDeleteUsageBasicArray()
  472. {
  473. $reported_by = $this->_db->quoteIdentifier('reported_by', true);
  474. $account1 = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
  475. ->find('mmouse')
  476. ->current();
  477. $this->assertEquals(
  478. 1,
  479. count($account1->findDependentRowset('My_ZendDbTable_TableBugsCustom')),
  480. 'Expecting to find one dependent row'
  481. );
  482. $account1->delete();
  483. $tableBugsCustom = $this->_getTable('My_ZendDbTable_TableBugsCustom');
  484. $this->assertEquals(
  485. 0,
  486. count(
  487. $tableBugsCustom->fetchAll(
  488. $tableBugsCustom->getAdapter()
  489. ->quoteInto("$reported_by = ?", 'mmouse')
  490. )
  491. ),
  492. 'Expecting cascading delete to have reduced dependent rows to zero'
  493. );
  494. }
  495. /**
  496. * Ensures that cascading delete functionality is not run when onDelete != self::CASCADE
  497. *
  498. * @return void
  499. */
  500. public function testTableRelationshipCascadingDeleteUsageInvalidNoop()
  501. {
  502. $product1 = $this->_getTable('My_ZendDbTable_TableProductsCustom')
  503. ->find(1)
  504. ->current();
  505. $this->assertEquals(
  506. 1,
  507. count($product1->findDependentRowset('My_ZendDbTable_TableBugsProductsCustom')),
  508. 'Expecting to find one dependent row'
  509. );
  510. $product1->delete();
  511. $product_id = $this->_db->quoteIdentifier('product_id', true);
  512. $this->assertEquals(
  513. 1,
  514. count($this->_getTable('My_ZendDbTable_TableBugsProductsCustom')->fetchAll("$product_id = 1")),
  515. 'Expecting to find one dependent row'
  516. );
  517. }
  518. public function testTableRelationshipGetReference()
  519. {
  520. $table = $this->_table['bugs'];
  521. $map = $table->getReference('My_ZendDbTable_TableAccounts', 'Reporter');
  522. $this->assertThat($map, $this->arrayHasKey('columns'));
  523. $this->assertThat($map, $this->arrayHasKey('refTableClass'));
  524. $this->assertThat($map, $this->arrayHasKey('refColumns'));
  525. }
  526. public function testTableRelationshipGetReferenceException()
  527. {
  528. $table = $this->_table['bugs'];
  529. try {
  530. $table->getReference('My_ZendDbTable_TableAccounts', 'Nonexistent');
  531. $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent reference rule');
  532. } catch (Zend_Exception $e) {
  533. $this->assertType('Zend_Db_Table_Exception', $e,
  534. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  535. }
  536. try {
  537. $table->getReference('Nonexistent', 'Reporter');
  538. $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent rule tableClass');
  539. } catch (Zend_Exception $e) {
  540. $this->assertType('Zend_Db_Table_Exception', $e,
  541. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  542. }
  543. try {
  544. $table->getReference('Nonexistent');
  545. $this->fail('Expected to catch Zend_Db_Table_Exception for nonexistent rule tableClass');
  546. } catch (Zend_Exception $e) {
  547. $this->assertType('Zend_Db_Table_Exception', $e,
  548. 'Expecting object of type Zend_Db_Table_Exception got '.get_class($e));
  549. }
  550. }
  551. /**
  552. * Ensures that findParentRow() returns an instance of a custom row class when passed an instance
  553. * of the table class having $_rowClass overridden.
  554. *
  555. * @return void
  556. */
  557. public function testTableRelationshipFindParentRowCustomInstance()
  558. {
  559. $this->_useMyIncludePath();
  560. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  561. Zend_Loader::loadClass($myRowClass);
  562. $bug1Reporter = $this->_table['bugs']
  563. ->find(1)
  564. ->current()
  565. ->findParentRow($this->_table['accounts']->setRowClass($myRowClass));
  566. $this->assertType($myRowClass, $bug1Reporter,
  567. "Expecting object of type $myRowClass, got ".get_class($bug1Reporter));
  568. }
  569. /**
  570. * Ensures that findParentRow() returns an instance of a custom row class when passed a string class
  571. * name, where the class has $_rowClass overridden.
  572. *
  573. * @return void
  574. */
  575. public function testTableRelationshipFindParentRowCustomClass()
  576. {
  577. $this->_useMyIncludePath();
  578. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  579. Zend_Loader::loadClass($myRowClass);
  580. Zend_Loader::loadClass('My_ZendDbTable_TableAccountsCustom');
  581. $bug1Reporter = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  582. ->find(1)
  583. ->current()
  584. ->findParentRow(new My_ZendDbTable_TableAccountsCustom(array('db' => $this->_db)));
  585. $this->assertType($myRowClass, $bug1Reporter,
  586. "Expecting object of type $myRowClass, got ".get_class($bug1Reporter));
  587. }
  588. /**
  589. * Ensures that findDependentRowset() returns instances of custom row and rowset classes when
  590. * passed an instance of the table class.
  591. *
  592. * @return void
  593. */
  594. public function testTableRelationshipFindDependentRowsetCustomInstance()
  595. {
  596. $this->_useMyIncludePath();
  597. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  598. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  599. Zend_Loader::loadClass($myRowsetClass);
  600. $account_name = $this->_db->quoteIdentifier('account_name', true);
  601. $bugs = $this->_table['accounts']
  602. ->fetchRow($this->_db->quoteInto("$account_name = ?", 'mmouse'))
  603. ->findDependentRowset(
  604. $this->_table['bugs']
  605. ->setRowsetClass($myRowsetClass)
  606. ->setRowClass($myRowClass),
  607. 'Engineer'
  608. );
  609. $this->assertType($myRowsetClass, $bugs,
  610. "Expecting object of type $myRowsetClass, got ".get_class($bugs));
  611. $this->assertEquals(3, count($bugs));
  612. foreach ($bugs as $bug) {
  613. $this->assertType($myRowClass, $bug,
  614. "Expecting object of type $myRowClass, got ".get_class($bug));
  615. }
  616. }
  617. /**
  618. * Ensures that findDependentRowset() returns instances of custom row and rowset classes when
  619. * passed the named class.
  620. *
  621. * @return void
  622. */
  623. public function testTableRelationshipFindDependentRowsetCustomClass()
  624. {
  625. $this->_useMyIncludePath();
  626. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  627. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  628. Zend_Loader::loadClass($myRowsetClass);
  629. $account_name = $this->_db->quoteIdentifier('account_name', true);
  630. $bugs = $this->_getTable('My_ZendDbTable_TableAccountsCustom')
  631. ->fetchRow($this->_db->quoteInto("$account_name = ?", 'mmouse'))
  632. ->findDependentRowset('My_ZendDbTable_TableBugsCustom', 'Engineer');
  633. $this->assertType($myRowsetClass, $bugs,
  634. "Expecting object of type $myRowsetClass, got ".get_class($bugs));
  635. $this->assertEquals(3, count($bugs));
  636. foreach ($bugs as $bug) {
  637. $this->assertType($myRowClass, $bug,
  638. "Expecting object of type $myRowClass, got ".get_class($bug));
  639. }
  640. }
  641. /**
  642. * Ensures that findManyToManyRowset() returns instances of custom row and rowset class when
  643. * passed an instance of the table class.
  644. *
  645. * @return void
  646. */
  647. public function testTableRelationshipFindManyToManyRowsetCustomInstance()
  648. {
  649. $this->_useMyIncludePath();
  650. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  651. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  652. Zend_Loader::loadClass($myRowsetClass);
  653. $bug1Products = $this->_table['bugs']
  654. ->find(1)
  655. ->current()
  656. ->findManyToManyRowset(
  657. $this->_table['products']
  658. ->setRowsetClass($myRowsetClass)
  659. ->setRowClass($myRowClass),
  660. 'My_ZendDbTable_TableBugsProducts'
  661. );
  662. $this->assertType($myRowsetClass, $bug1Products,
  663. "Expecting object of type $myRowsetClass, got ".get_class($bug1Products));
  664. $this->assertEquals(3, count($bug1Products));
  665. foreach ($bug1Products as $bug1Product) {
  666. $this->assertType($myRowClass, $bug1Product,
  667. "Expecting object of type $myRowClass, got ".get_class($bug1Product));
  668. }
  669. }
  670. /**
  671. * Ensures that findManyToManyRowset() returns instances of custom row and rowset classes when
  672. * passed the named class.
  673. *
  674. * @return void
  675. */
  676. public function testTableRelationshipFindManyToManyRowsetCustomClass()
  677. {
  678. $this->_useMyIncludePath();
  679. $myRowsetClass = 'My_ZendDbTable_Rowset_TestMyRowset';
  680. $myRowClass = 'My_ZendDbTable_Row_TestMyRow';
  681. Zend_Loader::loadClass($myRowsetClass);
  682. $bug1Products = $this->_getTable('My_ZendDbTable_TableBugsCustom')
  683. ->find(1)
  684. ->current()
  685. ->findManyToManyRowset(
  686. 'My_ZendDbTable_TableProductsCustom',
  687. 'My_ZendDbTable_TableBugsProductsCustom'
  688. );
  689. $this->assertType($myRowsetClass, $bug1Products,
  690. "Expecting object of type $myRowsetClass, got ".get_class($bug1Products));
  691. $this->assertEquals(3, count($bug1Products));
  692. foreach ($bug1Products as $bug1Product) {
  693. $this->assertType($myRowClass, $bug1Product,
  694. "Expecting object of type $myRowClass, got ".get_class($bug1Product));
  695. }
  696. }
  697. /**
  698. * Ensures that rows returned by findParentRow() are updatable.
  699. *
  700. * @return void
  701. */
  702. public function testTableRelationshipFindParentRowIsUpdateable()
  703. {
  704. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  705. $account_name = $this->_db->foldCase('account_name');
  706. $table = $this->_table['bugs'];
  707. $childRows = $table->fetchAll("$bug_id = 1");
  708. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  709. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  710. $childRow1 = $childRows->current();
  711. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  712. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  713. $parentRow = $childRow1->findParentRow('My_ZendDbTable_TableAccounts');
  714. $this->assertType('Zend_Db_Table_Row_Abstract', $parentRow,
  715. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($parentRow));
  716. $this->assertEquals('goofy', $parentRow->$account_name);
  717. $parentRow->$account_name = 'clarabell';
  718. try {
  719. $parentRow->save();
  720. } catch (Zend_Exception $e) {
  721. $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
  722. }
  723. $accounts = $this->_db->quoteIdentifier('zfaccounts', true);
  724. $account_name = $this->_db->quoteIdentifier('account_name', true);
  725. $accounts_list = $this->_db->fetchCol("SELECT $account_name from $accounts ORDER BY $account_name");
  726. // if the save() did an UPDATE instead of an INSERT, then goofy should
  727. // be missing, and clarabell should be present
  728. $this->assertEquals(array('clarabell', 'dduck', 'mmouse'), $accounts_list);
  729. }
  730. /**
  731. * Ensures that rows returned by findDependentRowset() are updatable.
  732. *
  733. * @return void
  734. */
  735. public function testTableRelationshipFindDependentRowsetIsUpdateable()
  736. {
  737. $table = $this->_table['accounts'];
  738. $bug_id_column = $this->_db->foldCase('bug_id');
  739. $bug_description = $this->_db->foldCase('bug_description');
  740. $parentRows = $table->find('mmouse');
  741. $this->assertType('Zend_Db_Table_Rowset_Abstract', $parentRows,
  742. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($parentRows));
  743. $parentRow1 = $parentRows->current();
  744. $childRows = $parentRow1->findDependentRowset('My_ZendDbTable_TableBugs');
  745. $this->assertType('Zend_Db_Table_Rowset_Abstract', $childRows,
  746. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($childRows));
  747. $this->assertEquals(1, $childRows->count());
  748. $childRow1 = $childRows->current();
  749. $this->assertType('Zend_Db_Table_Row_Abstract', $childRow1,
  750. 'Expecting object of type Zend_Db_Table_Row_Abstract, got '.get_class($childRow1));
  751. $childRow1->$bug_description = 'Updated description';
  752. $bug_id = $childRow1->$bug_id_column;
  753. try {
  754. $childRow1->save();
  755. } catch (Zend_Exception $e) {
  756. $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
  757. }
  758. // find the row we just updated and make sure it has the new value.
  759. $bugs_table = $this->_table['bugs'];
  760. $bugs_rows = $bugs_table->find($bug_id);
  761. $this->assertEquals(1, $bugs_rows->count());
  762. $bug1 = $bugs_rows->current();
  763. $this->assertEquals($bug_id, $bug1->$bug_id_column);
  764. $this->assertEquals('Updated description', $bug1->$bug_description);
  765. }
  766. /**
  767. * Ensures that rows returned by findManyToManyRowset() are updatable.
  768. *
  769. * @return void
  770. */
  771. public function testTableRelationshipFindManyToManyRowsetIsUpdateable()
  772. {
  773. $table = $this->_table['bugs'];
  774. $product_id_column = $this->_db->foldCase('product_id');
  775. $product_name = $this->_db->foldCase('product_name');
  776. $originRows = $table->find(1);
  777. $originRow1 = $originRows->current();
  778. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableProducts', 'My_ZendDbTable_TableBugsProducts');
  779. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  780. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  781. $this->assertEquals(3, $destRows->count());
  782. $row1 = $destRows->current();
  783. $product_id = $row1->$product_id_column;
  784. $row1->$product_name = 'AmigaOS';
  785. try {
  786. $row1->save();
  787. } catch (Zend_Exception $e) {
  788. $this->fail('Failed with unexpected '.get_class($e).': '.$e->getMessage());
  789. }
  790. // find the row we just updated and make sure it has the new value.
  791. $products_table = $this->_table['products'];
  792. $product_rows = $products_table->find($product_id);
  793. $this->assertEquals(1, $product_rows->count());
  794. $product_row = $product_rows->current();
  795. $this->assertEquals($product_id, $product_row->$product_id_column);
  796. $this->assertEquals('AmigaOS', $product_row->$product_name);
  797. }
  798. public function testTableRelationshipOmitRefColumns()
  799. {
  800. $refMap = array(
  801. 'Reporter' => array(
  802. 'columns' => array('reported_by'),
  803. 'refTableClass' => 'My_ZendDbTable_TableAccounts'
  804. )
  805. );
  806. $table = $this->_getTable('My_ZendDbTable_TableSpecial',
  807. array(
  808. 'name' => 'zfbugs',
  809. 'referenceMap' => $refMap
  810. )
  811. );
  812. $bug1 = $table->find(1)->current();
  813. $reporter = $bug1->findParentRow('My_ZendDbTable_TableAccounts');
  814. $this->assertEquals(array('account_name' => 'goofy'), $reporter->toArray());
  815. }
  816. /**
  817. * Test that findParentRow() works even if the column names are
  818. * not the same.
  819. */
  820. public function testTableRelationshipFindParentRowWithDissimilarColumns()
  821. {
  822. $bug_id = $this->_db->foldCase('bug_id');
  823. $product_id = $this->_db->foldCase('product_id');
  824. $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
  825. $intRow = $intersectionTable->find(2, 3)->current();
  826. $bugRow = $intRow->findParentRow('My_ZendDbTable_TableBugs');
  827. $this->assertEquals(2, $bugRow->$bug_id);
  828. $productRow = $intRow->findParentRow('My_ZendDbTable_TableProducts');
  829. $this->assertEquals(3, $productRow->$product_id);
  830. }
  831. /**
  832. * Test that findDependentRowset() works even if the column names are
  833. * not the same.
  834. */
  835. public function testTableRelationshipFindDependentRowsetWithDissimilarColumns()
  836. {
  837. $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
  838. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
  839. $bugRow = $bugsTable->find(2)->current();
  840. $intRows = $bugRow->findDependentRowset($intersectionTable);
  841. $this->assertEquals(array(2, 3), array_values($intRows->current()->toArray()));
  842. }
  843. /**
  844. * Test that findManyToManyRowset() works even if the column names are
  845. * not the same.
  846. */
  847. public function testTableRelationshipFindManyToManyRowsetWithDissimilarColumns()
  848. {
  849. $product_id = $this->_db->foldCase('product_id');
  850. $intersectionTable = $this->_getBugsProductsWithDissimilarColumns();
  851. $bugsTable = $this->_getTable('My_ZendDbTable_TableBugs');
  852. $bugRow = $bugsTable->find(2)->current();
  853. $productRows = $bugRow->findManyToManyRowset('My_ZendDbTable_TableProducts', $intersectionTable);
  854. $this->assertEquals(3, $productRows->current()->$product_id);
  855. }
  856. /**
  857. * Test that findManyToManyRowset() works even if the column types are
  858. * not the same.
  859. */
  860. public function testTableRelationshipFindManyToManyRowsetWithDissimilarTypes()
  861. {
  862. $table = $this->_table['products'];
  863. $originRows = $table->find(1);
  864. $originRow1 = $originRows->current();
  865. $destRows = $originRow1->findManyToManyRowset('My_ZendDbTable_TableBugs', 'My_ZendDbTable_TableBugsProducts');
  866. $this->assertType('Zend_Db_Table_Rowset_Abstract', $destRows,
  867. 'Expecting object of type Zend_Db_Table_Rowset_Abstract, got '.get_class($destRows));
  868. $this->assertEquals(1, $destRows->count());
  869. }
  870. /**
  871. * @group ZF-3486
  872. */
  873. public function testTableRelationshipCanFindParentViaConcreteInstantiation()
  874. {
  875. Zend_Db_Table::setDefaultAdapter($this->_db);
  876. $definition = $this->_getTableDefinition();
  877. $bugsTable = new Zend_Db_Table('Bugs', $definition);
  878. $rowset = $bugsTable->find(1);
  879. $row = $rowset->current();
  880. $parent = $row->findParentRow('Accounts', 'Engineer');
  881. $this->assertEquals('mmouse', $parent->account_name);
  882. Zend_Db_Table::setDefaultAdapter();
  883. }
  884. /**
  885. * @group ZF-3486
  886. */
  887. public function testTableRelationshipCanFindDependentRowsetViaConcreteInstantiation()
  888. {
  889. Zend_Db_Table::setDefaultAdapter($this->_db);
  890. $definition = $this->_getTableDefinition();
  891. $productsTable = new Zend_Db_Table('Products', $definition);
  892. $productsRowset = $productsTable->find(1);
  893. $productRow = $productsRowset->current();
  894. $this->assertEquals('Windows', $productRow->product_name);
  895. $this->assertEquals(1, count($productRow->findDependentRowset('BugsProducts', 'Product')));
  896. $bugsProductRow = $productRow->findDependentRowset('BugsProducts', 'Product')->current();
  897. $this->assertEquals(1, $bugsProductRow->product_id);
  898. $this->assertEquals(1, $bugsProductRow->bug_id);
  899. Zend_Db_Table::setDefaultAdapter();
  900. }
  901. /**
  902. * @group ZF-3486
  903. */
  904. public function testTableRelationshipCanFindManyToManyRowsetViaConcreteInstantiation()
  905. {
  906. Zend_Db_Table::setDefaultAdapter($this->_db);
  907. $definition = $this->_getTableDefinition();
  908. $bugsTable = new Zend_Db_Table('Bugs', $definition);
  909. $bugsRowset = $bugsTable->find(1);
  910. $bugRow = $bugsRowset->current();
  911. $m2mRowset = $bugRow->findManyToManyRowset('Products', 'BugsProducts');
  912. $this->assertEquals(3, $m2mRowset->count());
  913. }
  914. /**
  915. * Utility Methods Below
  916. *
  917. */
  918. /**
  919. * _getTableDefinition()
  920. *
  921. * @return Zend_Db_Table_Definition
  922. */
  923. protected function _getTableDefinition()
  924. {
  925. $definition = array(
  926. 'Bugs' => array(
  927. 'name' => 'zfbugs',
  928. 'referenceMap' => array(
  929. 'Reporter' => array(
  930. 'columns' => 'reported_by',
  931. 'refTableClass' => 'Accounts',
  932. 'refColumns' => 'account_name'
  933. ),
  934. 'Engineer' => array(
  935. 'columns' => 'assigned_to',
  936. 'refTableClass' => 'Accounts',
  937. 'refColumns' => 'account_name'
  938. ),
  939. 'Verifier' => array(
  940. 'columns' => 'verified_by',
  941. 'refTableClass' => 'Accounts',
  942. 'refColumns' => 'account_name'
  943. )
  944. )
  945. ),
  946. 'Accounts' => array(
  947. 'name' => 'zfaccounts'
  948. ),
  949. 'BugsProducts' => array(
  950. 'name' => 'zfbugs_products',
  951. 'referenceMap' => array(
  952. 'Bug' => array(
  953. 'columns' => 'bug_id', // Deliberate non-array value
  954. 'refTableClass' => 'Bugs',
  955. 'refColumns' => 'bug_id'
  956. ),
  957. 'Product' => array(
  958. 'columns' => 'product_id',
  959. 'refTableClass' => 'Products',
  960. 'refColumns' => 'product_id',
  961. 'onDelete' => Zend_Db_Table::CASCADE,
  962. 'onUpdate' => Zend_Db_Table::CASCADE
  963. )
  964. )
  965. ),
  966. 'Products' => array(
  967. 'name' => 'zfproducts'
  968. )
  969. );
  970. return new Zend_Db_Table_Definition($definition);
  971. }
  972. /**
  973. * Create database table based on BUGS_PRODUCTS bug with alternative
  974. * spellings of column names. Then create a Table class for this
  975. * physical table and return it.
  976. */
  977. protected function _getBugsProductsWithDissimilarColumns()
  978. {
  979. $altCols = array(
  980. 'boog_id' => 'INTEGER NOT NULL',
  981. 'produck_id' => 'INTEGER NOT NULL',
  982. 'PRIMARY KEY' => 'boog_id,produck_id'
  983. );
  984. $this->_util->createTable('AltBugsProducts', $altCols);
  985. $altBugsProducts = $this->_db->quoteIdentifier($this->_db->foldCase('zfalt_bugs_products'), true);
  986. $bugsProducts = $this->_db->quoteIdentifier($this->_db->foldCase('zfbugs_products'), true);
  987. $this->_db->query("INSERT INTO $altBugsProducts SELECT * FROM $bugsProducts");
  988. $refMap = array(
  989. 'Boog' => array(
  990. 'columns' => array('boog_id'),
  991. 'refTableClass' => 'My_ZendDbTable_TableBugs',
  992. 'refColumns' => array('bug_id')
  993. ),
  994. 'Produck' => array(
  995. 'columns' => array('produck_id'),
  996. 'refTableClass' => 'My_ZendDbTable_TableProducts',
  997. 'refColumns' => array('product_id')
  998. )
  999. );
  1000. $options = array('name' => 'zfalt_bugs_products', 'referenceMap' => $refMap);
  1001. $table = $this->_getTable('My_ZendDbTable_TableSpecial', $options);
  1002. return $table;
  1003. }
  1004. }