TestCommon.php 45 KB

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