PageRenderTime 91ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/Model/ModelReadTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 7691 lines | 7005 code | 347 blank | 339 comment | 15 complexity | 699f144506177b0ef2a1165e161e160c MD5 | raw file
  1. <?php
  2. /**
  3. * ModelReadTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. require_once dirname(__FILE__) . DS . 'ModelTestBase.php';
  20. /**
  21. * ModelReadTest
  22. *
  23. * @package Cake.Test.Case.Model
  24. */
  25. class ModelReadTest extends BaseModelTest {
  26. /**
  27. * testFetchingNonUniqueFKJoinTableRecords()
  28. *
  29. * Tests if the results are properly returned in the case there are non-unique FK's
  30. * in the join table but another fields value is different. For example:
  31. * something_id | something_else_id | doomed = 1
  32. * something_id | something_else_id | doomed = 0
  33. * Should return both records and not just one.
  34. *
  35. * @return void
  36. */
  37. public function testFetchingNonUniqueFKJoinTableRecords() {
  38. $this->loadFixtures('Something', 'SomethingElse', 'JoinThing');
  39. $Something = new Something();
  40. $joinThingData = array(
  41. 'JoinThing' => array(
  42. 'something_id' => 1,
  43. 'something_else_id' => 2,
  44. 'doomed' => '0',
  45. 'created' => '2007-03-18 10:39:23',
  46. 'updated' => '2007-03-18 10:41:31'
  47. )
  48. );
  49. $Something->JoinThing->create($joinThingData);
  50. $Something->JoinThing->save();
  51. $result = $Something->JoinThing->find('all', array('conditions' => array('something_else_id' => 2)));
  52. $this->assertEquals($result[0]['JoinThing']['doomed'], true);
  53. $this->assertEquals($result[1]['JoinThing']['doomed'], false);
  54. $result = $Something->find('first');
  55. $this->assertEquals(count($result['SomethingElse']), 2);
  56. $doomed = Set::extract('/JoinThing/doomed', $result['SomethingElse']);
  57. $this->assertTrue(in_array(true, $doomed));
  58. $this->assertTrue(in_array(false, $doomed));
  59. }
  60. /**
  61. * testGroupBy method
  62. *
  63. * These tests will never pass with Postgres or Oracle as all fields in a select must be
  64. * part of an aggregate function or in the GROUP BY statement.
  65. *
  66. * @return void
  67. */
  68. public function testGroupBy() {
  69. $db = ConnectionManager::getDataSource('test');
  70. $isStrictGroupBy = $this->db instanceof Postgres || $this->db instanceof Sqlite || $this->db instanceof Oracle || $this->db instanceof Sqlserver;
  71. $message = 'Postgres, Oracle, SQLite and SQL Server have strict GROUP BY and are incompatible with this test.';
  72. $this->skipIf($isStrictGroupBy, $message);
  73. $this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid');
  74. $Thread = new Thread();
  75. $Product = new Product();
  76. $result = $Thread->find('all', array(
  77. 'group' => 'Thread.project_id',
  78. 'order' => 'Thread.id ASC'
  79. ));
  80. $expected = array(
  81. array(
  82. 'Thread' => array(
  83. 'id' => 1,
  84. 'project_id' => 1,
  85. 'name' => 'Project 1, Thread 1'
  86. ),
  87. 'Project' => array(
  88. 'id' => 1,
  89. 'name' => 'Project 1'
  90. ),
  91. 'Message' => array(
  92. array(
  93. 'id' => 1,
  94. 'thread_id' => 1,
  95. 'name' => 'Thread 1, Message 1'
  96. ))),
  97. array(
  98. 'Thread' => array(
  99. 'id' => 3,
  100. 'project_id' => 2,
  101. 'name' => 'Project 2, Thread 1'
  102. ),
  103. 'Project' => array(
  104. 'id' => 2,
  105. 'name' => 'Project 2'
  106. ),
  107. 'Message' => array(
  108. array(
  109. 'id' => 3,
  110. 'thread_id' => 3,
  111. 'name' => 'Thread 3, Message 1'
  112. ))));
  113. $this->assertEquals($expected, $result);
  114. $rows = $Thread->find('all', array(
  115. 'group' => 'Thread.project_id',
  116. 'fields' => array('Thread.project_id', 'COUNT(*) AS total')
  117. ));
  118. $result = array();
  119. foreach ($rows as $row) {
  120. $result[$row['Thread']['project_id']] = $row[0]['total'];
  121. }
  122. $expected = array(
  123. 1 => 2,
  124. 2 => 1
  125. );
  126. $this->assertEquals($expected, $result);
  127. $rows = $Thread->find('all', array(
  128. 'group' => 'Thread.project_id',
  129. 'fields' => array('Thread.project_id', 'COUNT(*) AS total'),
  130. 'order' => 'Thread.project_id'
  131. ));
  132. $result = array();
  133. foreach ($rows as $row) {
  134. $result[$row['Thread']['project_id']] = $row[0]['total'];
  135. }
  136. $expected = array(
  137. 1 => 2,
  138. 2 => 1
  139. );
  140. $this->assertEquals($expected, $result);
  141. $result = $Thread->find('all', array(
  142. 'conditions' => array('Thread.project_id' => 1),
  143. 'group' => 'Thread.project_id'
  144. ));
  145. $expected = array(
  146. array(
  147. 'Thread' => array(
  148. 'id' => 1,
  149. 'project_id' => 1,
  150. 'name' => 'Project 1, Thread 1'
  151. ),
  152. 'Project' => array(
  153. 'id' => 1,
  154. 'name' => 'Project 1'
  155. ),
  156. 'Message' => array(
  157. array(
  158. 'id' => 1,
  159. 'thread_id' => 1,
  160. 'name' => 'Thread 1, Message 1'
  161. ))));
  162. $this->assertEquals($expected, $result);
  163. $result = $Thread->find('all', array(
  164. 'conditions' => array('Thread.project_id' => 1),
  165. 'group' => 'Thread.project_id, Project.id'
  166. ));
  167. $this->assertEquals($expected, $result);
  168. $result = $Thread->find('all', array(
  169. 'conditions' => array('Thread.project_id' => 1),
  170. 'group' => 'project_id'
  171. ));
  172. $this->assertEquals($expected, $result);
  173. $result = $Thread->find('all', array(
  174. 'conditions' => array('Thread.project_id' => 1),
  175. 'group' => array('project_id')
  176. ));
  177. $this->assertEquals($expected, $result);
  178. $result = $Thread->find('all', array(
  179. 'conditions' => array('Thread.project_id' => 1),
  180. 'group' => array('project_id', 'Project.id')
  181. ));
  182. $this->assertEquals($expected, $result);
  183. $result = $Thread->find('all', array(
  184. 'conditions' => array('Thread.project_id' => 1),
  185. 'group' => array('Thread.project_id', 'Project.id')
  186. ));
  187. $this->assertEquals($expected, $result);
  188. $expected = array(
  189. array('Product' => array('type' => 'Clothing'), array('price' => 32)),
  190. array('Product' => array('type' => 'Food'), array('price' => 9)),
  191. array('Product' => array('type' => 'Music'), array('price' => 4)),
  192. array('Product' => array('type' => 'Toy'), array('price' => 3))
  193. );
  194. $result = $Product->find('all',array(
  195. 'fields' => array('Product.type', 'MIN(Product.price) as price'),
  196. 'group' => 'Product.type',
  197. 'order' => 'Product.type ASC'
  198. ));
  199. $this->assertEquals($expected, $result);
  200. $result = $Product->find('all', array(
  201. 'fields' => array('Product.type', 'MIN(Product.price) as price'),
  202. 'group' => array('Product.type'),
  203. 'order' => 'Product.type ASC'));
  204. $this->assertEquals($expected, $result);
  205. }
  206. /**
  207. * testOldQuery method
  208. *
  209. * @return void
  210. */
  211. public function testOldQuery() {
  212. $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment');
  213. $Article = new Article();
  214. $query = 'SELECT title FROM ';
  215. $query .= $this->db->fullTableName('articles');
  216. $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id IN (1,2)';
  217. $results = $Article->query($query);
  218. $this->assertTrue(is_array($results));
  219. $this->assertEquals(count($results), 2);
  220. $query = 'SELECT title, body FROM ';
  221. $query .= $this->db->fullTableName('articles');
  222. $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.id = 1';
  223. $results = $Article->query($query, false);
  224. $this->assertFalse($this->db->getQueryCache($query));
  225. $this->assertTrue(is_array($results));
  226. $query = 'SELECT title, id FROM ';
  227. $query .= $this->db->fullTableName('articles');
  228. $query .= ' WHERE ' . $this->db->fullTableName('articles');
  229. $query .= '.published = ' . $this->db->value('Y');
  230. $results = $Article->query($query, true);
  231. $result = $this->db->getQueryCache($query);
  232. $this->assertFalse(empty($result));
  233. $this->assertTrue(is_array($results));
  234. }
  235. /**
  236. * testPreparedQuery method
  237. *
  238. * @return void
  239. */
  240. public function testPreparedQuery() {
  241. $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
  242. $Article = new Article();
  243. $query = 'SELECT title, published FROM ';
  244. $query .= $this->db->fullTableName('articles');
  245. $query .= ' WHERE ' . $this->db->fullTableName('articles');
  246. $query .= '.id = ? AND ' . $this->db->fullTableName('articles') . '.published = ?';
  247. $params = array(1, 'Y');
  248. $result = $Article->query($query, $params);
  249. $expected = array(
  250. '0' => array(
  251. $this->db->fullTableName('articles', false) => array(
  252. 'title' => 'First Article', 'published' => 'Y')
  253. ));
  254. if (isset($result[0][0])) {
  255. $expected[0][0] = $expected[0][$this->db->fullTableName('articles', false)];
  256. unset($expected[0][$this->db->fullTableName('articles', false)]);
  257. }
  258. $this->assertEquals($expected, $result);
  259. $result = $this->db->getQueryCache($query, $params);
  260. $this->assertFalse(empty($result));
  261. $query = 'SELECT id, created FROM ';
  262. $query .= $this->db->fullTableName('articles');
  263. $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title = ?';
  264. $params = array('First Article');
  265. $result = $Article->query($query, $params, false);
  266. $this->assertTrue(is_array($result));
  267. $this->assertTrue(
  268. isset($result[0][$this->db->fullTableName('articles', false)])
  269. || isset($result[0][0])
  270. );
  271. $result = $this->db->getQueryCache($query, $params);
  272. $this->assertTrue(empty($result));
  273. $query = 'SELECT title FROM ';
  274. $query .= $this->db->fullTableName('articles');
  275. $query .= ' WHERE ' . $this->db->fullTableName('articles') . '.title LIKE ?';
  276. $params = array('%First%');
  277. $result = $Article->query($query, $params);
  278. $this->assertTrue(is_array($result));
  279. $this->assertTrue(
  280. isset($result[0][$this->db->fullTableName('articles', false)]['title'])
  281. || isset($result[0][0]['title'])
  282. );
  283. //related to ticket #5035
  284. $query = 'SELECT title FROM ';
  285. $query .= $this->db->fullTableName('articles') . ' WHERE title = ? AND published = ?';
  286. $params = array('First? Article', 'Y');
  287. $Article->query($query, $params);
  288. $result = $this->db->getQueryCache($query, $params);
  289. $this->assertFalse($result === false);
  290. }
  291. /**
  292. * testParameterMismatch method
  293. *
  294. * @expectedException PDOException
  295. * @return void
  296. */
  297. public function testParameterMismatch() {
  298. $this->skipIf($this->db instanceof Sqlite, 'Sqlite does not accept real prepared statements, no way to check this');
  299. $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
  300. $Article = new Article();
  301. $query = 'SELECT * FROM ' . $this->db->fullTableName('articles');
  302. $query .= ' WHERE ' . $this->db->fullTableName('articles');
  303. $query .= '.published = ? AND ' . $this->db->fullTableName('articles') . '.user_id = ?';
  304. $params = array('Y');
  305. $result = $Article->query($query, $params);
  306. }
  307. /**
  308. * testVeryStrangeUseCase method
  309. *
  310. * @expectedException PDOException
  311. * @return void
  312. */
  313. public function testVeryStrangeUseCase() {
  314. $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
  315. $Article = new Article();
  316. $query = 'SELECT * FROM ? WHERE ? = ? AND ? = ?';
  317. $param = array(
  318. $this->db->fullTableName('articles'),
  319. $this->db->fullTableName('articles') . '.user_id', '3',
  320. $this->db->fullTableName('articles') . '.published', 'Y'
  321. );
  322. $result = $Article->query($query, $param);
  323. }
  324. /**
  325. * testRecursiveUnbind method
  326. *
  327. * @return void
  328. */
  329. public function testRecursiveUnbind() {
  330. $this->skipIf($this->db instanceof Sqlserver, 'The test of testRecursiveUnbind test is not compatible with SQL Server, because it check for time columns.');
  331. $this->loadFixtures('Apple', 'Sample');
  332. $TestModel = new Apple();
  333. $TestModel->recursive = 2;
  334. $result = $TestModel->find('all');
  335. $expected = array(
  336. array(
  337. 'Apple' => array(
  338. 'id' => 1,
  339. 'apple_id' => 2,
  340. 'color' => 'Red 1',
  341. 'name' => 'Red Apple 1',
  342. 'created' => '2006-11-22 10:38:58',
  343. 'date' => '1951-01-04',
  344. 'modified' => '2006-12-01 13:31:26',
  345. 'mytime' => '22:57:17'
  346. ),
  347. 'Parent' => array(
  348. 'id' => 2,
  349. 'apple_id' => 1,
  350. 'color' => 'Bright Red 1',
  351. 'name' => 'Bright Red Apple',
  352. 'created' => '2006-11-22 10:43:13',
  353. 'date' => '2014-01-01',
  354. 'modified' => '2006-11-30 18:38:10',
  355. 'mytime' => '22:57:17',
  356. 'Parent' => array(
  357. 'id' => 1,
  358. 'apple_id' => 2,
  359. 'color' => 'Red 1',
  360. 'name' => 'Red Apple 1',
  361. 'created' => '2006-11-22 10:38:58',
  362. 'date' => '1951-01-04',
  363. 'modified' => '2006-12-01 13:31:26',
  364. 'mytime' => '22:57:17'
  365. ),
  366. 'Sample' => array(
  367. 'id' => 2,
  368. 'apple_id' => 2,
  369. 'name' => 'sample2'
  370. ),
  371. 'Child' => array(
  372. array(
  373. 'id' => 1,
  374. 'apple_id' => 2,
  375. 'color' => 'Red 1',
  376. 'name' => 'Red Apple 1',
  377. 'created' => '2006-11-22 10:38:58',
  378. 'date' => '1951-01-04',
  379. 'modified' => '2006-12-01 13:31:26',
  380. 'mytime' => '22:57:17'
  381. ),
  382. array(
  383. 'id' => 3,
  384. 'apple_id' => 2,
  385. 'color' => 'blue green',
  386. 'name' => 'green blue',
  387. 'created' => '2006-12-25 05:13:36',
  388. 'date' => '2006-12-25',
  389. 'modified' => '2006-12-25 05:23:24',
  390. 'mytime' => '22:57:17'
  391. ),
  392. array(
  393. 'id' => 4,
  394. 'apple_id' => 2,
  395. 'color' => 'Blue Green',
  396. 'name' => 'Test Name',
  397. 'created' => '2006-12-25 05:23:36',
  398. 'date' => '2006-12-25',
  399. 'modified' => '2006-12-25 05:23:36',
  400. 'mytime' => '22:57:17'
  401. ))),
  402. 'Sample' => array(
  403. 'id' => '',
  404. 'apple_id' => '',
  405. 'name' => ''
  406. ),
  407. 'Child' => array(
  408. array(
  409. 'id' => 2,
  410. 'apple_id' => 1,
  411. 'color' => 'Bright Red 1',
  412. 'name' => 'Bright Red Apple',
  413. 'created' => '2006-11-22 10:43:13',
  414. 'date' => '2014-01-01',
  415. 'modified' => '2006-11-30 18:38:10',
  416. 'mytime' => '22:57:17',
  417. 'Parent' => array(
  418. 'id' => 1,
  419. 'apple_id' => 2,
  420. 'color' => 'Red 1',
  421. 'name' => 'Red Apple 1',
  422. 'created' => '2006-11-22 10:38:58',
  423. 'date' => '1951-01-04',
  424. 'modified' => '2006-12-01 13:31:26',
  425. 'mytime' => '22:57:17'
  426. ),
  427. 'Sample' => array(
  428. 'id' => 2,
  429. 'apple_id' => 2,
  430. 'name' => 'sample2'
  431. ),
  432. 'Child' => array(
  433. array(
  434. 'id' => 1,
  435. 'apple_id' => 2,
  436. 'color' => 'Red 1',
  437. 'name' => 'Red Apple 1',
  438. 'created' => '2006-11-22 10:38:58',
  439. 'date' => '1951-01-04',
  440. 'modified' => '2006-12-01 13:31:26',
  441. 'mytime' => '22:57:17'
  442. ),
  443. array(
  444. 'id' => 3,
  445. 'apple_id' => 2,
  446. 'color' => 'blue green',
  447. 'name' => 'green blue',
  448. 'created' => '2006-12-25 05:13:36',
  449. 'date' => '2006-12-25',
  450. 'modified' => '2006-12-25 05:23:24',
  451. 'mytime' => '22:57:17'
  452. ),
  453. array(
  454. 'id' => 4,
  455. 'apple_id' => 2,
  456. 'color' => 'Blue Green',
  457. 'name' => 'Test Name',
  458. 'created' => '2006-12-25 05:23:36',
  459. 'date' => '2006-12-25',
  460. 'modified' => '2006-12-25 05:23:36',
  461. 'mytime' => '22:57:17'
  462. ))))),
  463. array(
  464. 'Apple' => array(
  465. 'id' => 2,
  466. 'apple_id' => 1,
  467. 'color' => 'Bright Red 1',
  468. 'name' => 'Bright Red Apple',
  469. 'created' => '2006-11-22 10:43:13',
  470. 'date' => '2014-01-01',
  471. 'modified' => '2006-11-30 18:38:10',
  472. 'mytime' => '22:57:17'
  473. ),
  474. 'Parent' => array(
  475. 'id' => 1,
  476. 'apple_id' => 2,
  477. 'color' => 'Red 1',
  478. 'name' => 'Red Apple 1',
  479. 'created' => '2006-11-22 10:38:58',
  480. 'date' => '1951-01-04',
  481. 'modified' => '2006-12-01 13:31:26',
  482. 'mytime' => '22:57:17',
  483. 'Parent' => array(
  484. 'id' => 2,
  485. 'apple_id' => 1,
  486. 'color' => 'Bright Red 1',
  487. 'name' => 'Bright Red Apple',
  488. 'created' => '2006-11-22 10:43:13',
  489. 'date' => '2014-01-01',
  490. 'modified' => '2006-11-30 18:38:10',
  491. 'mytime' => '22:57:17'
  492. ),
  493. 'Sample' => array(),
  494. 'Child' => array(
  495. array(
  496. 'id' => 2,
  497. 'apple_id' => 1,
  498. 'color' => 'Bright Red 1',
  499. 'name' => 'Bright Red Apple',
  500. 'created' => '2006-11-22 10:43:13',
  501. 'date' => '2014-01-01',
  502. 'modified' => '2006-11-30 18:38:10',
  503. 'mytime' => '22:57:17'
  504. ))),
  505. 'Sample' => array(
  506. 'id' => 2,
  507. 'apple_id' => 2,
  508. 'name' => 'sample2',
  509. 'Apple' => array(
  510. 'id' => 2,
  511. 'apple_id' => 1,
  512. 'color' => 'Bright Red 1',
  513. 'name' => 'Bright Red Apple',
  514. 'created' => '2006-11-22 10:43:13',
  515. 'date' => '2014-01-01',
  516. 'modified' => '2006-11-30 18:38:10',
  517. 'mytime' => '22:57:17'
  518. )),
  519. 'Child' => array(
  520. array(
  521. 'id' => 1,
  522. 'apple_id' => 2,
  523. 'color' => 'Red 1',
  524. 'name' => 'Red Apple 1',
  525. 'created' => '2006-11-22 10:38:58',
  526. 'date' => '1951-01-04',
  527. 'modified' => '2006-12-01 13:31:26',
  528. 'mytime' => '22:57:17',
  529. 'Parent' => array(
  530. 'id' => 2,
  531. 'apple_id' => 1,
  532. 'color' => 'Bright Red 1',
  533. 'name' => 'Bright Red Apple',
  534. 'created' => '2006-11-22 10:43:13',
  535. 'date' => '2014-01-01',
  536. 'modified' => '2006-11-30 18:38:10',
  537. 'mytime' => '22:57:17'
  538. ),
  539. 'Sample' => array(),
  540. 'Child' => array(
  541. array(
  542. 'id' => 2,
  543. 'apple_id' => 1,
  544. 'color' => 'Bright Red 1',
  545. 'name' => 'Bright Red Apple',
  546. 'created' => '2006-11-22 10:43:13',
  547. 'date' => '2014-01-01',
  548. 'modified' => '2006-11-30 18:38:10',
  549. 'mytime' => '22:57:17'
  550. ))),
  551. array(
  552. 'id' => 3,
  553. 'apple_id' => 2,
  554. 'color' => 'blue green',
  555. 'name' => 'green blue',
  556. 'created' => '2006-12-25 05:13:36',
  557. 'date' => '2006-12-25',
  558. 'modified' => '2006-12-25 05:23:24',
  559. 'mytime' => '22:57:17',
  560. 'Parent' => array(
  561. 'id' => 2,
  562. 'apple_id' => 1,
  563. 'color' => 'Bright Red 1',
  564. 'name' => 'Bright Red Apple',
  565. 'created' => '2006-11-22 10:43:13',
  566. 'date' => '2014-01-01',
  567. 'modified' => '2006-11-30 18:38:10',
  568. 'mytime' => '22:57:17'
  569. ),
  570. 'Sample' => array(
  571. 'id' => 1,
  572. 'apple_id' => 3,
  573. 'name' => 'sample1'
  574. )),
  575. array(
  576. 'id' => 4,
  577. 'apple_id' => 2,
  578. 'color' => 'Blue Green',
  579. 'name' => 'Test Name',
  580. 'created' => '2006-12-25 05:23:36',
  581. 'date' => '2006-12-25',
  582. 'modified' => '2006-12-25 05:23:36',
  583. 'mytime' => '22:57:17',
  584. 'Parent' => array(
  585. 'id' => 2,
  586. 'apple_id' => 1,
  587. 'color' => 'Bright Red 1',
  588. 'name' => 'Bright Red Apple',
  589. 'created' => '2006-11-22 10:43:13',
  590. 'date' => '2014-01-01',
  591. 'modified' => '2006-11-30 18:38:10',
  592. 'mytime' => '22:57:17'
  593. ),
  594. 'Sample' => array(
  595. 'id' => 3,
  596. 'apple_id' => 4,
  597. 'name' => 'sample3'
  598. ),
  599. 'Child' => array(
  600. array(
  601. 'id' => 6,
  602. 'apple_id' => 4,
  603. 'color' => 'My new appleOrange',
  604. 'name' => 'My new apple',
  605. 'created' => '2006-12-25 05:29:39',
  606. 'date' => '2006-12-25',
  607. 'modified' => '2006-12-25 05:29:39',
  608. 'mytime' => '22:57:17'
  609. ))))),
  610. array(
  611. 'Apple' => array(
  612. 'id' => 3,
  613. 'apple_id' => 2,
  614. 'color' => 'blue green',
  615. 'name' => 'green blue',
  616. 'created' => '2006-12-25 05:13:36',
  617. 'date' => '2006-12-25',
  618. 'modified' => '2006-12-25 05:23:24',
  619. 'mytime' => '22:57:17'
  620. ),
  621. 'Parent' => array(
  622. 'id' => 2,
  623. 'apple_id' => 1,
  624. 'color' => 'Bright Red 1',
  625. 'name' => 'Bright Red Apple',
  626. 'created' => '2006-11-22 10:43:13',
  627. 'date' => '2014-01-01',
  628. 'modified' => '2006-11-30 18:38:10',
  629. 'mytime' => '22:57:17',
  630. 'Parent' => array(
  631. 'id' => 1,
  632. 'apple_id' => 2,
  633. 'color' => 'Red 1',
  634. 'name' => 'Red Apple 1',
  635. 'created' => '2006-11-22 10:38:58',
  636. 'date' => '1951-01-04',
  637. 'modified' => '2006-12-01 13:31:26',
  638. 'mytime' => '22:57:17'
  639. ),
  640. 'Sample' => array(
  641. 'id' => 2,
  642. 'apple_id' => 2,
  643. 'name' => 'sample2'
  644. ),
  645. 'Child' => array(
  646. array(
  647. 'id' => 1,
  648. 'apple_id' => 2,
  649. 'color' => 'Red 1',
  650. 'name' => 'Red Apple 1',
  651. 'created' => '2006-11-22 10:38:58',
  652. 'date' => '1951-01-04',
  653. 'modified' => '2006-12-01 13:31:26',
  654. 'mytime' => '22:57:17'
  655. ),
  656. array(
  657. 'id' => 3,
  658. 'apple_id' => 2,
  659. 'color' => 'blue green',
  660. 'name' => 'green blue',
  661. 'created' => '2006-12-25 05:13:36',
  662. 'date' => '2006-12-25',
  663. 'modified' => '2006-12-25 05:23:24',
  664. 'mytime' => '22:57:17'
  665. ),
  666. array(
  667. 'id' => 4,
  668. 'apple_id' => 2,
  669. 'color' => 'Blue Green',
  670. 'name' => 'Test Name',
  671. 'created' => '2006-12-25 05:23:36',
  672. 'date' => '2006-12-25',
  673. 'modified' => '2006-12-25 05:23:36',
  674. 'mytime' => '22:57:17'
  675. ))),
  676. 'Sample' => array(
  677. 'id' => 1,
  678. 'apple_id' => 3,
  679. 'name' => 'sample1',
  680. 'Apple' => array(
  681. 'id' => 3,
  682. 'apple_id' => 2,
  683. 'color' => 'blue green',
  684. 'name' => 'green blue',
  685. 'created' => '2006-12-25 05:13:36',
  686. 'date' => '2006-12-25',
  687. 'modified' => '2006-12-25 05:23:24',
  688. 'mytime' => '22:57:17'
  689. )),
  690. 'Child' => array()
  691. ),
  692. array(
  693. 'Apple' => array(
  694. 'id' => 4,
  695. 'apple_id' => 2,
  696. 'color' => 'Blue Green',
  697. 'name' => 'Test Name',
  698. 'created' => '2006-12-25 05:23:36',
  699. 'date' => '2006-12-25',
  700. 'modified' => '2006-12-25 05:23:36',
  701. 'mytime' => '22:57:17'
  702. ),
  703. 'Parent' => array(
  704. 'id' => 2,
  705. 'apple_id' => 1,
  706. 'color' => 'Bright Red 1',
  707. 'name' => 'Bright Red Apple',
  708. 'created' => '2006-11-22 10:43:13',
  709. 'date' => '2014-01-01',
  710. 'modified' => '2006-11-30 18:38:10',
  711. 'mytime' => '22:57:17',
  712. 'Parent' => array(
  713. 'id' => 1,
  714. 'apple_id' => 2,
  715. 'color' => 'Red 1',
  716. 'name' => 'Red Apple 1',
  717. 'created' => '2006-11-22 10:38:58',
  718. 'date' => '1951-01-04',
  719. 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
  720. 'Sample' => array('id' => 2, 'apple_id' => 2, 'name' => 'sample2'),
  721. 'Child' => array(
  722. array(
  723. 'id' => 1,
  724. 'apple_id' => 2,
  725. 'color' => 'Red 1',
  726. 'name' => 'Red Apple 1',
  727. 'created' => '2006-11-22 10:38:58',
  728. 'date' => '1951-01-04',
  729. 'modified' => '2006-12-01 13:31:26',
  730. 'mytime' => '22:57:17'
  731. ),
  732. array(
  733. 'id' => 3,
  734. 'apple_id' => 2,
  735. 'color' => 'blue green',
  736. 'name' => 'green blue',
  737. 'created' => '2006-12-25 05:13:36',
  738. 'date' => '2006-12-25',
  739. 'modified' => '2006-12-25 05:23:24',
  740. 'mytime' => '22:57:17'
  741. ),
  742. array(
  743. 'id' => 4,
  744. 'apple_id' => 2,
  745. 'color' => 'Blue Green',
  746. 'name' => 'Test Name',
  747. 'created' => '2006-12-25 05:23:36',
  748. 'date' => '2006-12-25',
  749. 'modified' => '2006-12-25 05:23:36',
  750. 'mytime' => '22:57:17'
  751. ))),
  752. 'Sample' => array(
  753. 'id' => 3,
  754. 'apple_id' => 4,
  755. 'name' => 'sample3',
  756. 'Apple' => array(
  757. 'id' => 4,
  758. 'apple_id' => 2,
  759. 'color' => 'Blue Green',
  760. 'name' => 'Test Name',
  761. 'created' => '2006-12-25 05:23:36',
  762. 'date' => '2006-12-25',
  763. 'modified' => '2006-12-25 05:23:36',
  764. 'mytime' => '22:57:17'
  765. )),
  766. 'Child' => array(
  767. array(
  768. 'id' => 6,
  769. 'apple_id' => 4,
  770. 'color' => 'My new appleOrange',
  771. 'name' => 'My new apple',
  772. 'created' => '2006-12-25 05:29:39',
  773. 'date' => '2006-12-25',
  774. 'modified' => '2006-12-25 05:29:39',
  775. 'mytime' => '22:57:17',
  776. 'Parent' => array(
  777. 'id' => 4,
  778. 'apple_id' => 2,
  779. 'color' => 'Blue Green',
  780. 'name' => 'Test Name',
  781. 'created' => '2006-12-25 05:23:36',
  782. 'date' => '2006-12-25',
  783. 'modified' => '2006-12-25 05:23:36',
  784. 'mytime' => '22:57:17'
  785. ),
  786. 'Sample' => array(),
  787. 'Child' => array(
  788. array(
  789. 'id' => 7,
  790. 'apple_id' => 6,
  791. 'color' => 'Some wierd color',
  792. 'name' => 'Some odd color',
  793. 'created' => '2006-12-25 05:34:21',
  794. 'date' => '2006-12-25',
  795. 'modified' => '2006-12-25 05:34:21',
  796. 'mytime' => '22:57:17'
  797. ))))),
  798. array(
  799. 'Apple' => array(
  800. 'id' => 5,
  801. 'apple_id' => 5,
  802. 'color' => 'Green',
  803. 'name' => 'Blue Green',
  804. 'created' => '2006-12-25 05:24:06',
  805. 'date' => '2006-12-25',
  806. 'modified' => '2006-12-25 05:29:16',
  807. 'mytime' => '22:57:17'
  808. ),
  809. 'Parent' => array(
  810. 'id' => 5,
  811. 'apple_id' => 5,
  812. 'color' => 'Green',
  813. 'name' => 'Blue Green',
  814. 'created' => '2006-12-25 05:24:06',
  815. 'date' => '2006-12-25',
  816. 'modified' => '2006-12-25 05:29:16',
  817. 'mytime' => '22:57:17',
  818. 'Parent' => array(
  819. 'id' => 5,
  820. 'apple_id' => 5,
  821. 'color' => 'Green',
  822. 'name' => 'Blue Green',
  823. 'created' => '2006-12-25 05:24:06',
  824. 'date' => '2006-12-25',
  825. 'modified' => '2006-12-25 05:29:16',
  826. 'mytime' => '22:57:17'
  827. ),
  828. 'Sample' => array(
  829. 'id' => 4,
  830. 'apple_id' => 5,
  831. 'name' => 'sample4'
  832. ),
  833. 'Child' => array(
  834. array(
  835. 'id' => 5,
  836. 'apple_id' => 5,
  837. 'color' => 'Green',
  838. 'name' => 'Blue Green',
  839. 'created' => '2006-12-25 05:24:06',
  840. 'date' => '2006-12-25',
  841. 'modified' => '2006-12-25 05:29:16',
  842. 'mytime' => '22:57:17'
  843. ))),
  844. 'Sample' => array(
  845. 'id' => 4,
  846. 'apple_id' => 5,
  847. 'name' => 'sample4',
  848. 'Apple' => array(
  849. 'id' => 5,
  850. 'apple_id' => 5,
  851. 'color' => 'Green',
  852. 'name' => 'Blue Green',
  853. 'created' => '2006-12-25 05:24:06',
  854. 'date' => '2006-12-25',
  855. 'modified' => '2006-12-25 05:29:16',
  856. 'mytime' => '22:57:17'
  857. )),
  858. 'Child' => array(
  859. array(
  860. 'id' => 5,
  861. 'apple_id' => 5,
  862. 'color' => 'Green',
  863. 'name' => 'Blue Green',
  864. 'created' => '2006-12-25 05:24:06',
  865. 'date' => '2006-12-25',
  866. 'modified' => '2006-12-25 05:29:16',
  867. 'mytime' => '22:57:17',
  868. 'Parent' => array(
  869. 'id' => 5,
  870. 'apple_id' => 5,
  871. 'color' => 'Green',
  872. 'name' => 'Blue Green',
  873. 'created' => '2006-12-25 05:24:06',
  874. 'date' => '2006-12-25',
  875. 'modified' => '2006-12-25 05:29:16',
  876. 'mytime' => '22:57:17'
  877. ),
  878. 'Sample' => array(
  879. 'id' => 4,
  880. 'apple_id' => 5,
  881. 'name' => 'sample4'
  882. ),
  883. 'Child' => array(
  884. array(
  885. 'id' => 5,
  886. 'apple_id' => 5,
  887. 'color' => 'Green',
  888. 'name' => 'Blue Green',
  889. 'created' => '2006-12-25 05:24:06',
  890. 'date' => '2006-12-25',
  891. 'modified' => '2006-12-25 05:29:16',
  892. 'mytime' => '22:57:17'
  893. ))))),
  894. array(
  895. 'Apple' => array(
  896. 'id' => 6,
  897. 'apple_id' => 4,
  898. 'color' => 'My new appleOrange',
  899. 'name' => 'My new apple',
  900. 'created' => '2006-12-25 05:29:39',
  901. 'date' => '2006-12-25',
  902. 'modified' => '2006-12-25 05:29:39',
  903. 'mytime' => '22:57:17'
  904. ),
  905. 'Parent' => array(
  906. 'id' => 4,
  907. 'apple_id' => 2,
  908. 'color' => 'Blue Green',
  909. 'name' => 'Test Name',
  910. 'created' => '2006-12-25 05:23:36',
  911. 'date' => '2006-12-25',
  912. 'modified' => '2006-12-25 05:23:36',
  913. 'mytime' => '22:57:17',
  914. 'Parent' => array(
  915. 'id' => 2,
  916. 'apple_id' => 1,
  917. 'color' => 'Bright Red 1',
  918. 'name' => 'Bright Red Apple',
  919. 'created' => '2006-11-22 10:43:13',
  920. 'date' => '2014-01-01',
  921. 'modified' => '2006-11-30 18:38:10',
  922. 'mytime' => '22:57:17'
  923. ),
  924. 'Sample' => array(
  925. 'id' => 3,
  926. 'apple_id' => 4,
  927. 'name' => 'sample3'
  928. ),
  929. 'Child' => array(
  930. array(
  931. 'id' => 6,
  932. 'apple_id' => 4,
  933. 'color' => 'My new appleOrange',
  934. 'name' => 'My new apple',
  935. 'created' => '2006-12-25 05:29:39',
  936. 'date' => '2006-12-25',
  937. 'modified' => '2006-12-25 05:29:39',
  938. 'mytime' => '22:57:17'
  939. ))),
  940. 'Sample' => array(
  941. 'id' => '',
  942. 'apple_id' => '',
  943. 'name' => ''
  944. ),
  945. 'Child' => array(
  946. array(
  947. 'id' => 7,
  948. 'apple_id' => 6,
  949. 'color' => 'Some wierd color',
  950. 'name' => 'Some odd color',
  951. 'created' => '2006-12-25 05:34:21',
  952. 'date' => '2006-12-25',
  953. 'modified' => '2006-12-25 05:34:21',
  954. 'mytime' => '22:57:17',
  955. 'Parent' => array(
  956. 'id' => 6,
  957. 'apple_id' => 4,
  958. 'color' => 'My new appleOrange',
  959. 'name' => 'My new apple',
  960. 'created' => '2006-12-25 05:29:39',
  961. 'date' => '2006-12-25',
  962. 'modified' => '2006-12-25 05:29:39',
  963. 'mytime' => '22:57:17'
  964. ),
  965. 'Sample' => array()
  966. ))),
  967. array(
  968. 'Apple' => array(
  969. 'id' => 7,
  970. 'apple_id' => 6,
  971. 'color' =>
  972. 'Some wierd color',
  973. 'name' => 'Some odd color',
  974. 'created' => '2006-12-25 05:34:21',
  975. 'date' => '2006-12-25',
  976. 'modified' => '2006-12-25 05:34:21',
  977. 'mytime' => '22:57:17'
  978. ),
  979. 'Parent' => array(
  980. 'id' => 6,
  981. 'apple_id' => 4,
  982. 'color' => 'My new appleOrange',
  983. 'name' => 'My new apple',
  984. 'created' => '2006-12-25 05:29:39',
  985. 'date' => '2006-12-25',
  986. 'modified' => '2006-12-25 05:29:39',
  987. 'mytime' => '22:57:17',
  988. 'Parent' => array(
  989. 'id' => 4,
  990. 'apple_id' => 2,
  991. 'color' => 'Blue Green',
  992. 'name' => 'Test Name',
  993. 'created' => '2006-12-25 05:23:36',
  994. 'date' => '2006-12-25',
  995. 'modified' => '2006-12-25 05:23:36',
  996. 'mytime' => '22:57:17'
  997. ),
  998. 'Sample' => array(),
  999. 'Child' => array(
  1000. array(
  1001. 'id' => 7,
  1002. 'apple_id' => 6,
  1003. 'color' => 'Some wierd color',
  1004. 'name' => 'Some odd color',
  1005. 'created' => '2006-12-25 05:34:21',
  1006. 'date' => '2006-12-25',
  1007. 'modified' => '2006-12-25 05:34:21',
  1008. 'mytime' => '22:57:17'
  1009. ))),
  1010. 'Sample' => array(
  1011. 'id' => '',
  1012. 'apple_id' => '',
  1013. 'name' => ''
  1014. ),
  1015. 'Child' => array()));
  1016. $this->assertEquals($expected, $result);
  1017. $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample')));
  1018. $this->assertTrue($result);
  1019. $result = $TestModel->find('all');
  1020. $expected = array(
  1021. array(
  1022. 'Apple' => array(
  1023. 'id' => 1,
  1024. 'apple_id' => 2,
  1025. 'color' => 'Red 1',
  1026. 'name' => 'Red Apple 1',
  1027. 'created' => '2006-11-22 10:38:58',
  1028. 'date' => '1951-01-04',
  1029. 'modified' => '2006-12-01 13:31:26',
  1030. 'mytime' => '22:57:17'),
  1031. 'Parent' => array(
  1032. 'id' => 2,
  1033. 'apple_id' => 1,
  1034. 'color' => 'Bright Red 1',
  1035. 'name' => 'Bright Red Apple',
  1036. 'created' => '2006-11-22 10:43:13',
  1037. 'date' => '2014-01-01',
  1038. 'modified' => '2006-11-30 18:38:10',
  1039. 'mytime' => '22:57:17',
  1040. 'Parent' => array(
  1041. 'id' => 1,
  1042. 'apple_id' => 2,
  1043. 'color' => 'Red 1',
  1044. 'name' => 'Red Apple 1',
  1045. 'created' => '2006-11-22 10:38:58',
  1046. 'date' => '1951-01-04',
  1047. 'modified' => '2006-12-01 13:31:26',
  1048. 'mytime' => '22:57:17'
  1049. ),
  1050. 'Child' => array(
  1051. array(
  1052. 'id' => 1,
  1053. 'apple_id' => 2,
  1054. 'color' => 'Red 1',
  1055. 'name' => 'Red Apple 1',
  1056. 'created' => '2006-11-22 10:38:58',
  1057. 'date' => '1951-01-04',
  1058. 'modified' => '2006-12-01 13:31:26',
  1059. 'mytime' => '22:57:17'
  1060. ),
  1061. array(
  1062. 'id' => 3,
  1063. 'apple_id' => 2,
  1064. 'color' => 'blue green',
  1065. 'name' => 'green blue',
  1066. 'created' => '2006-12-25 05:13:36',
  1067. 'date' => '2006-12-25',
  1068. 'modified' => '2006-12-25 05:23:24',
  1069. 'mytime' => '22:57:17'
  1070. ),
  1071. array(
  1072. 'id' => 4,
  1073. 'apple_id' => 2,
  1074. 'color' => 'Blue Green',
  1075. 'name' => 'Test Name',
  1076. 'created' => '2006-12-25 05:23:36',
  1077. 'date' => '2006-12-25',
  1078. 'modified' => '2006-12-25 05:23:36',
  1079. 'mytime' => '22:57:17'
  1080. ))),
  1081. 'Sample' => array(
  1082. 'id' => '',
  1083. 'apple_id' => '',
  1084. 'name' => ''
  1085. ),
  1086. 'Child' => array(
  1087. array(
  1088. 'id' => 2,
  1089. 'apple_id' => 1,
  1090. 'color' => 'Bright Red 1',
  1091. 'name' => 'Bright Red Apple',
  1092. 'created' => '2006-11-22 10:43:13',
  1093. 'date' => '2014-01-01',
  1094. 'modified' => '2006-11-30 18:38:10',
  1095. 'mytime' => '22:57:17',
  1096. 'Parent' => array(
  1097. 'id' => 1,
  1098. 'apple_id' => 2,
  1099. 'color' => 'Red 1',
  1100. 'name' => 'Red Apple 1',
  1101. 'created' => '2006-11-22 10:38:58',
  1102. 'date' => '1951-01-04',
  1103. 'modified' => '2006-12-01 13:31:26',
  1104. 'mytime' => '22:57:17'
  1105. ),
  1106. 'Sample' => array(
  1107. 'id' => 2,
  1108. 'apple_id' => 2,
  1109. 'name' => 'sample2'
  1110. ),
  1111. 'Child' => array(
  1112. array(
  1113. 'id' => 1,
  1114. 'apple_id' => 2,
  1115. 'color' => 'Red 1',
  1116. 'name' => 'Red Apple 1',
  1117. 'created' => '2006-11-22 10:38:58',
  1118. 'date' => '1951-01-04',
  1119. 'modified' => '2006-12-01 13:31:26',
  1120. 'mytime' => '22:57:17'
  1121. ),
  1122. array(
  1123. 'id' => 3,
  1124. 'apple_id' => 2,
  1125. 'color' => 'blue green',
  1126. 'name' => 'green blue',
  1127. 'created' => '2006-12-25 05:13:36',
  1128. 'date' => '2006-12-25',
  1129. 'modified' => '2006-12-25 05:23:24',
  1130. 'mytime' => '22:57:17'
  1131. ),
  1132. array(
  1133. 'id' => 4,
  1134. 'apple_id' => 2,
  1135. 'color' => 'Blue Green',
  1136. 'name' => 'Test Name',
  1137. 'created' => '2006-12-25 05:23:36',
  1138. 'date' => '2006-12-25',
  1139. 'modified' => '2006-12-25 05:23:36',
  1140. 'mytime' => '22:57:17'
  1141. ))))),
  1142. array(
  1143. 'Apple' => array(
  1144. 'id' => 2,
  1145. 'apple_id' => 1,
  1146. 'color' => 'Bright Red 1',
  1147. 'name' => 'Bright Red Apple',
  1148. 'created' => '2006-11-22 10:43:13',
  1149. 'date' => '2014-01-01',
  1150. 'modified' => '2006-11-30 18:38:10',
  1151. 'mytime' => '22:57:17'
  1152. ),
  1153. 'Parent' => array(
  1154. 'id' => 1,
  1155. 'apple_id' => 2,
  1156. 'color' => 'Red 1',
  1157. 'name' => 'Red Apple 1',
  1158. 'created' => '2006-11-22 10:38:58',
  1159. 'date' => '1951-01-04',
  1160. 'modified' => '2006-12-01 13:31:26',
  1161. 'mytime' => '22:57:17',
  1162. 'Parent' => array(
  1163. 'id' => 2,
  1164. 'apple_id' => 1,
  1165. 'color' => 'Bright Red 1',
  1166. 'name' => 'Bright Red Apple',
  1167. 'created' => '2006-11-22 10:43:13',
  1168. 'date' => '2014-01-01',
  1169. 'modified' => '2006-11-30 18:38:10',
  1170. 'mytime' => '22:57:17'
  1171. ),
  1172. 'Child' => array(
  1173. array(
  1174. 'id' => 2,
  1175. 'apple_id' => 1,
  1176. 'color' => 'Bright Red 1',
  1177. 'name' => 'Bright Red Apple',
  1178. 'created' => '2006-11-22 10:43:13',
  1179. 'date' => '2014-01-01',
  1180. 'modified' => '2006-11-30 18:38:10',
  1181. 'mytime' => '22:57:17'
  1182. ))),
  1183. 'Sample' => array(
  1184. 'id' => 2,
  1185. 'apple_id' => 2,
  1186. 'name' => 'sample2',
  1187. 'Apple' => array(
  1188. 'id' => 2,
  1189. 'apple_id' => 1,
  1190. 'color' => 'Bright Red 1',
  1191. 'name' => 'Bright Red Apple',
  1192. 'created' => '2006-11-22 10:43:13',
  1193. 'date' => '2014-01-01',
  1194. 'modified' => '2006-11-30 18:38:10',
  1195. 'mytime' => '22:57:17'
  1196. )),
  1197. 'Child' => array(
  1198. array(
  1199. 'id' => 1,
  1200. 'apple_id' => 2,
  1201. 'color' => 'Red 1',
  1202. 'name' => 'Red Apple 1',
  1203. 'created' => '2006-11-22 10:38:58',
  1204. 'date' => '1951-01-04',
  1205. 'modified' => '2006-12-01 13:31:26',
  1206. 'mytime' => '22:57:17',
  1207. 'Parent' => array(
  1208. 'id' => 2,
  1209. 'apple_id' => 1,
  1210. 'color' => 'Bright Red 1',
  1211. 'name' => 'Bright Red Apple',
  1212. 'created' => '2006-11-22 10:43:13',
  1213. 'date' => '2014-01-01',
  1214. 'modified' => '2006-11-30 18:38:10',
  1215. 'mytime' => '22:57:17'
  1216. ),
  1217. 'Sample' => array(),
  1218. 'Child' => array(
  1219. array(
  1220. 'id' => 2,
  1221. 'apple_id' => 1,
  1222. 'color' => 'Bright Red 1',
  1223. 'name' => 'Bright Red Apple',
  1224. 'created' => '2006-11-22 10:43:13',
  1225. 'date' => '2014-01-01', 'modified' =>
  1226. '2006-11-30 18:38:10',
  1227. 'mytime' => '22:57:17'
  1228. ))),
  1229. array(
  1230. 'id' => 3,
  1231. 'apple_id' => 2,
  1232. 'color' => 'blue green',
  1233. 'name' => 'green blue',
  1234. 'created' => '2006-12-25 05:13:36',
  1235. 'date' => '2006-12-25',
  1236. 'modified' => '2006-12-25 05:23:24',
  1237. 'mytime' => '22:57:17',
  1238. 'Parent' => array(
  1239. 'id' => 2,
  1240. 'apple_id' => 1,
  1241. 'color' => 'Bright Red 1',
  1242. 'name' => 'Bright Red Apple',
  1243. 'created' => '2006-11-22 10:43:13',
  1244. 'date' => '2014-01-01',
  1245. 'modified' => '2006-11-30 18:38:10',
  1246. 'mytime' => '22:57:17'
  1247. ),
  1248. 'Sample' => array(
  1249. 'id' => 1,
  1250. 'apple_id' => 3,
  1251. 'name' => 'sample1'
  1252. )),
  1253. array(
  1254. 'id' => 4,
  1255. 'apple_id' => 2,
  1256. 'color' => 'Blue Green',
  1257. 'name' => 'Test Name',
  1258. 'created' => '2006-12-25 05:23:36',
  1259. 'date' => '2006-12-25',
  1260. 'modified' => '2006-12-25 05:23:36',
  1261. 'mytime' => '22:57:17',
  1262. 'Parent' => array(
  1263. 'id' => 2,
  1264. 'apple_id' => 1,
  1265. 'color' => 'Bright Red 1',
  1266. 'name' => 'Bright Red Apple',
  1267. 'created' => '2006-11-22 10:43:13',
  1268. 'date' => '2014-01-01',
  1269. 'modified' => '2006-11-30 18:38:10',
  1270. 'mytime' => '22:57:17'
  1271. ),
  1272. 'Sample' => array(
  1273. 'id' => 3,
  1274. 'apple_id' => 4,
  1275. 'name' => 'sample3'
  1276. ),
  1277. 'Child' => array(
  1278. array(
  1279. 'id' => 6,
  1280. 'apple_id' => 4,
  1281. 'color' => 'My new appleOrange',
  1282. 'name' => 'My new apple',
  1283. 'created' => '2006-12-25 05:29:39',
  1284. 'date' => '2006-12-25',
  1285. 'modified' => '2006-12-25 05:29:39',
  1286. 'mytime' => '22:57:17'
  1287. ))))),
  1288. array(
  1289. 'Apple' => array(
  1290. 'id' => 3,
  1291. 'apple_id' => 2,
  1292. 'color' => 'blue green',
  1293. 'name' => 'green blue',
  1294. 'created' => '2006-12-25 05:13:36',
  1295. 'date' => '2006-12-25',
  1296. 'modified' => '2006-12-25 05:23:24',
  1297. 'mytime' => '22:57:17'
  1298. ),
  1299. 'Parent' => array(
  1300. 'id' => 2,
  1301. 'apple_id' => 1,
  1302. 'color' => 'Bright Red 1',
  1303. 'name' => 'Bright Red Apple',
  1304. 'created' => '2006-11-22 10:43:13',
  1305. 'date' => '2014-01-01',
  1306. 'modified' => '2006-11-30 18:38:10',
  1307. 'mytime' => '22:57:17',
  1308. 'Parent' => array(
  1309. 'id' => 1,
  1310. 'apple_id' => 2,
  1311. 'color' => 'Red 1',
  1312. 'name' => 'Red Apple 1',
  1313. 'created' => '2006-11-22 10:38:58',
  1314. 'date' => '1951-01-04',
  1315. 'modified' => '2006-12-01 13:31:26',
  1316. 'mytime' => '22:57:17'
  1317. ),
  1318. 'Child' => array(
  1319. array(
  1320. 'id' => 1,
  1321. 'apple_id' => 2,
  1322. 'color' => 'Red 1',
  1323. 'name' => 'Red Apple 1',
  1324. 'created' => '2006-11-22 10:38:58',
  1325. 'date' => '1951-01-04',
  1326. 'modified' => '2006-12-01 13:31:26',
  1327. 'mytime' => '22:57:17'
  1328. ),
  1329. array(
  1330. 'id' => 3,
  1331. 'apple_id' => 2,
  1332. 'color' => 'blue green',
  1333. 'name' => 'green blue',
  1334. 'created' => '2006-12-25 05:13:36',
  1335. 'date' => '2006-12-25',
  1336. 'modified' => '2006-12-25 05:23:24',
  1337. 'mytime' => '22:57:17'
  1338. ),
  1339. array(
  1340. 'id' => 4,
  1341. 'apple_id' => 2,
  1342. 'color' => 'Blue Green',
  1343. 'name' => 'Test Name',
  1344. 'created' => '2006-12-25 05:23:36',
  1345. 'date' => '2006-12-25',
  1346. 'modified' => '2006-12-25 05:23:36',
  1347. 'mytime' => '22:57:17'
  1348. ))),
  1349. 'Sample' => array(
  1350. 'id' => 1,
  1351. 'apple_id' => 3,
  1352. 'name' => 'sample1',
  1353. 'Apple' => array(
  1354. 'id' => 3,
  1355. 'apple_id' => 2,
  1356. 'color' => 'blue green',
  1357. 'name' => 'green blue',
  1358. 'created' => '2006-12-25 05:13:36',
  1359. 'date' => '2006-12-25',
  1360. 'modified' => '2006-12-25 05:23:24',
  1361. 'mytime' => '22:57:17'
  1362. )),
  1363. 'Child' => array()
  1364. ),
  1365. array(
  1366. 'Apple' => array(
  1367. 'id' => 4,
  1368. 'apple_id' => 2,
  1369. 'color' => 'Blue Green',
  1370. 'name' => 'Test Name',
  1371. 'created' => '2006-12-25 05:23:36',
  1372. 'date' => '2006-12-25',
  1373. 'modified' => '2006-12-25 05:23:36',
  1374. 'mytime' => '22:57:17'
  1375. ),
  1376. 'Parent' => array(
  1377. 'id' => 2,
  1378. 'apple_id' => 1,
  1379. 'color' => 'Bright Red 1',
  1380. 'name' => 'Bright Red Apple',
  1381. 'created' => '2006-11-22 10:43:13',
  1382. 'date' => '2014-01-01',
  1383. 'modified' => '2006-11-30 18:38:10',
  1384. 'mytime' => '22:57:17',
  1385. 'Parent' => array(
  1386. 'id' => 1,
  1387. 'apple_id' => 2,
  1388. 'color' => 'Red 1',
  1389. 'name' => 'Red Apple 1',
  1390. 'created' => '2006-11-22 10:38:58',
  1391. 'date' => '1951-01-04',
  1392. 'modified' => '2006-12-01 13:31:26',
  1393. 'mytime' => '22:57:17'
  1394. ),
  1395. 'Child' => array(
  1396. array(
  1397. 'id' => 1,
  1398. 'apple_id' => 2,
  1399. 'color' => 'Red 1',
  1400. 'name' => 'Red Apple 1',
  1401. 'created' => '2006-11-22 10:38:58',
  1402. 'date' => '1951-01-04',
  1403. 'modified' => '2006-12-01 13:31:26',
  1404. 'mytime' => '22:57:17'
  1405. ),
  1406. array(
  1407. 'id' => 3,
  1408. 'apple_id' => 2,
  1409. 'color' => 'blue green',
  1410. 'name' => 'green blue',
  1411. 'created' => '2006-12-25 05:13:36',
  1412. 'date' => '2006-12-25',
  1413. 'modified' => '2006-12-25 05:23:24',
  1414. 'mytime' => '22:57:17'
  1415. ),
  1416. array(
  1417. 'id' => 4,
  1418. 'apple_id' => 2,
  1419. 'color' => 'Blue Green',
  1420. 'name' => 'Test Name',
  1421. 'created' => '2006-12-25 05:23:36',
  1422. 'date' => '2006-12-25',
  1423. 'modified' => '2006-12-25 05:23:36',
  1424. 'mytime' => '22:57:17'
  1425. ))),
  1426. 'Sample' => array(
  1427. 'id' => 3,
  1428. 'apple_id' => 4,
  1429. 'name' => 'sample3',
  1430. 'Apple' => array(
  1431. 'id' => 4,
  1432. 'apple_id' => 2,
  1433. 'color' => 'Blue Green',
  1434. 'name' => 'Test Name',
  1435. 'created' => '2006-12-25 05:23:36',
  1436. 'date' => '2006-12-25',
  1437. 'modified' => '2006-12-25 05:23:36',
  1438. 'mytime' => '22:57:17'
  1439. )),
  1440. 'Child' => array(
  1441. array(
  1442. 'id' => 6,
  1443. 'apple_id' => 4,
  1444. 'color' => 'My new appleOrange',
  1445. 'name' => 'My new apple',
  1446. 'created' => '2006-12-25 05:29:39',
  1447. 'date' => '2006-12-25',
  1448. 'modified' => '2006-12-25 05:29:39',
  1449. 'mytime' => '22:57:17',
  1450. 'Parent' => array(
  1451. 'id' => 4,
  1452. 'apple_id' => 2,
  1453. 'color' => 'Blue Green',
  1454. 'name' => 'Test Name',
  1455. 'created' => '2006-12-25 05:23:36',
  1456. 'date' => '2006-12-25',
  1457. 'modified' => '2006-12-25 05:23:36',
  1458. 'mytime' => '22:57:17'
  1459. ),
  1460. 'Sample' => array(),
  1461. 'Child' => array(
  1462. array(
  1463. 'id' => 7,
  1464. 'apple_id' => 6,
  1465. 'color' => 'Some wierd color',
  1466. 'name' => 'Some odd color',
  1467. 'created' => '2006-12-25 05:34:21',
  1468. 'date' => '2006-12-25',
  1469. 'modified' => '2006-12-25 05:34:21',
  1470. 'mytime' => '22:57:17'
  1471. ))))),
  1472. array(
  1473. 'Apple' => array(
  1474. 'id' => 5,
  1475. 'apple_id' => 5,
  1476. 'color' => 'Green',
  1477. 'name' => 'Blue Green',
  1478. 'created' => '2006-12-25 05:24:06',
  1479. 'date' => '2006-12-25',
  1480. 'modified' => '2006-12-25 05:29:16',
  1481. 'mytime' => '22:57:17'
  1482. ),
  1483. 'Parent' => array(
  1484. 'id' => 5,
  1485. 'apple_id' => 5,
  1486. 'color' => 'Green',
  1487. 'name' => 'Blue Green',
  1488. 'created' => '2006-12-25 05:24:06',
  1489. 'date' => '2006-12-25',
  1490. 'modified' => '2006-12-25 05:29:16',
  1491. 'mytime' => '22:57:17',
  1492. 'Parent' => array(
  1493. 'id' => 5,
  1494. 'apple_id' => 5,
  1495. 'color' => 'Green',
  1496. 'name' => 'Blue Green',
  1497. 'created' => '2006-12-25 05:24:06',
  1498. 'date' => '2006-12-25',
  1499. 'modified' => '2006-12-25 05:29:16',
  1500. 'mytime' => '22:57:17'
  1501. ),
  1502. 'Child' => array(
  1503. array(
  1504. 'id' => 5,
  1505. 'apple_id' => 5,
  1506. 'color' => 'Green',
  1507. 'name' => 'Blue Green',
  1508. 'created' => '2006-12-25 05:24:06',
  1509. 'date' => '2006-12-25',
  1510. 'modified' => '2006-12-25 05:29:16',
  1511. 'mytime' => '22:57:17'
  1512. ))),
  1513. 'Sample' => array(
  1514. 'id' => 4,
  1515. 'apple_id' => 5,
  1516. 'name' => 'sample4',
  1517. 'Apple' => array(
  1518. 'id' => 5,
  1519. 'apple_id' => 5,
  1520. 'color' => 'Green',
  1521. 'name' => 'Blue Green',
  1522. 'created' => '2006-12-25 05:24:06',
  1523. 'date' => '2006-12-25',
  1524. 'modified' => '2006-12-25 05:29:16',
  1525. 'mytime' => '22:57:17'
  1526. )),
  1527. 'Child' => array(
  1528. array(
  1529. 'id' => 5,
  1530. 'apple_id' => 5,
  1531. 'color' => 'Green',
  1532. 'name' => 'Blue Green',
  1533. 'created' => '2006-12-25 05:24:06',
  1534. 'date' => '2006-12-25',
  1535. 'modified' => '2006-12-25 05:29:16',
  1536. 'mytime' => '22:57:17',
  1537. 'Parent' => array(
  1538. 'id' => 5,
  1539. 'apple_id' => 5,
  1540. 'color' => 'Green',
  1541. 'name' => 'Blue Green',
  1542. 'created' => '2006-12-25 05:24:06',
  1543. 'date' => '2006-12-25',
  1544. 'modified' => '2006-12-25 05:29:16',
  1545. 'mytime' => '22:57:17'
  1546. ),
  1547. 'Sample' => array(
  1548. 'id' => 4,
  1549. 'apple_id' => 5,
  1550. 'name' => 'sample4'
  1551. ),
  1552. 'Child' => array(
  1553. array(
  1554. 'id' => 5,
  1555. 'apple_id' => 5,
  1556. 'color' => 'Green',
  1557. 'name' => 'Blue Green',
  1558. 'created' => '2006-12-25 05:24:06',
  1559. 'date' => '2006-12-25',
  1560. 'modified' => '2006-12-25 05:29:16',
  1561. 'mytime' => '22:57:17'
  1562. ))))),
  1563. array(
  1564. 'Apple' => array(
  1565. 'id' => 6,
  1566. 'apple_id' => 4,
  1567. 'color' => 'My new appleOrange',
  1568. 'name' => 'My new apple',
  1569. 'created' => '2006-12-25 05:29:39',
  1570. 'date' => '2006-12-25',
  1571. 'modified' => '2006-12-25 05:29:39',
  1572. 'mytime' => '22:57:17'
  1573. ),
  1574. 'Parent' => array(
  1575. 'id' => 4,
  1576. 'apple_id' => 2,
  1577. 'color' => 'Blue Green',
  1578. 'name' => 'Test Name',
  1579. 'created' => '2006-12-25 05:23:36',
  1580. 'date' => '2006-12-25',
  1581. 'modified' => '2006-12-25 05:23:36',
  1582. 'mytime' => '22:57:17',
  1583. 'Parent' => array(
  1584. 'id' => 2,
  1585. 'apple_id' => 1,
  1586. 'color' => 'Bright Red 1',
  1587. 'name' => 'Bright Red Apple',
  1588. 'created' => '2006-11-22 10:43:13',
  1589. 'date' => '2014-01-01',
  1590. 'modified' => '2006-11-30 18:38:10',
  1591. 'mytime' => '22:57:17'
  1592. ),
  1593. 'Child' => array(
  1594. array(
  1595. 'id' => 6,
  1596. 'apple_id' => 4,
  1597. 'color' => 'My new appleOrange',
  1598. 'name' => 'My new apple',
  1599. 'created' => '2006-12-25 05:29:39',
  1600. 'date' => '2006-12-25',
  1601. 'modified' => '2006-12-25 05:29:39',
  1602. 'mytime' => '22:57:17'
  1603. ))),
  1604. 'Sample' => array(
  1605. 'id' => '',
  1606. 'apple_id' => '',
  1607. 'name' => ''
  1608. ),
  1609. 'Child' => array(
  1610. array(
  1611. 'id' => 7,
  1612. 'apple_id' => 6,
  1613. 'color' => 'Some wierd color',
  1614. 'name' => 'Some odd color',
  1615. 'created' => '2006-12-25 05:34:21',
  1616. 'date' => '2006-12-25',
  1617. 'modified' => '2006-12-25 05:34:21',
  1618. 'mytime' => '22:57:17',
  1619. 'Parent' => array(
  1620. 'id' => 6,
  1621. 'apple_id' => 4,
  1622. 'color' => 'My new appleOrange',
  1623. 'name' => 'My new apple',
  1624. 'created' => '2006-12-25 05:29:39',
  1625. 'date' => '2006-12-25',
  1626. 'modified' => '2006-12-25 05:29:39',
  1627. 'mytime' => '22:57:17'
  1628. ),
  1629. 'Sample' => array()
  1630. ))),
  1631. array(
  1632. 'Apple' => array(
  1633. 'id' => 7,
  1634. 'apple_id' => 6,
  1635. 'color' => 'Some wierd color',
  1636. 'name' => 'Some odd color',
  1637. 'created' => '2006-12-25 05:34:21',
  1638. 'date' => '2006-12-25',
  1639. 'modified' => '2006-12-25 05:34:21',
  1640. 'mytime' => '22:57:17'
  1641. ),
  1642. 'Parent' => array(
  1643. 'id' => 6,
  1644. 'apple_id' => 4,
  1645. 'color' => 'My new appleOrange',
  1646. 'name' => 'My new apple',
  1647. 'created' => '2006-12-25 05:29:39',
  1648. 'date' => '2006-12-25',
  1649. 'modified' => '2006-12-25 05:29:39',
  1650. 'mytime' => '22:57:17',
  1651. 'Parent' => array(
  1652. 'id' => 4,
  1653. 'apple_id' => 2,
  1654. 'color' => 'Blue Green',
  1655. 'name' => 'Test Name',
  1656. 'created' => '2006-12-25 05:23:36',
  1657. 'date' => '2006-12-25',
  1658. 'modified' => '2006-12-25 05:23:36',
  1659. 'mytime' => '22:57:17'
  1660. ),
  1661. 'Child' => array(
  1662. array(
  1663. 'id' => 7,
  1664. 'apple_id' => 6,
  1665. 'color' => 'Some wierd color',
  1666. 'name' => 'Some odd color',
  1667. 'created' => '2006-12-25 05:34:21',
  1668. 'date' => '2006-12-25',
  1669. 'modified' => '2006-12-25 05:34:21',
  1670. 'mytime' => '22:57:17'
  1671. ))),
  1672. 'Sample' => array(
  1673. 'id' => '',
  1674. 'apple_id' => '',
  1675. 'name' => ''
  1676. ),
  1677. 'Child' => array()
  1678. ));
  1679. $this->assertEquals($expected, $result);
  1680. $result = $TestModel->Parent->unbindModel(array('hasOne' => array('Sample')));
  1681. $this->assertTrue($result);
  1682. $result = $TestModel->unbindModel(array('hasMany' => array('Child')));
  1683. $this->assertTrue($result);
  1684. $result = $TestModel->find('all');
  1685. $expected = array(
  1686. array(
  1687. 'Apple' => array(
  1688. 'id' => 1,
  1689. 'apple_id' => 2,
  1690. 'color' => 'Red 1',
  1691. 'name' => 'Red Apple 1',
  1692. 'created' => '2006-11-22 10:38:58',
  1693. 'date' => '1951-01-04',
  1694. 'modified' => '2006-12-01 13:31:26',
  1695. 'mytime' => '22:57:17'
  1696. ),
  1697. 'Parent' => array(
  1698. 'id' => 2,
  1699. 'apple_id' => 1,
  1700. 'color' => 'Bright Red 1',
  1701. 'name' => 'Bright Red Apple',
  1702. 'created' => '2006-11-22 10:43:13',
  1703. 'date' => '2014-01-01',
  1704. 'modified' => '2006-11-30 18:38:10',
  1705. 'mytime' => '22:57:17',
  1706. 'Parent' => array(
  1707. 'id' => 1,
  1708. 'apple_id' => 2,
  1709. 'color' => 'Red 1',
  1710. 'name' => 'Red Apple 1',
  1711. 'created' => '2006-11-22 10:38:58',
  1712. 'date' => '1951-01-04',
  1713. 'modified' => '2006-12-01 13:31:26',
  1714. 'mytime' => '22:57:17'
  1715. ),
  1716. 'Child' => array(
  1717. array(
  1718. 'id' => 1,
  1719. 'apple_id' => 2,
  1720. 'color' => 'Red 1',
  1721. 'name' => 'Red Apple 1',
  1722. 'created' => '2006-11-22 10:38:58',
  1723. 'date' => '1951-01-04',
  1724. 'modified' => '2006-12-01 13:31:26',
  1725. 'mytime' => '22:57:17'
  1726. ),
  1727. array(
  1728. 'id' => 3,
  1729. 'apple_id' => 2,
  1730. 'color' => 'blue green',
  1731. 'name' => 'green blue',
  1732. 'created' => '2006-12-25 05:13:36',
  1733. 'date' => '2006-12-25',
  1734. 'modified' => '2006-12-25 05:23:24',
  1735. 'mytime' => '22:57:17'
  1736. ),
  1737. array(
  1738. 'id' => 4,
  1739. 'apple_id' => 2,
  1740. 'color' => 'Blue Green',
  1741. 'name' => 'Test Name',
  1742. 'created' => '2006-12-25 05:23:36',
  1743. 'date' => '2006-12-25',
  1744. 'modified' => '2006-12-25 05:23:36',
  1745. 'mytime' => '22:57:17'
  1746. ))),
  1747. 'Sample' => array(
  1748. 'id' => '',
  1749. 'apple_id' => '',
  1750. 'name' => ''
  1751. )),
  1752. array(
  1753. 'Apple' => array(
  1754. 'id' => 2,
  1755. 'apple_id' => 1,
  1756. 'color' => 'Bright Red 1',
  1757. 'name' => 'Bright Red Apple',
  1758. 'created' => '2006-11-22 10:43:13',
  1759. 'date' => '2014-01-01',
  1760. 'modified' => '2006-11-30 18:38:10',
  1761. 'mytime' => '22:57:17'
  1762. ),
  1763. 'Parent' => array(
  1764. 'id' => 1,
  1765. 'apple_id' => 2,
  1766. 'color' => 'Red 1',
  1767. 'name' => 'Red Apple 1',
  1768. 'created' => '2006-11-22 10:38:58',
  1769. 'date' => '1951-01-04',
  1770. 'modified' => '2006-12-01 13:31:26',
  1771. 'mytime' => '22:57:17',
  1772. 'Parent' => array(
  1773. 'id' => 2,
  1774. 'apple_id' => 1,
  1775. 'color' => 'Bright Red 1',
  1776. 'name' => 'Bright Red Apple',
  1777. 'created' => '2006-11-22 10:43:13',
  1778. 'date' => '2014-01-01',
  1779. 'modified' => '2006-11-30 18:38:10',
  1780. 'mytime' => '22:57:17'
  1781. ),
  1782. 'Child' => array(
  1783. array(
  1784. 'id' => 2,
  1785. 'apple_id' => 1,
  1786. 'color' => 'Bright Red 1',
  1787. 'name' => 'Bright Red Apple',
  1788. 'created' => '2006-11-22 10:43:13',
  1789. 'date' => '2014-01-01',
  1790. 'modified' => '2006-11-30 18:38:10',
  1791. 'mytime' => '22:57:17'
  1792. ))),
  1793. 'Sample' => array(
  1794. 'id' => 2,
  1795. 'apple_id' => 2,
  1796. 'name' => 'sample2',
  1797. 'Apple' => array(
  1798. 'id' => 2,
  1799. 'apple_id' => 1,
  1800. 'color' => 'Bright Red 1',
  1801. 'name' => 'Bright Red Apple',
  1802. 'created' => '2006-11-22 10:43:13',
  1803. 'date' => '2014-01-01',
  1804. 'modified' => '2006-11-30 18:38:10',
  1805. 'mytime' => '22:57:17'
  1806. ))),
  1807. array(
  1808. 'Apple' => array(
  1809. 'id' => 3,
  1810. 'apple_id' => 2,
  1811. 'color' => 'blue green',
  1812. 'name' => 'green blue',
  1813. 'created' => '2006-12-25 05:13:36',
  1814. 'date' => '2006-12-25',
  1815. 'modified' => '2006-12-25 05:23:24',
  1816. 'mytime' => '22:57:17'
  1817. ),
  1818. 'Parent' => array(
  1819. 'id' => 2,
  1820. 'apple_id' => 1,
  1821. 'color' => 'Bright Red 1',
  1822. 'name' => 'Bright Red Apple',
  1823. 'created' => '2006-11-22 10:43:13',
  1824. 'date' => '2014-01-01',
  1825. 'modified' => '2006-11-30 18:38:10',
  1826. 'mytime' => '22:57:17',
  1827. 'Parent' => array(
  1828. 'id' => 1,
  1829. 'apple_id' => 2,
  1830. 'color' => 'Red 1',
  1831. 'name' => 'Red Apple 1',
  1832. 'created' => '2006-11-22 10:38:58',
  1833. 'date' => '1951-01-04',
  1834. 'modified' => '2006-12-01 13:31:26',
  1835. 'mytime' => '22:57:17'
  1836. ),
  1837. 'Child' => array(
  1838. array(
  1839. 'id' => 1,
  1840. 'apple_id' => 2,
  1841. 'color' => 'Red 1',
  1842. 'name' => 'Red Apple 1',
  1843. 'created' => '2006-11-22 10:38:58',
  1844. 'date' => '1951-01-04',
  1845. 'modified' => '2006-12-01 13:31:26',
  1846. 'mytime' => '22:57:17'
  1847. ),
  1848. array(
  1849. 'id' => 3,
  1850. 'apple_id' => 2,
  1851. 'color' => 'blue green',
  1852. 'name' => 'green blue',
  1853. 'created' => '2006-12-25 05:13:36',
  1854. 'date' => '2006-12-25',
  1855. 'modified' => '2006-12-25 05:23:24',
  1856. 'mytime' => '22:57:17'
  1857. ),
  1858. array(
  1859. 'id' => 4,
  1860. 'apple_id' => 2,
  1861. 'color' => 'Blue Green',
  1862. 'name' => 'Test Name',
  1863. 'created' => '2006-12-25 05:23:36',
  1864. 'date' => '2006-12-25',
  1865. 'modified' => '2006-12-25 05:23:36',
  1866. 'mytime' => '22:57:17'
  1867. ))),
  1868. 'Sample' => array(
  1869. 'id' => 1,
  1870. 'apple_id' => 3,
  1871. 'name' => 'sample1',
  1872. 'Apple' => array(
  1873. 'id' => 3,
  1874. 'apple_id' => 2,
  1875. 'color' => 'blue green',
  1876. 'name' => 'green blue',
  1877. 'created' => '2006-12-25 05:13:36',
  1878. 'date' => '2006-12-25',
  1879. 'modified' => '2006-12-25 05:23:24',
  1880. 'mytime' => '22:57:17'
  1881. ))),
  1882. array(
  1883. 'Apple' => array(
  1884. 'id' => 4,
  1885. 'apple_id' => 2,
  1886. 'color' => 'Blue Green',
  1887. 'name' => 'Test Name',
  1888. 'created' => '2006-12-25 05:23:36',
  1889. 'date' => '2006-12-25',
  1890. 'modified' => '2006-12-25 05:23:36',
  1891. 'mytime' => '22:57:17'
  1892. ),
  1893. 'Parent' => array(
  1894. 'id' => 2,
  1895. 'apple_id' => 1,
  1896. 'color' => 'Bright Red 1',
  1897. 'name' => 'Bright Red Apple',
  1898. 'created' => '2006-11-22 10:43:13',
  1899. 'date' => '2014-01-01',
  1900. 'modified' => '2006-11-30 18:38:10',
  1901. 'mytime' => '22:57:17',
  1902. 'Parent' => array(
  1903. 'id' => 1,
  1904. 'apple_id' => 2,
  1905. 'color' => 'Red 1',
  1906. 'name' => 'Red Apple 1',
  1907. 'created' => '2006-11-22 10:38:58',
  1908. 'date' => '1951-01-04',
  1909. 'modified' => '2006-12-01 13:31:26',
  1910. 'mytime' => '22:57:17'
  1911. ),
  1912. 'Child' => array(
  1913. array(
  1914. 'id' => 1,
  1915. 'apple_id' => 2,
  1916. 'color' => 'Red 1',
  1917. 'name' => 'Red Apple 1',
  1918. 'created' => '2006-11-22 10:38:58',
  1919. 'date' => '1951-01-04',
  1920. 'modified' => '2006-12-01 13:31:26',
  1921. 'mytime' => '22:57:17'
  1922. ),
  1923. array(
  1924. 'id' => 3,
  1925. 'apple_id' => 2,
  1926. 'color' => 'blue green',
  1927. 'name' => 'green blue',
  1928. 'created' => '2006-12-25 05:13:36',
  1929. 'date' => '2006-12-25',
  1930. 'modified' => '2006-12-25 05:23:24',
  1931. 'mytime' => '22:57:17'
  1932. ),
  1933. array(
  1934. 'id' => 4,
  1935. 'apple_id' => 2,
  1936. 'color' => 'Blue Green',
  1937. 'name' => 'Test Name',
  1938. 'created' => '2006-12-25 05:23:36',
  1939. 'date' => '2006-12-25',
  1940. 'modified' => '2006-12-25 05:23:36',
  1941. 'mytime' => '22:57:17'
  1942. ))),
  1943. 'Sample' => array(
  1944. 'id' => 3,
  1945. 'apple_id' => 4,
  1946. 'name' => 'sample3',
  1947. 'Apple' => array(
  1948. 'id' => 4,
  1949. 'apple_id' => 2,
  1950. 'color' => 'Blue Green',
  1951. 'name' => 'Test Name',
  1952. 'created' => '2006-12-25 05:23:36',
  1953. 'date' => '2006-12-25',
  1954. 'modified' => '2006-12-25 05:23:36',
  1955. 'mytime' => '22:57:17'
  1956. ))),
  1957. array(
  1958. 'Apple' => array(
  1959. 'id' => 5,
  1960. 'apple_id' => 5,
  1961. 'color' => 'Green',
  1962. 'name' => 'Blue Green',
  1963. 'created' => '2006-12-25 05:24:06',
  1964. 'date' => '2006-12-25',
  1965. 'modified' => '2006-12-25 05:29:16',
  1966. 'mytime' => '22:57:17'
  1967. ),
  1968. 'Parent' => array(
  1969. 'id' => 5,
  1970. 'apple_id' => 5,
  1971. 'color' => 'Green',
  1972. 'name' => 'Blue Green',
  1973. 'created' => '2006-12-25 05:24:06',
  1974. 'date' => '2006-12-25',
  1975. 'modified' => '2006-12-25 05:29:16',
  1976. 'mytime' => '22:57:17',
  1977. 'Parent' => array(
  1978. 'id' => 5,
  1979. 'apple_id' => 5,
  1980. 'color' => 'Green',
  1981. 'name' => 'Blue Green',
  1982. 'created' => '2006-12-25 05:24:06',
  1983. 'date' => '2006-12-25',
  1984. 'modified' => '2006-12-25 05:29:16',
  1985. 'mytime' => '22:57:17'
  1986. ),
  1987. 'Child' => array(
  1988. array(
  1989. 'id' => 5,
  1990. 'apple_id' => 5,
  1991. 'color' => 'Green',
  1992. 'name' => 'Blue Green',
  1993. 'created' => '2006-12-25 05:24:06',
  1994. 'date' => '2006-12-25',
  1995. 'modified' => '2006-12-25 05:29:16',
  1996. 'mytime' => '22:57:17'
  1997. ))),
  1998. 'Sample' => array(
  1999. 'id' => 4,
  2000. 'apple_id' => 5,
  2001. 'name' => 'sample4',
  2002. 'Apple' => array(
  2003. 'id' => 5,
  2004. 'apple_id' => 5,
  2005. 'color' => 'Green',
  2006. 'name' => 'Blue Green',
  2007. 'created' => '2006-12-25 05:24:06',
  2008. 'date' => '2006-12-25',
  2009. 'modified' => '2006-12-25 05:29:16',
  2010. 'mytime' => '22:57:17'
  2011. ))),
  2012. array(
  2013. 'Apple' => array(
  2014. 'id' => 6,
  2015. 'apple_id' => 4,
  2016. 'color' => 'My new appleOrange',
  2017. 'name' => 'My new apple',
  2018. 'created' => '2006-12-25 05:29:39',
  2019. 'date' => '2006-12-25',
  2020. 'modified' => '2006-12-25 05:29:39',
  2021. 'mytime' => '22:57:17'
  2022. ),
  2023. 'Parent' => array(
  2024. 'id' => 4,
  2025. 'apple_id' => 2,
  2026. 'color' => 'Blue Green',
  2027. 'name' => 'Test Name',
  2028. 'created' => '2006-12-25 05:23:36',
  2029. 'date' => '2006-12-25',
  2030. 'modified' => '2006-12-25 05:23:36',
  2031. 'mytime' => '22:57:17',
  2032. 'Parent' => array(
  2033. 'id' => 2,
  2034. 'apple_id' => 1,
  2035. 'color' => 'Bright Red 1',
  2036. 'name' => 'Bright Red Apple',
  2037. 'created' => '2006-11-22 10:43:13',
  2038. 'date' => '2014-01-01',
  2039. 'modified' => '2006-11-30 18:38:10',
  2040. 'mytime' => '22:57:17'
  2041. ),
  2042. 'Child' => array(
  2043. array(
  2044. 'id' => 6,
  2045. 'apple_id' => 4,
  2046. 'color' => 'My new appleOrange',
  2047. 'name' => 'My new apple',
  2048. 'created' => '2006-12-25 05:29:39',
  2049. 'date' => '2006-12-25',
  2050. 'modified' => '2006-12-25 05:29:39',
  2051. 'mytime' => '22:57:17'
  2052. ))),
  2053. 'Sample' => array(
  2054. 'id' => '',
  2055. 'apple_id' => '',
  2056. 'name' => ''
  2057. )),
  2058. array(
  2059. 'Apple' => array(
  2060. 'id' => 7,
  2061. 'apple_id' => 6,
  2062. 'color' => 'Some wierd color',
  2063. 'name' => 'Some odd color',
  2064. 'created' => '2006-12-25 05:34:21',
  2065. 'date' => '2006-12-25',
  2066. 'modified' => '2006-12-25 05:34:21',
  2067. 'mytime' => '22:57:17'
  2068. ),
  2069. 'Parent' => array(
  2070. 'id' => 6,
  2071. 'apple_id' => 4,
  2072. 'color' => 'My new appleOrange',
  2073. 'name' => 'My new apple',
  2074. 'created' => '2006-12-25 05:29:39',
  2075. 'date' => '2006-12-25',
  2076. 'modified' => '2006-12-25 05:29:39',
  2077. 'mytime' => '22:57:17',
  2078. 'Parent' => array(
  2079. 'id' => 4,
  2080. 'apple_id' => 2,
  2081. 'color' => 'Blue Green',
  2082. 'name' => 'Test Name',
  2083. 'created' => '2006-12-25 05:23:36',
  2084. 'date' => '2006-12-25',
  2085. 'modified' => '2006-12-25 05:23:36',
  2086. 'mytime' => '22:57:17'
  2087. ),
  2088. 'Child' => array(
  2089. array(
  2090. 'id' => 7,
  2091. 'apple_id' => 6,
  2092. 'color' => 'Some wierd color',
  2093. 'name' => 'Some odd color',
  2094. 'created' => '2006-12-25 05:34:21',
  2095. 'date' => '2006-12-25',
  2096. 'modified' => '2006-12-25 05:34:21',
  2097. 'mytime' => '22:57:17'
  2098. ))),
  2099. 'Sample' => array(
  2100. 'id' => '',
  2101. 'apple_id' => '',
  2102. 'name' => ''
  2103. )));
  2104. $this->assertEquals($expected, $result);
  2105. $result = $TestModel->unbindModel(array('hasMany' => array('Child')));
  2106. $this->assertTrue($result);
  2107. $result = $TestModel->Sample->unbindModel(array('belongsTo' => array('Apple')));
  2108. $this->assertTrue($result);
  2109. $result = $TestModel->find('all');
  2110. $expected = array(
  2111. array(
  2112. 'Apple' => array(
  2113. 'id' => 1,
  2114. 'apple_id' => 2,
  2115. 'color' => 'Red 1',
  2116. 'name' => 'Red Apple 1',
  2117. 'created' => '2006-11-22 10:38:58',
  2118. 'date' => '1951-01-04',
  2119. 'modified' => '2006-12-01 13:31:26',
  2120. 'mytime' => '22:57:17'
  2121. ),
  2122. 'Parent' => array(
  2123. 'id' => 2,
  2124. 'apple_id' => 1,
  2125. 'color' => 'Bright Red 1',
  2126. 'name' => 'Bright Red Apple',
  2127. 'created' => '2006-11-22 10:43:13',
  2128. 'date' => '2014-01-01',
  2129. 'modified' => '2006-11-30 18:38:10',
  2130. 'mytime' => '22:57:17',
  2131. 'Parent' => array(
  2132. 'id' => 1,
  2133. 'apple_id' => 2,
  2134. 'color' => 'Red 1',
  2135. 'name' => 'Red Apple 1',
  2136. 'created' => '2006-11-22 10:38:58',
  2137. 'date' => '1951-01-04',
  2138. 'modified' => '2006-12-01 13:31:26',
  2139. 'mytime' => '22:57:17'
  2140. ),
  2141. 'Sample' => array(
  2142. 'id' => 2,
  2143. 'apple_id' => 2,
  2144. 'name' => 'sample2'
  2145. ),
  2146. 'Child' => array(
  2147. array(
  2148. 'id' => 1,
  2149. 'apple_id' => 2,
  2150. 'color' => 'Red 1',
  2151. 'name' => 'Red Apple 1',
  2152. 'created' => '2006-11-22 10:38:58',
  2153. 'date' => '1951-01-04',
  2154. 'modified' => '2006-12-01 13:31:26',
  2155. 'mytime' => '22:57:17'
  2156. ),
  2157. array(
  2158. 'id' => 3,
  2159. 'apple_id' => 2,
  2160. 'color' => 'blue green',
  2161. 'name' => 'green blue',
  2162. 'created' => '2006-12-25 05:13:36',
  2163. 'date' => '2006-12-25',
  2164. 'modified' => '2006-12-25 05:23:24',
  2165. 'mytime' => '22:57:17'
  2166. ),
  2167. array(
  2168. 'id' => 4,
  2169. 'apple_id' => 2,
  2170. 'color' => 'Blue Green',
  2171. 'name' => 'Test Name',
  2172. 'created' => '2006-12-25 05:23:36',
  2173. 'date' => '2006-12-25',
  2174. 'modified' => '2006-12-25 05:23:36',
  2175. 'mytime' => '22:57:17'
  2176. ))),
  2177. 'Sample' => array(
  2178. 'id' => '',
  2179. 'apple_id' => '',
  2180. 'name' => ''
  2181. )),
  2182. array(
  2183. 'Apple' => array(
  2184. 'id' => 2,
  2185. 'apple_id' => 1,
  2186. 'color' => 'Bright Red 1',
  2187. 'name' => 'Bright Red Apple',
  2188. 'created' => '2006-11-22 10:43:13',
  2189. 'date' => '2014-01-01',
  2190. 'modified' => '2006-11-30 18:38:10',
  2191. 'mytime' => '22:57:17'
  2192. ),
  2193. 'Parent' => array(
  2194. 'id' => 1,
  2195. 'apple_id' => 2,
  2196. 'color' => 'Red 1',
  2197. 'name' => 'Red Apple 1',
  2198. 'created' => '2006-11-22 10:38:58',
  2199. 'date' => '1951-01-04',
  2200. 'modified' => '2006-12-01 13:31:26',
  2201. 'mytime' => '22:57:17',
  2202. 'Parent' => array(
  2203. 'id' => 2,
  2204. 'apple_id' => 1,
  2205. 'color' => 'Bright Red 1',
  2206. 'name' => 'Bright Red Apple',
  2207. 'created' => '2006-11-22 10:43:13',
  2208. 'date' => '2014-01-01',
  2209. 'modified' => '2006-11-30 18:38:10',
  2210. 'mytime' => '22:57:17'
  2211. ),
  2212. 'Sample' => array(),
  2213. 'Child' => array(
  2214. array(
  2215. 'id' => 2,
  2216. 'apple_id' => 1,
  2217. 'color' => 'Bright Red 1',
  2218. 'name' => 'Bright Red Apple',
  2219. 'created' => '2006-11-22 10:43:13',
  2220. 'date' => '2014-01-01',
  2221. 'modified' => '2006-11-30 18:38:10',
  2222. 'mytime' => '22:57:17'
  2223. ))),
  2224. 'Sample' => array(
  2225. 'id' => 2,
  2226. 'apple_id' => 2,
  2227. 'name' => 'sample2'
  2228. )),
  2229. array(
  2230. 'Apple' => array(
  2231. 'id' => 3,
  2232. 'apple_id' => 2,
  2233. 'color' => 'blue green',
  2234. 'name' => 'green blue',
  2235. 'created' => '2006-12-25 05:13:36',
  2236. 'date' => '2006-12-25',
  2237. 'modified' => '2006-12-25 05:23:24',
  2238. 'mytime' => '22:57:17'
  2239. ),
  2240. 'Parent' => array(
  2241. 'id' => 2,
  2242. 'apple_id' => 1,
  2243. 'color' => 'Bright Red 1',
  2244. 'name' => 'Bright Red Apple',
  2245. 'created' => '2006-11-22 10:43:13',
  2246. 'date' => '2014-01-01',
  2247. 'modified' => '2006-11-30 18:38:10',
  2248. 'mytime' => '22:57:17',
  2249. 'Parent' => array(
  2250. 'id' => 1,
  2251. 'apple_id' => 2,
  2252. 'color' => 'Red 1',
  2253. 'name' => 'Red Apple 1',
  2254. 'created' => '2006-11-22 10:38:58',
  2255. 'date' => '1951-01-04',
  2256. 'modified' => '2006-12-01 13:31:26',
  2257. 'mytime' => '22:57:17'
  2258. ),
  2259. 'Sample' => array(
  2260. 'id' => 2,
  2261. 'apple_id' => 2,
  2262. 'name' => 'sample2'
  2263. ),
  2264. 'Child' => array(
  2265. array(
  2266. 'id' => 1,
  2267. 'apple_id' => 2,
  2268. 'color' => 'Red 1',
  2269. 'name' => 'Red Apple 1',
  2270. 'created' => '2006-11-22 10:38:58',
  2271. 'date' => '1951-01-04',
  2272. 'modified' => '2006-12-01 13:31:26',
  2273. 'mytime' => '22:57:17'
  2274. ),
  2275. array(
  2276. 'id' => 3,
  2277. 'apple_id' => 2,
  2278. 'color' => 'blue green',
  2279. 'name' => 'green blue',
  2280. 'created' => '2006-12-25 05:13:36',
  2281. 'date' => '2006-12-25',
  2282. 'modified' => '2006-12-25 05:23:24',
  2283. 'mytime' => '22:57:17'
  2284. ),
  2285. array(
  2286. 'id' => 4,
  2287. 'apple_id' => 2,
  2288. 'color' => 'Blue Green',
  2289. 'name' => 'Test Name',
  2290. 'created' => '2006-12-25 05:23:36',
  2291. 'date' => '2006-12-25',
  2292. 'modified' => '2006-12-25 05:23:36',
  2293. 'mytime' => '22:57:17'
  2294. ))),
  2295. 'Sample' => array(
  2296. 'id' => 1,
  2297. 'apple_id' => 3,
  2298. 'name' => 'sample1'
  2299. )),
  2300. array(
  2301. 'Apple' => array(
  2302. 'id' => 4,
  2303. 'apple_id' => 2,
  2304. 'color' => 'Blue Green',
  2305. 'name' => 'Test Name',
  2306. 'created' => '2006-12-25 05:23:36',
  2307. 'date' => '2006-12-25',
  2308. 'modified' => '2006-12-25 05:23:36',
  2309. 'mytime' => '22:57:17'
  2310. ),
  2311. 'Parent' => array(
  2312. 'id' => 2,
  2313. 'apple_id' => 1,
  2314. 'color' => 'Bright Red 1',
  2315. 'name' => 'Bright Red Apple',
  2316. 'created' => '2006-11-22 10:43:13',
  2317. 'date' => '2014-01-01',
  2318. 'modified' => '2006-11-30 18:38:10',
  2319. 'mytime' => '22:57:17',
  2320. 'Parent' => array(
  2321. 'id' => 1,
  2322. 'apple_id' => 2,
  2323. 'color' => 'Red 1',
  2324. 'name' => 'Red Apple 1',
  2325. 'created' => '2006-11-22 10:38:58',
  2326. 'date' => '1951-01-04',
  2327. 'modified' => '2006-12-01 13:31:26',
  2328. 'mytime' => '22:57:17'
  2329. ),
  2330. 'Sample' => array(
  2331. 'id' => 2,
  2332. 'apple_id' => 2,
  2333. 'name' => 'sample2'
  2334. ),
  2335. 'Child' => array(
  2336. array(
  2337. 'id' => 1,
  2338. 'apple_id' => 2,
  2339. 'color' => 'Red 1',
  2340. 'name' => 'Red Apple 1',
  2341. 'created' => '2006-11-22 10:38:58',
  2342. 'date' => '1951-01-04',
  2343. 'modified' => '2006-12-01 13:31:26',
  2344. 'mytime' => '22:57:17'
  2345. ),
  2346. array(
  2347. 'id' => 3,
  2348. 'apple_id' => 2,
  2349. 'color' => 'blue green',
  2350. 'name' => 'green blue',
  2351. 'created' => '2006-12-25 05:13:36',
  2352. 'date' => '2006-12-25',
  2353. 'modified' => '2006-12-25 05:23:24',
  2354. 'mytime' => '22:57:17'
  2355. ),
  2356. array(
  2357. 'id' => 4,
  2358. 'apple_id' => 2,
  2359. 'color' => 'Blue Green',
  2360. 'name' => 'Test Name',
  2361. 'created' => '2006-12-25 05:23:36',
  2362. 'date' => '2006-12-25',
  2363. 'modified' => '2006-12-25 05:23:36',
  2364. 'mytime' => '22:57:17'
  2365. ))),
  2366. 'Sample' => array(
  2367. 'id' => 3,
  2368. 'apple_id' => 4,
  2369. 'name' => 'sample3'
  2370. )),
  2371. array(
  2372. 'Apple' => array(
  2373. 'id' => 5,
  2374. 'apple_id' => 5,
  2375. 'color' => 'Green',
  2376. 'name' => 'Blue Green',
  2377. 'created' => '2006-12-25 05:24:06',
  2378. 'date' => '2006-12-25',
  2379. 'modified' => '2006-12-25 05:29:16',
  2380. 'mytime' => '22:57:17'
  2381. ),
  2382. 'Parent' => array(
  2383. 'id' => 5,
  2384. 'apple_id' => 5,
  2385. 'color' => 'Green',
  2386. 'name' => 'Blue Green',
  2387. 'created' => '2006-12-25 05:24:06',
  2388. 'date' => '2006-12-25',
  2389. 'modified' => '2006-12-25 05:29:16',
  2390. 'mytime' => '22:57:17',
  2391. 'Parent' => array(
  2392. 'id' => 5,
  2393. 'apple_id' => 5,
  2394. 'color' => 'Green',
  2395. 'name' => 'Blue Green',
  2396. 'created' => '2006-12-25 05:24:06',
  2397. 'date' => '2006-12-25',
  2398. 'modified' => '2006-12-25 05:29:16',
  2399. 'mytime' => '22:57:17'
  2400. ),
  2401. 'Sample' => array(
  2402. 'id' => 4,
  2403. 'apple_id' => 5,
  2404. 'name' => 'sample4'
  2405. ),
  2406. 'Child' => array(
  2407. array(
  2408. 'id' => 5,
  2409. 'apple_id' => 5,
  2410. 'color' => 'Green',
  2411. 'name' => 'Blue Green',
  2412. 'created' => '2006-12-25 05:24:06',
  2413. 'date' => '2006-12-25',
  2414. 'modified' => '2006-12-25 05:29:16',
  2415. 'mytime' => '22:57:17'
  2416. ))),
  2417. 'Sample' => array(
  2418. 'id' => 4,
  2419. 'apple_id' => 5,
  2420. 'name' => 'sample4'
  2421. )),
  2422. array(
  2423. 'Apple' => array(
  2424. 'id' => 6,
  2425. 'apple_id' => 4,
  2426. 'color' => 'My new appleOrange',
  2427. 'name' => 'My new apple',
  2428. 'created' => '2006-12-25 05:29:39',
  2429. 'date' => '2006-12-25',
  2430. 'modified' => '2006-12-25 05:29:39',
  2431. 'mytime' => '22:57:17'
  2432. ),
  2433. 'Parent' => array(
  2434. 'id' => 4,
  2435. 'apple_id' => 2,
  2436. 'color' => 'Blue Green',
  2437. 'name' => 'Test Name',
  2438. 'created' => '2006-12-25 05:23:36',
  2439. 'date' => '2006-12-25',
  2440. 'modified' => '2006-12-25 05:23:36',
  2441. 'mytime' => '22:57:17',
  2442. 'Parent' => array(
  2443. 'id' => 2,
  2444. 'apple_id' => 1,
  2445. 'color' => 'Bright Red 1',
  2446. 'name' => 'Bright Red Apple',
  2447. 'created' => '2006-11-22 10:43:13',
  2448. 'date' => '2014-01-01',
  2449. 'modified' => '2006-11-30 18:38:10',
  2450. 'mytime' => '22:57:17'
  2451. ),
  2452. 'Sample' => array(
  2453. 'id' => 3,
  2454. 'apple_id' => 4,
  2455. 'name' => 'sample3'
  2456. ),
  2457. 'Child' => array(
  2458. array(
  2459. 'id' => 6,
  2460. 'apple_id' => 4,
  2461. 'color' => 'My new appleOrange',
  2462. 'name' => 'My new apple',
  2463. 'created' => '2006-12-25 05:29:39',
  2464. 'date' => '2006-12-25',
  2465. 'modified' => '2006-12-25 05:29:39',
  2466. 'mytime' => '22:57:17'
  2467. ))),
  2468. 'Sample' => array(
  2469. 'id' => '',
  2470. 'apple_id' => '',
  2471. 'name' => ''
  2472. )),
  2473. array(
  2474. 'Apple' => array(
  2475. 'id' => 7,
  2476. 'apple_id' => 6,
  2477. 'color' => 'Some wierd color',
  2478. 'name' => 'Some odd color',
  2479. 'created' => '2006-12-25 05:34:21',
  2480. 'date' => '2006-12-25',
  2481. 'modified' => '2006-12-25 05:34:21',
  2482. 'mytime' => '22:57:17'
  2483. ),
  2484. 'Parent' => array(
  2485. 'id' => 6,
  2486. 'apple_id' => 4,
  2487. 'color' => 'My new appleOrange',
  2488. 'name' => 'My new apple',
  2489. 'created' => '2006-12-25 05:29:39',
  2490. 'date' => '2006-12-25',
  2491. 'modified' => '2006-12-25 05:29:39',
  2492. 'mytime' => '22:57:17',
  2493. 'Parent' => array(
  2494. 'id' => 4,
  2495. 'apple_id' => 2,
  2496. 'color' => 'Blue Green',
  2497. 'name' => 'Test Name',
  2498. 'created' => '2006-12-25 05:23:36',
  2499. 'date' => '2006-12-25',
  2500. 'modified' => '2006-12-25 05:23:36',
  2501. 'mytime' => '22:57:17'
  2502. ),
  2503. 'Sample' => array(),
  2504. 'Child' => array(
  2505. array(
  2506. 'id' => 7,
  2507. 'apple_id' => 6,
  2508. 'color' => 'Some wierd color',
  2509. 'name' => 'Some odd color',
  2510. 'created' => '2006-12-25 05:34:21',
  2511. 'date' => '2006-12-25',
  2512. 'modified' => '2006-12-25 05:34:21',
  2513. 'mytime' => '22:57:17'
  2514. ))),
  2515. 'Sample' => array(
  2516. 'id' => '',
  2517. 'apple_id' => '',
  2518. 'name' => ''
  2519. )));
  2520. $this->assertEquals($expected, $result);
  2521. $result = $TestModel->Parent->unbindModel(array('belongsTo' => array('Parent')));
  2522. $this->assertTrue($result);
  2523. $result = $TestModel->unbindModel(array('hasMany' => array('Child')));
  2524. $this->assertTrue($result);
  2525. $result = $TestModel->find('all');
  2526. $expected = array(
  2527. array(
  2528. 'Apple' => array(
  2529. 'id' => 1,
  2530. 'apple_id' => 2,
  2531. 'color' => 'Red 1',
  2532. 'name' => 'Red Apple 1',
  2533. 'created' => '2006-11-22 10:38:58',
  2534. 'date' => '1951-01-04',
  2535. 'modified' => '2006-12-01 13:31:26',
  2536. 'mytime' => '22:57:17'
  2537. ),
  2538. 'Parent' => array(
  2539. 'id' => 2,
  2540. 'apple_id' => 1,
  2541. 'color' => 'Bright Red 1',
  2542. 'name' => 'Bright Red Apple',
  2543. 'created' => '2006-11-22 10:43:13',
  2544. 'date' => '2014-01-01',
  2545. 'modified' => '2006-11-30 18:38:10',
  2546. 'mytime' => '22:57:17',
  2547. 'Sample' => array(
  2548. 'id' => 2,
  2549. 'apple_id' => 2,
  2550. 'name' => 'sample2'
  2551. ),
  2552. 'Child' => array(
  2553. array(
  2554. 'id' => 1,
  2555. 'apple_id' => 2,
  2556. 'color' => 'Red 1',
  2557. 'name' => 'Red Apple 1',
  2558. 'created' => '2006-11-22 10:38:58',
  2559. 'date' => '1951-01-04',
  2560. 'modified' => '2006-12-01 13:31:26',
  2561. 'mytime' => '22:57:17'
  2562. ),
  2563. array(
  2564. 'id' => 3,
  2565. 'apple_id' => 2,
  2566. 'color' => 'blue green',
  2567. 'name' => 'green blue',
  2568. 'created' => '2006-12-25 05:13:36',
  2569. 'date' => '2006-12-25',
  2570. 'modified' => '2006-12-25 05:23:24',
  2571. 'mytime' => '22:57:17'
  2572. ),
  2573. array(
  2574. 'id' => 4,
  2575. 'apple_id' => 2,
  2576. 'color' => 'Blue Green',
  2577. 'name' => 'Test Name',
  2578. 'created' => '2006-12-25 05:23:36',
  2579. 'date' => '2006-12-25',
  2580. 'modified' => '2006-12-25 05:23:36',
  2581. 'mytime' => '22:57:17'
  2582. ))),
  2583. 'Sample' => array(
  2584. 'id' => '',
  2585. 'apple_id' => '',
  2586. 'name' => ''
  2587. )),
  2588. array(
  2589. 'Apple' => array(
  2590. 'id' => 2,
  2591. 'apple_id' => 1,
  2592. 'color' => 'Bright Red 1',
  2593. 'name' => 'Bright Red Apple',
  2594. 'created' => '2006-11-22 10:43:13',
  2595. 'date' => '2014-01-01',
  2596. 'modified' => '2006-11-30 18:38:10',
  2597. 'mytime' => '22:57:17'
  2598. ),
  2599. 'Parent' => array(
  2600. 'id' => 1,
  2601. 'apple_id' => 2,
  2602. 'color' => 'Red 1',
  2603. 'name' => 'Red Apple 1',
  2604. 'created' => '2006-11-22 10:38:58',
  2605. 'date' => '1951-01-04',
  2606. 'modified' => '2006-12-01 13:31:26',
  2607. 'mytime' => '22:57:17',
  2608. 'Sample' => array(),
  2609. 'Child' => array(
  2610. array(
  2611. 'id' => 2,
  2612. 'apple_id' => 1,
  2613. 'color' => 'Bright Red 1',
  2614. 'name' => 'Bright Red Apple',
  2615. 'created' => '2006-11-22 10:43:13',
  2616. 'date' => '2014-01-01',
  2617. 'modified' => '2006-11-30 18:38:10',
  2618. 'mytime' => '22:57:17'
  2619. ))),
  2620. 'Sample' => array(
  2621. 'id' => 2,
  2622. 'apple_id' => 2,
  2623. 'name' => 'sample2',
  2624. 'Apple' => array(
  2625. 'id' => 2,
  2626. 'apple_id' => 1,
  2627. 'color' => 'Bright Red 1',
  2628. 'name' => 'Bright Red Apple',
  2629. 'created' => '2006-11-22 10:43:13',
  2630. 'date' => '2014-01-01',
  2631. 'modified' => '2006-11-30 18:38:10',
  2632. 'mytime' => '22:57:17'
  2633. ))),
  2634. array(
  2635. 'Apple' => array(
  2636. 'id' => 3,
  2637. 'apple_id' => 2,
  2638. 'color' => 'blue green',
  2639. 'name' => 'green blue',
  2640. 'created' => '2006-12-25 05:13:36',
  2641. 'date' => '2006-12-25',
  2642. 'modified' => '2006-12-25 05:23:24',
  2643. 'mytime' => '22:57:17'
  2644. ),
  2645. 'Parent' => array(
  2646. 'id' => 2,
  2647. 'apple_id' => 1,
  2648. 'color' => 'Bright Red 1',
  2649. 'name' => 'Bright Red Apple',
  2650. 'created' => '2006-11-22 10:43:13',
  2651. 'date' => '2014-01-01',
  2652. 'modified' => '2006-11-30 18:38:10',
  2653. 'mytime' => '22:57:17',
  2654. 'Sample' => array(
  2655. 'id' => 2,
  2656. 'apple_id' => 2,
  2657. 'name' => 'sample2'
  2658. ),
  2659. 'Child' => array(
  2660. array(
  2661. 'id' => 1,
  2662. 'apple_id' => 2,
  2663. 'color' => 'Red 1',
  2664. 'name' => 'Red Apple 1',
  2665. 'created' => '2006-11-22 10:38:58',
  2666. 'date' => '1951-01-04',
  2667. 'modified' => '2006-12-01 13:31:26',
  2668. 'mytime' => '22:57:17'
  2669. ),
  2670. array(
  2671. 'id' => 3,
  2672. 'apple_id' => 2,
  2673. 'color' => 'blue green',
  2674. 'name' => 'green blue',
  2675. 'created' => '2006-12-25 05:13:36',
  2676. 'date' => '2006-12-25',
  2677. 'modified' => '2006-12-25 05:23:24',
  2678. 'mytime' => '22:57:17'
  2679. ),
  2680. array(
  2681. 'id' => 4,
  2682. 'apple_id' => 2,
  2683. 'color' => 'Blue Green',
  2684. 'name' => 'Test Name',
  2685. 'created' => '2006-12-25 05:23:36',
  2686. 'date' => '2006-12-25',
  2687. 'modified' => '2006-12-25 05:23:36',
  2688. 'mytime' => '22:57:17'
  2689. ))),
  2690. 'Sample' => array(
  2691. 'id' => 1,
  2692. 'apple_id' => 3,
  2693. 'name' => 'sample1',
  2694. 'Apple' => array(
  2695. 'id' => 3,
  2696. 'apple_id' => 2,
  2697. 'color' => 'blue green',
  2698. 'name' => 'green blue',
  2699. 'created' => '2006-12-25 05:13:36',
  2700. 'date' => '2006-12-25',
  2701. 'modified' => '2006-12-25 05:23:24',
  2702. 'mytime' => '22:57:17'
  2703. ))),
  2704. array(
  2705. 'Apple' => array(
  2706. 'id' => 4,
  2707. 'apple_id' => 2,
  2708. 'color' => 'Blue Green',
  2709. 'name' => 'Test Name',
  2710. 'created' => '2006-12-25 05:23:36',
  2711. 'date' => '2006-12-25',
  2712. 'modified' => '2006-12-25 05:23:36',
  2713. 'mytime' => '22:57:17'
  2714. ),
  2715. 'Parent' => array(
  2716. 'id' => 2,
  2717. 'apple_id' => 1,
  2718. 'color' => 'Bright Red 1',
  2719. 'name' => 'Bright Red Apple',
  2720. 'created' => '2006-11-22 10:43:13',
  2721. 'date' => '2014-01-01',
  2722. 'modified' => '2006-11-30 18:38:10',
  2723. 'mytime' => '22:57:17',
  2724. 'Sample' => array(
  2725. 'id' => 2,
  2726. 'apple_id' => 2,
  2727. 'name' => 'sample2'
  2728. ),
  2729. 'Child' => array(
  2730. array(
  2731. 'id' => 1,
  2732. 'apple_id' => 2,
  2733. 'color' => 'Red 1',
  2734. 'name' => 'Red Apple 1',
  2735. 'created' => '2006-11-22 10:38:58',
  2736. 'date' => '1951-01-04',
  2737. 'modified' => '2006-12-01 13:31:26',
  2738. 'mytime' => '22:57:17'
  2739. ),
  2740. array(
  2741. 'id' => 3,
  2742. 'apple_id' => 2,
  2743. 'color' => 'blue green',
  2744. 'name' => 'green blue',
  2745. 'created' => '2006-12-25 05:13:36',
  2746. 'date' => '2006-12-25',
  2747. 'modified' => '2006-12-25 05:23:24',
  2748. 'mytime' => '22:57:17'
  2749. ),
  2750. array(
  2751. 'id' => 4,
  2752. 'apple_id' => 2,
  2753. 'color' => 'Blue Green',
  2754. 'name' => 'Test Name',
  2755. 'created' => '2006-12-25 05:23:36',
  2756. 'date' => '2006-12-25',
  2757. 'modified' => '2006-12-25 05:23:36',
  2758. 'mytime' => '22:57:17'
  2759. ))),
  2760. 'Sample' => array(
  2761. 'id' => 3,
  2762. 'apple_id' => 4,
  2763. 'name' => 'sample3',
  2764. 'Apple' => array(
  2765. 'id' => 4,
  2766. 'apple_id' => 2,
  2767. 'color' => 'Blue Green',
  2768. 'name' => 'Test Name',
  2769. 'created' => '2006-12-25 05:23:36',
  2770. 'date' => '2006-12-25',
  2771. 'modified' => '2006-12-25 05:23:36',
  2772. 'mytime' => '22:57:17'
  2773. ))),
  2774. array(
  2775. 'Apple' => array(
  2776. 'id' => 5,
  2777. 'apple_id' => 5,
  2778. 'color' => 'Green',
  2779. 'name' => 'Blue Green',
  2780. 'created' => '2006-12-25 05:24:06',
  2781. 'date' => '2006-12-25',
  2782. 'modified' =>
  2783. '2006-12-25 05:29:16',
  2784. 'mytime' => '22:57:17'
  2785. ),
  2786. 'Parent' => array(
  2787. 'id' => 5,
  2788. 'apple_id' => 5,
  2789. 'color' => 'Green',
  2790. 'name' => 'Blue Green',
  2791. 'created' => '2006-12-25 05:24:06',
  2792. 'date' => '2006-12-25',
  2793. 'modified' => '2006-12-25 05:29:16',
  2794. 'mytime' => '22:57:17',
  2795. 'Sample' => array(
  2796. 'id' => 4,
  2797. 'apple_id' => 5,
  2798. 'name' => 'sample4'
  2799. ),
  2800. 'Child' => array(
  2801. array(
  2802. 'id' => 5,
  2803. 'apple_id' => 5,
  2804. 'color' => 'Green',
  2805. 'name' => 'Blue Green',
  2806. 'created' => '2006-12-25 05:24:06',
  2807. 'date' => '2006-12-25',
  2808. 'modified' => '2006-12-25 05:29:16',
  2809. 'mytime' => '22:57:17'
  2810. ))),
  2811. 'Sample' => array(
  2812. 'id' => 4,
  2813. 'apple_id' => 5,
  2814. 'name' => 'sample4',
  2815. 'Apple' => array(
  2816. 'id' => 5,
  2817. 'apple_id' => 5,
  2818. 'color' => 'Green',
  2819. 'name' => 'Blue Green',
  2820. 'created' => '2006-12-25 05:24:06',
  2821. 'date' => '2006-12-25',
  2822. 'modified' => '2006-12-25 05:29:16',
  2823. 'mytime' => '22:57:17'
  2824. ))),
  2825. array(
  2826. 'Apple' => array(
  2827. 'id' => 6,
  2828. 'apple_id' => 4,
  2829. 'color' => 'My new appleOrange',
  2830. 'name' => 'My new apple',
  2831. 'created' => '2006-12-25 05:29:39',
  2832. 'date' => '2006-12-25',
  2833. 'modified' => '2006-12-25 05:29:39',
  2834. 'mytime' => '22:57:17'),
  2835. 'Parent' => array(
  2836. 'id' => 4,
  2837. 'apple_id' => 2,
  2838. 'color' => 'Blue Green',
  2839. 'name' => 'Test Name',
  2840. 'created' => '2006-12-25 05:23:36',
  2841. 'date' => '2006-12-25',
  2842. 'modified' => '2006-12-25 05:23:36',
  2843. 'mytime' => '22:57:17',
  2844. 'Sample' => array(
  2845. 'id' => 3,
  2846. 'apple_id' => 4,
  2847. 'name' => 'sample3'
  2848. ),
  2849. 'Child' => array(
  2850. array(
  2851. 'id' => 6,
  2852. 'apple_id' => 4,
  2853. 'color' => 'My new appleOrange',
  2854. 'name' => 'My new apple',
  2855. 'created' => '2006-12-25 05:29:39',
  2856. 'date' => '2006-12-25',
  2857. 'modified' => '2006-12-25 05:29:39',
  2858. 'mytime' => '22:57:17'
  2859. ))),
  2860. 'Sample' => array(
  2861. 'id' => '',
  2862. 'apple_id' => '',
  2863. 'name' => ''
  2864. )),
  2865. array(
  2866. 'Apple' => array(
  2867. 'id' => 7,
  2868. 'apple_id' => 6,
  2869. 'color' => 'Some wierd color',
  2870. 'name' => 'Some odd color',
  2871. 'created' => '2006-12-25 05:34:21',
  2872. 'date' => '2006-12-25',
  2873. 'modified' => '2006-12-25 05:34:21',
  2874. 'mytime' => '22:57:17'
  2875. ),
  2876. 'Parent' => array(
  2877. 'id' => 6,
  2878. 'apple_id' => 4,
  2879. 'color' => 'My new appleOrange',
  2880. 'name' => 'My new apple',
  2881. 'created' => '2006-12-25 05:29:39',
  2882. 'date' => '2006-12-25',
  2883. 'modified' => '2006-12-25 05:29:39',
  2884. 'mytime' => '22:57:17',
  2885. 'Sample' => array(),
  2886. 'Child' => array(
  2887. array(
  2888. 'id' => 7,
  2889. 'apple_id' => 6,
  2890. 'color' => 'Some wierd color',
  2891. 'name' => 'Some odd color',
  2892. 'created' => '2006-12-25 05:34:21',
  2893. 'date' => '2006-12-25', 'modified' =>
  2894. '2006-12-25 05:34:21',
  2895. 'mytime' => '22:57:17'
  2896. ))),
  2897. 'Sample' => array(
  2898. 'id' => '',
  2899. 'apple_id' => '',
  2900. 'name' => ''
  2901. )));
  2902. $this->assertEquals($expected, $result);
  2903. }
  2904. /**
  2905. * testSelfAssociationAfterFind method
  2906. *
  2907. * @return void
  2908. */
  2909. public function testSelfAssociationAfterFind() {
  2910. $this->loadFixtures('Apple', 'Sample');
  2911. $afterFindModel = new NodeAfterFind();
  2912. $afterFindModel->recursive = 3;
  2913. $afterFindData = $afterFindModel->find('all');
  2914. $duplicateModel = new NodeAfterFind();
  2915. $duplicateModel->recursive = 3;
  2916. $duplicateModelData = $duplicateModel->find('all');
  2917. $noAfterFindModel = new NodeNoAfterFind();
  2918. $noAfterFindModel->recursive = 3;
  2919. $noAfterFindData = $noAfterFindModel->find('all');
  2920. $this->assertFalse($afterFindModel == $noAfterFindModel);
  2921. // Limitation of PHP 4 and PHP 5 > 5.1.6 when comparing recursive objects
  2922. if (PHP_VERSION === '5.1.6') {
  2923. $this->assertFalse($afterFindModel != $duplicateModel);
  2924. }
  2925. $this->assertEquals($afterFindData, $noAfterFindData);
  2926. }
  2927. /**
  2928. * testFindAllThreaded method
  2929. *
  2930. * @return void
  2931. */
  2932. public function testFindAllThreaded() {
  2933. $this->loadFixtures('Category');
  2934. $TestModel = new Category();
  2935. $result = $TestModel->find('threaded');
  2936. $expected = array(
  2937. array(
  2938. 'Category' => array(
  2939. 'id' => '1',
  2940. 'parent_id' => '0',
  2941. 'name' => 'Category 1',
  2942. 'created' => '2007-03-18 15:30:23',
  2943. 'updated' => '2007-03-18 15:32:31'
  2944. ),
  2945. 'children' => array(
  2946. array(
  2947. 'Category' => array(
  2948. 'id' => '2',
  2949. 'parent_id' => '1',
  2950. 'name' => 'Category 1.1',
  2951. 'created' => '2007-03-18 15:30:23',
  2952. 'updated' => '2007-03-18 15:32:31'
  2953. ),
  2954. 'children' => array(
  2955. array('Category' => array(
  2956. 'id' => '7',
  2957. 'parent_id' => '2',
  2958. 'name' => 'Category 1.1.1',
  2959. 'created' => '2007-03-18 15:30:23',
  2960. 'updated' => '2007-03-18 15:32:31'),
  2961. 'children' => array()),
  2962. array('Category' => array(
  2963. 'id' => '8',
  2964. 'parent_id' => '2',
  2965. 'name' => 'Category 1.1.2',
  2966. 'created' => '2007-03-18 15:30:23',
  2967. 'updated' => '2007-03-18 15:32:31'),
  2968. 'children' => array()))
  2969. ),
  2970. array(
  2971. 'Category' => array(
  2972. 'id' => '3',
  2973. 'parent_id' => '1',
  2974. 'name' => 'Category 1.2',
  2975. 'created' => '2007-03-18 15:30:23',
  2976. 'updated' => '2007-03-18 15:32:31'
  2977. ),
  2978. 'children' => array()
  2979. )
  2980. )
  2981. ),
  2982. array(
  2983. 'Category' => array(
  2984. 'id' => '4',
  2985. 'parent_id' => '0',
  2986. 'name' => 'Category 2',
  2987. 'created' => '2007-03-18 15:30:23',
  2988. 'updated' => '2007-03-18 15:32:31'
  2989. ),
  2990. 'children' => array()
  2991. ),
  2992. array(
  2993. 'Category' => array(
  2994. 'id' => '5',
  2995. 'parent_id' => '0',
  2996. 'name' => 'Category 3',
  2997. 'created' => '2007-03-18 15:30:23',
  2998. 'updated' => '2007-03-18 15:32:31'
  2999. ),
  3000. 'children' => array(
  3001. array(
  3002. 'Category' => array(
  3003. 'id' => '6',
  3004. 'parent_id' => '5',
  3005. 'name' => 'Category 3.1',
  3006. 'created' => '2007-03-18 15:30:23',
  3007. 'updated' => '2007-03-18 15:32:31'
  3008. ),
  3009. 'children' => array()
  3010. )
  3011. )
  3012. )
  3013. );
  3014. $this->assertEquals($expected, $result);
  3015. $result = $TestModel->find('threaded', array(
  3016. 'conditions' => array('Category.name LIKE' => 'Category 1%')
  3017. ));
  3018. $expected = array(
  3019. array(
  3020. 'Category' => array(
  3021. 'id' => '1',
  3022. 'parent_id' => '0',
  3023. 'name' => 'Category 1',
  3024. 'created' => '2007-03-18 15:30:23',
  3025. 'updated' => '2007-03-18 15:32:31'
  3026. ),
  3027. 'children' => array(
  3028. array(
  3029. 'Category' => array(
  3030. 'id' => '2',
  3031. 'parent_id' => '1',
  3032. 'name' => 'Category 1.1',
  3033. 'created' => '2007-03-18 15:30:23',
  3034. 'updated' => '2007-03-18 15:32:31'
  3035. ),
  3036. 'children' => array(
  3037. array('Category' => array(
  3038. 'id' => '7',
  3039. 'parent_id' => '2',
  3040. 'name' => 'Category 1.1.1',
  3041. 'created' => '2007-03-18 15:30:23',
  3042. 'updated' => '2007-03-18 15:32:31'),
  3043. 'children' => array()),
  3044. array('Category' => array(
  3045. 'id' => '8',
  3046. 'parent_id' => '2',
  3047. 'name' => 'Category 1.1.2',
  3048. 'created' => '2007-03-18 15:30:23',
  3049. 'updated' => '2007-03-18 15:32:31'),
  3050. 'children' => array()))
  3051. ),
  3052. array(
  3053. 'Category' => array(
  3054. 'id' => '3',
  3055. 'parent_id' => '1',
  3056. 'name' => 'Category 1.2',
  3057. 'created' => '2007-03-18 15:30:23',
  3058. 'updated' => '2007-03-18 15:32:31'
  3059. ),
  3060. 'children' => array()
  3061. )
  3062. )
  3063. )
  3064. );
  3065. $this->assertEquals($expected, $result);
  3066. $result = $TestModel->find('threaded', array(
  3067. 'fields' => 'id, parent_id, name'
  3068. ));
  3069. $expected = array(
  3070. array(
  3071. 'Category' => array(
  3072. 'id' => '1',
  3073. 'parent_id' => '0',
  3074. 'name' => 'Category 1'
  3075. ),
  3076. 'children' => array(
  3077. array(
  3078. 'Category' => array(
  3079. 'id' => '2',
  3080. 'parent_id' => '1',
  3081. 'name' => 'Category 1.1'
  3082. ),
  3083. 'children' => array(
  3084. array('Category' => array(
  3085. 'id' => '7',
  3086. 'parent_id' => '2',
  3087. 'name' => 'Category 1.1.1'),
  3088. 'children' => array()),
  3089. array('Category' => array(
  3090. 'id' => '8',
  3091. 'parent_id' => '2',
  3092. 'name' => 'Category 1.1.2'),
  3093. 'children' => array()))
  3094. ),
  3095. array(
  3096. 'Category' => array(
  3097. 'id' => '3',
  3098. 'parent_id' => '1',
  3099. 'name' => 'Category 1.2'
  3100. ),
  3101. 'children' => array()
  3102. )
  3103. )
  3104. ),
  3105. array(
  3106. 'Category' => array(
  3107. 'id' => '4',
  3108. 'parent_id' => '0',
  3109. 'name' => 'Category 2'
  3110. ),
  3111. 'children' => array()
  3112. ),
  3113. array(
  3114. 'Category' => array(
  3115. 'id' => '5',
  3116. 'parent_id' => '0',
  3117. 'name' => 'Category 3'
  3118. ),
  3119. 'children' => array(
  3120. array(
  3121. 'Category' => array(
  3122. 'id' => '6',
  3123. 'parent_id' => '5',
  3124. 'name' => 'Category 3.1'
  3125. ),
  3126. 'children' => array()
  3127. )
  3128. )
  3129. )
  3130. );
  3131. $this->assertEquals($expected, $result);
  3132. $result = $TestModel->find('threaded', array('order' => 'id DESC'));
  3133. $expected = array(
  3134. array(
  3135. 'Category' => array(
  3136. 'id' => 5,
  3137. 'parent_id' => 0,
  3138. 'name' => 'Category 3',
  3139. 'created' => '2007-03-18 15:30:23',
  3140. 'updated' => '2007-03-18 15:32:31'
  3141. ),
  3142. 'children' => array(
  3143. array(
  3144. 'Category' => array(
  3145. 'id' => 6,
  3146. 'parent_id' => 5,
  3147. 'name' => 'Category 3.1',
  3148. 'created' => '2007-03-18 15:30:23',
  3149. 'updated' => '2007-03-18 15:32:31'
  3150. ),
  3151. 'children' => array()
  3152. )
  3153. )
  3154. ),
  3155. array(
  3156. 'Category' => array(
  3157. 'id' => 4,
  3158. 'parent_id' => 0,
  3159. 'name' => 'Category 2',
  3160. 'created' => '2007-03-18 15:30:23',
  3161. 'updated' => '2007-03-18 15:32:31'
  3162. ),
  3163. 'children' => array()
  3164. ),
  3165. array(
  3166. 'Category' => array(
  3167. 'id' => 1,
  3168. 'parent_id' => 0,
  3169. 'name' => 'Category 1',
  3170. 'created' => '2007-03-18 15:30:23',
  3171. 'updated' => '2007-03-18 15:32:31'
  3172. ),
  3173. 'children' => array(
  3174. array(
  3175. 'Category' => array(
  3176. 'id' => 3,
  3177. 'parent_id' => 1,
  3178. 'name' => 'Category 1.2',
  3179. 'created' => '2007-03-18 15:30:23',
  3180. 'updated' => '2007-03-18 15:32:31'
  3181. ),
  3182. 'children' => array()
  3183. ),
  3184. array(
  3185. 'Category' => array(
  3186. 'id' => 2,
  3187. 'parent_id' => 1,
  3188. 'name' => 'Category 1.1',
  3189. 'created' => '2007-03-18 15:30:23',
  3190. 'updated' => '2007-03-18 15:32:31'
  3191. ),
  3192. 'children' => array(
  3193. array('Category' => array(
  3194. 'id' => '8',
  3195. 'parent_id' => '2',
  3196. 'name' => 'Category 1.1.2',
  3197. 'created' => '2007-03-18 15:30:23',
  3198. 'updated' => '2007-03-18 15:32:31'),
  3199. 'children' => array()),
  3200. array('Category' => array(
  3201. 'id' => '7',
  3202. 'parent_id' => '2',
  3203. 'name' => 'Category 1.1.1',
  3204. 'created' => '2007-03-18 15:30:23',
  3205. 'updated' => '2007-03-18 15:32:31'),
  3206. 'children' => array()))
  3207. )
  3208. )
  3209. )
  3210. );
  3211. $this->assertEquals($expected, $result);
  3212. $result = $TestModel->find('threaded', array(
  3213. 'conditions' => array('Category.name LIKE' => 'Category 3%')
  3214. ));
  3215. $expected = array(
  3216. array(
  3217. 'Category' => array(
  3218. 'id' => '5',
  3219. 'parent_id' => '0',
  3220. 'name' => 'Category 3',
  3221. 'created' => '2007-03-18 15:30:23',
  3222. 'updated' => '2007-03-18 15:32:31'
  3223. ),
  3224. 'children' => array(
  3225. array(
  3226. 'Category' => array(
  3227. 'id' => '6',
  3228. 'parent_id' => '5',
  3229. 'name' => 'Category 3.1',
  3230. 'created' => '2007-03-18 15:30:23',
  3231. 'updated' => '2007-03-18 15:32:31'
  3232. ),
  3233. 'children' => array()
  3234. )
  3235. )
  3236. )
  3237. );
  3238. $this->assertEquals($expected, $result);
  3239. $result = $TestModel->find('threaded', array(
  3240. 'conditions' => array('Category.name LIKE' => 'Category 1.1%')
  3241. ));
  3242. $expected = array(
  3243. array('Category' =>
  3244. array(
  3245. 'id' => '2',
  3246. 'parent_id' => '1',
  3247. 'name' => 'Category 1.1',
  3248. 'created' => '2007-03-18 15:30:23',
  3249. 'updated' => '2007-03-18 15:32:31'),
  3250. 'children' => array(
  3251. array('Category' => array(
  3252. 'id' => '7',
  3253. 'parent_id' => '2',
  3254. 'name' => 'Category 1.1.1',
  3255. 'created' => '2007-03-18 15:30:23',
  3256. 'updated' => '2007-03-18 15:32:31'),
  3257. 'children' => array()),
  3258. array('Category' => array(
  3259. 'id' => '8',
  3260. 'parent_id' => '2',
  3261. 'name' => 'Category 1.1.2',
  3262. 'created' => '2007-03-18 15:30:23',
  3263. 'updated' => '2007-03-18 15:32:31'),
  3264. 'children' => array()))));
  3265. $this->assertEquals($expected, $result);
  3266. $result = $TestModel->find('threaded', array(
  3267. 'fields' => 'id, parent_id, name',
  3268. 'conditions' => array('Category.id !=' => 2)
  3269. ));
  3270. $expected = array(
  3271. array(
  3272. 'Category' => array(
  3273. 'id' => '1',
  3274. 'parent_id' => '0',
  3275. 'name' => 'Category 1'
  3276. ),
  3277. 'children' => array(
  3278. array(
  3279. 'Category' => array(
  3280. 'id' => '3',
  3281. 'parent_id' => '1',
  3282. 'name' => 'Category 1.2'
  3283. ),
  3284. 'children' => array()
  3285. )
  3286. )
  3287. ),
  3288. array(
  3289. 'Category' => array(
  3290. 'id' => '4',
  3291. 'parent_id' => '0',
  3292. 'name' => 'Category 2'
  3293. ),
  3294. 'children' => array()
  3295. ),
  3296. array(
  3297. 'Category' => array(
  3298. 'id' => '5',
  3299. 'parent_id' => '0',
  3300. 'name' => 'Category 3'
  3301. ),
  3302. 'children' => array(
  3303. array(
  3304. 'Category' => array(
  3305. 'id' => '6',
  3306. 'parent_id' => '5',
  3307. 'name' => 'Category 3.1'
  3308. ),
  3309. 'children' => array()
  3310. )
  3311. )
  3312. )
  3313. );
  3314. $this->assertEquals($expected, $result);
  3315. $result = $TestModel->find('all', array(
  3316. 'fields' => 'id, name, parent_id',
  3317. 'conditions' => array('Category.id !=' => 1)
  3318. ));
  3319. $expected = array(
  3320. array('Category' => array(
  3321. 'id' => '2',
  3322. 'name' => 'Category 1.1',
  3323. 'parent_id' => '1'
  3324. )),
  3325. array('Category' => array(
  3326. 'id' => '3',
  3327. 'name' => 'Category 1.2',
  3328. 'parent_id' => '1'
  3329. )),
  3330. array('Category' => array(
  3331. 'id' => '4',
  3332. 'name' => 'Category 2',
  3333. 'parent_id' => '0'
  3334. )),
  3335. array('Category' => array(
  3336. 'id' => '5',
  3337. 'name' => 'Category 3',
  3338. 'parent_id' => '0'
  3339. )),
  3340. array('Category' => array(
  3341. 'id' => '6',
  3342. 'name' => 'Category 3.1',
  3343. 'parent_id' => '5'
  3344. )),
  3345. array('Category' => array(
  3346. 'id' => '7',
  3347. 'name' => 'Category 1.1.1',
  3348. 'parent_id' => '2'
  3349. )),
  3350. array('Category' => array(
  3351. 'id' => '8',
  3352. 'name' => 'Category 1.1.2',
  3353. 'parent_id' => '2'
  3354. )));
  3355. $this->assertEquals($expected, $result);
  3356. $result = $TestModel->find('threaded', array(
  3357. 'fields' => 'id, parent_id, name',
  3358. 'conditions' => array('Category.id !=' => 1)
  3359. ));
  3360. $expected = array(
  3361. array(
  3362. 'Category' => array(
  3363. 'id' => '2',
  3364. 'parent_id' => '1',
  3365. 'name' => 'Category 1.1'
  3366. ),
  3367. 'children' => array(
  3368. array('Category' => array(
  3369. 'id' => '7',
  3370. 'parent_id' => '2',
  3371. 'name' => 'Category 1.1.1'),
  3372. 'children' => array()),
  3373. array('Category' => array(
  3374. 'id' => '8',
  3375. 'parent_id' => '2',
  3376. 'name' => 'Category 1.1.2'),
  3377. 'children' => array()))
  3378. ),
  3379. array(
  3380. 'Category' => array(
  3381. 'id' => '3',
  3382. 'parent_id' => '1',
  3383. 'name' => 'Category 1.2'
  3384. ),
  3385. 'children' => array()
  3386. )
  3387. );
  3388. $this->assertEquals($expected, $result);
  3389. }
  3390. /**
  3391. * test find('neighbors')
  3392. *
  3393. * @return void
  3394. */
  3395. public function testFindNeighbors() {
  3396. $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Attachment');
  3397. $TestModel = new Article();
  3398. $TestModel->id = 1;
  3399. $result = $TestModel->find('neighbors', array('fields' => array('id')));
  3400. $this->assertNull($result['prev']);
  3401. $this->assertEquals($result['next']['Article'], array('id' => 2));
  3402. $this->assertEquals(count($result['next']['Comment']), 2);
  3403. $this->assertEquals(count($result['next']['Tag']), 2);
  3404. $TestModel->id = 2;
  3405. $TestModel->recursive = 0;
  3406. $result = $TestModel->find('neighbors', array(
  3407. 'fields' => array('id')
  3408. ));
  3409. $expected = array(
  3410. 'prev' => array(
  3411. 'Article' => array(
  3412. 'id' => 1
  3413. )),
  3414. 'next' => array(
  3415. 'Article' => array(
  3416. 'id' => 3
  3417. )));
  3418. $this->assertEquals($expected, $result);
  3419. $TestModel->id = 3;
  3420. $TestModel->recursive = 1;
  3421. $result = $TestModel->find('neighbors', array('fields' => array('id')));
  3422. $this->assertNull($result['next']);
  3423. $this->assertEquals($result['prev']['Article'], array('id' => 2));
  3424. $this->assertEquals(count($result['prev']['Comment']), 2);
  3425. $this->assertEquals(count($result['prev']['Tag']), 2);
  3426. $TestModel->id = 1;
  3427. $result = $TestModel->find('neighbors', array('recursive' => -1));
  3428. $expected = array(
  3429. 'prev' => null,
  3430. 'next' => array(
  3431. 'Article' => array(
  3432. 'id' => 2,
  3433. 'user_id' => 3,
  3434. 'title' => 'Second Article',
  3435. 'body' => 'Second Article Body',
  3436. 'published' => 'Y',
  3437. 'created' => '2007-03-18 10:41:23',
  3438. 'updated' => '2007-03-18 10:43:31'
  3439. )
  3440. )
  3441. );
  3442. $this->assertEquals($expected, $result);
  3443. $TestModel->id = 2;
  3444. $result = $TestModel->find('neighbors', array('recursive' => -1));
  3445. $expected = array(
  3446. 'prev' => array(
  3447. 'Article' => array(
  3448. 'id' => 1,
  3449. 'user_id' => 1,
  3450. 'title' => 'First Article',
  3451. 'body' => 'First Article Body',
  3452. 'published' => 'Y',
  3453. 'created' => '2007-03-18 10:39:23',
  3454. 'updated' => '2007-03-18 10:41:31'
  3455. )
  3456. ),
  3457. 'next' => array(
  3458. 'Article' => array(
  3459. 'id' => 3,
  3460. 'user_id' => 1,
  3461. 'title' => 'Third Article',
  3462. 'body' => 'Third Article Body',
  3463. 'published' => 'Y',
  3464. 'created' => '2007-03-18 10:43:23',
  3465. 'updated' => '2007-03-18 10:45:31'
  3466. )
  3467. )
  3468. );
  3469. $this->assertEquals($expected, $result);
  3470. $TestModel->id = 3;
  3471. $result = $TestModel->find('neighbors', array('recursive' => -1));
  3472. $expected = array(
  3473. 'prev' => array(
  3474. 'Article' => array(
  3475. 'id' => 2,
  3476. 'user_id' => 3,
  3477. 'title' => 'Second Article',
  3478. 'body' => 'Second Article Body',
  3479. 'published' => 'Y',
  3480. 'created' => '2007-03-18 10:41:23',
  3481. 'updated' => '2007-03-18 10:43:31'
  3482. )
  3483. ),
  3484. 'next' => null
  3485. );
  3486. $this->assertEquals($expected, $result);
  3487. $TestModel->recursive = 0;
  3488. $TestModel->id = 1;
  3489. $one = $TestModel->read();
  3490. $TestModel->id = 2;
  3491. $two = $TestModel->read();
  3492. $TestModel->id = 3;
  3493. $three = $TestModel->read();
  3494. $TestModel->id = 1;
  3495. $result = $TestModel->find('neighbors');
  3496. $expected = array('prev' => null, 'next' => $two);
  3497. $this->assertEquals($expected, $result);
  3498. $TestModel->id = 2;
  3499. $result = $TestModel->find('neighbors');
  3500. $expected = array('prev' => $one, 'next' => $three);
  3501. $this->assertEquals($expected, $result);
  3502. $TestModel->id = 3;
  3503. $result = $TestModel->find('neighbors');
  3504. $expected = array('prev' => $two, 'next' => null);
  3505. $this->assertEquals($expected, $result);
  3506. $TestModel->recursive = 2;
  3507. $TestModel->id = 1;
  3508. $one = $TestModel->read();
  3509. $TestModel->id = 2;
  3510. $two = $TestModel->read();
  3511. $TestModel->id = 3;
  3512. $three = $TestModel->read();
  3513. $TestModel->id = 1;
  3514. $result = $TestModel->find('neighbors', array('recursive' => 2));
  3515. $expected = array('prev' => null, 'next' => $two);
  3516. $this->assertEquals($expected, $result);
  3517. $TestModel->id = 2;
  3518. $result = $TestModel->find('neighbors', array('recursive' => 2));
  3519. $expected = array('prev' => $one, 'next' => $three);
  3520. $this->assertEquals($expected, $result);
  3521. $TestModel->id = 3;
  3522. $result = $TestModel->find('neighbors', array('recursive' => 2));
  3523. $expected = array('prev' => $two, 'next' => null);
  3524. $this->assertEquals($expected, $result);
  3525. }
  3526. /**
  3527. * testFindCombinedRelations method
  3528. *
  3529. * @return void
  3530. */
  3531. public function testFindCombinedRelations() {
  3532. $this->skipIf($this->db instanceof Sqlserver, 'The test of testRecursiveUnbind test is not compatible with SQL Server, because it check for time columns.');
  3533. $this->loadFixtures('Apple', 'Sample');
  3534. $TestModel = new Apple();
  3535. $result = $TestModel->find('all');
  3536. $expected = array(
  3537. array(
  3538. 'Apple' => array(
  3539. 'id' => '1',
  3540. 'apple_id' => '2',
  3541. 'color' => 'Red 1',
  3542. 'name' => 'Red Apple 1',
  3543. 'created' => '2006-11-22 10:38:58',
  3544. 'date' => '1951-01-04',
  3545. 'modified' => '2006-12-01 13:31:26',
  3546. 'mytime' => '22:57:17'
  3547. ),
  3548. 'Parent' => array(
  3549. 'id' => '2',
  3550. 'apple_id' => '1',
  3551. 'color' => 'Bright Red 1',
  3552. 'name' => 'Bright Red Apple',
  3553. 'created' => '2006-11-22 10:43:13',
  3554. 'date' => '2014-01-01',
  3555. 'modified' => '2006-11-30 18:38:10',
  3556. 'mytime' => '22:57:17'
  3557. ),
  3558. 'Sample' => array(
  3559. 'id' => null,
  3560. 'apple_id' => null,
  3561. 'name' => null
  3562. ),
  3563. 'Child' => array(
  3564. array(
  3565. 'id' => '2',
  3566. 'apple_id' => '1',
  3567. 'color' => 'Bright Red 1',
  3568. 'name' => 'Bright Red Apple',
  3569. 'created' => '2006-11-22 10:43:13',
  3570. 'date' => '2014-01-01',
  3571. 'modified' => '2006-11-30 18:38:10',
  3572. 'mytime' => '22:57:17'
  3573. ))),
  3574. array(
  3575. 'Apple' => array(
  3576. 'id' => '2',
  3577. 'apple_id' => '1',
  3578. 'color' => 'Bright Red 1',
  3579. 'name' => 'Bright Red Apple',
  3580. 'created' => '2006-11-22 10:43:13',
  3581. 'date' => '2014-01-01',
  3582. 'modified' => '2006-11-30 18:38:10',
  3583. 'mytime' => '22:57:17'
  3584. ),
  3585. 'Parent' => array(
  3586. 'id' => '1',
  3587. 'apple_id' => '2',
  3588. 'color' => 'Red 1',
  3589. 'name' => 'Red Apple 1',
  3590. 'created' => '2006-11-22 10:38:58',
  3591. 'date' => '1951-01-04',
  3592. 'modified' => '2006-12-01 13:31:26',
  3593. 'mytime' => '22:57:17'
  3594. ),
  3595. 'Sample' => array(
  3596. 'id' => '2',
  3597. 'apple_id' => '2',
  3598. 'name' => 'sample2'
  3599. ),
  3600. 'Child' => array(
  3601. array(
  3602. 'id' => '1',
  3603. 'apple_id' => '2',
  3604. 'color' => 'Red 1',
  3605. 'name' => 'Red Apple 1',
  3606. 'created' => '2006-11-22 10:38:58',
  3607. 'date' => '1951-01-04',
  3608. 'modified' => '2006-12-01 13:31:26',
  3609. 'mytime' => '22:57:17'
  3610. ),
  3611. array(
  3612. 'id' => '3',
  3613. 'apple_id' => '2',
  3614. 'color' => 'blue green',
  3615. 'name' => 'green blue',
  3616. 'created' => '2006-12-25 05:13:36',
  3617. 'date' => '2006-12-25',
  3618. 'modified' => '2006-12-25 05:23:24',
  3619. 'mytime' => '22:57:17'
  3620. ),
  3621. array(
  3622. 'id' => '4',
  3623. 'apple_id' => '2',
  3624. 'color' => 'Blue Green',
  3625. 'name' => 'Test Name',
  3626. 'created' => '2006-12-25 05:23:36',
  3627. 'date' => '2006-12-25',
  3628. 'modified' => '2006-12-25 05:23:36',
  3629. 'mytime' => '22:57:17'
  3630. ))),
  3631. array(
  3632. 'Apple' => array(
  3633. 'id' => '3',
  3634. 'apple_id' => '2',
  3635. 'color' => 'blue green',
  3636. 'name' => 'green blue',
  3637. 'created' => '2006-12-25 05:13:36',
  3638. 'date' => '2006-12-25',
  3639. 'modified' => '2006-12-25 05:23:24',
  3640. 'mytime' => '22:57:17'
  3641. ),
  3642. 'Parent' => array(
  3643. 'id' => '2',
  3644. 'apple_id' => '1',
  3645. 'color' => 'Bright Red 1',
  3646. 'name' => 'Bright Red Apple',
  3647. 'created' => '2006-11-22 10:43:13',
  3648. 'date' => '2014-01-01',
  3649. 'modified' => '2006-11-30 18:38:10',
  3650. 'mytime' => '22:57:17'
  3651. ),
  3652. 'Sample' => array(
  3653. 'id' => '1',
  3654. 'apple_id' => '3',
  3655. 'name' => 'sample1'
  3656. ),
  3657. 'Child' => array()
  3658. ),
  3659. array(
  3660. 'Apple' => array(
  3661. 'id' => '4',
  3662. 'apple_id' => '2',
  3663. 'color' => 'Blue Green',
  3664. 'name' => 'Test Name',
  3665. 'created' => '2006-12-25 05:23:36',
  3666. 'date' => '2006-12-25',
  3667. 'modified' => '2006-12-25 05:23:36',
  3668. 'mytime' => '22:57:17'
  3669. ),
  3670. 'Parent' => array(
  3671. 'id' => '2',
  3672. 'apple_id' => '1',
  3673. 'color' => 'Bright Red 1',
  3674. 'name' => 'Bright Red Apple',
  3675. 'created' => '2006-11-22 10:43:13',
  3676. 'date' => '2014-01-01',
  3677. 'modified' => '2006-11-30 18:38:10',
  3678. 'mytime' => '22:57:17'
  3679. ),
  3680. 'Sample' => array(
  3681. 'id' => '3',
  3682. 'apple_id' => '4',
  3683. 'name' => 'sample3'
  3684. ),
  3685. 'Child' => array(
  3686. array(
  3687. 'id' => '6',
  3688. 'apple_id' => '4',
  3689. 'color' => 'My new appleOrange',
  3690. 'name' => 'My new apple',
  3691. 'created' => '2006-12-25 05:29:39',
  3692. 'date' => '2006-12-25',
  3693. 'modified' => '2006-12-25 05:29:39',
  3694. 'mytime' => '22:57:17'
  3695. ))),
  3696. array(
  3697. 'Apple' => array(
  3698. 'id' => '5',
  3699. 'apple_id' => '5',
  3700. 'color' => 'Green',
  3701. 'name' => 'Blue Green',
  3702. 'created' => '2006-12-25 05:24:06',
  3703. 'date' => '2006-12-25',
  3704. 'modified' => '2006-12-25 05:29:16',
  3705. 'mytime' => '22:57:17'
  3706. ),
  3707. 'Parent' => array(
  3708. 'id' => '5',
  3709. 'apple_id' => '5',
  3710. 'color' => 'Green',
  3711. 'name' => 'Blue Green',
  3712. 'created' => '2006-12-25 05:24:06',
  3713. 'date' => '2006-12-25',
  3714. 'modified' => '2006-12-25 05:29:16',
  3715. 'mytime' => '22:57:17'
  3716. ),
  3717. 'Sample' => array(
  3718. 'id' => '4',
  3719. 'apple_id' => '5',
  3720. 'name' => 'sample4'
  3721. ),
  3722. 'Child' => array(
  3723. array(
  3724. 'id' => '5',
  3725. 'apple_id' => '5',
  3726. 'color' => 'Green',
  3727. 'name' => 'Blue Green',
  3728. 'created' => '2006-12-25 05:24:06',
  3729. 'date' => '2006-12-25',
  3730. 'modified' => '2006-12-25 05:29:16',
  3731. 'mytime' => '22:57:17'
  3732. ))),
  3733. array(
  3734. 'Apple' => array(
  3735. 'id' => '6',
  3736. 'apple_id' => '4',
  3737. 'color' => 'My new appleOrange',
  3738. 'name' => 'My new apple',
  3739. 'created' => '2006-12-25 05:29:39',
  3740. 'date' => '2006-12-25',
  3741. 'modified' => '2006-12-25 05:29:39',
  3742. 'mytime' => '22:57:17'
  3743. ),
  3744. 'Parent' => array(
  3745. 'id' => '4',
  3746. 'apple_id' => '2',
  3747. 'color' => 'Blue Green',
  3748. 'name' => 'Test Name',
  3749. 'created' => '2006-12-25 05:23:36',
  3750. 'date' => '2006-12-25',
  3751. 'modified' => '2006-12-25 05:23:36',
  3752. 'mytime' => '22:57:17'
  3753. ),
  3754. 'Sample' => array(
  3755. 'id' => null,
  3756. 'apple_id' => null,
  3757. 'name' => null
  3758. ),
  3759. 'Child' => array(
  3760. array(
  3761. 'id' => '7',
  3762. 'apple_id' => '6',
  3763. 'color' => 'Some wierd color',
  3764. 'name' => 'Some odd color',
  3765. 'created' => '2006-12-25 05:34:21',
  3766. 'date' => '2006-12-25',
  3767. 'modified' => '2006-12-25 05:34:21',
  3768. 'mytime' => '22:57:17'
  3769. ))),
  3770. array(
  3771. 'Apple' => array(
  3772. 'id' => '7',
  3773. 'apple_id' => '6',
  3774. 'color' => 'Some wierd color',
  3775. 'name' => 'Some odd color',
  3776. 'created' => '2006-12-25 05:34:21',
  3777. 'date' => '2006-12-25',
  3778. 'modified' => '2006-12-25 05:34:21',
  3779. 'mytime' => '22:57:17'
  3780. ),
  3781. 'Parent' => array(
  3782. 'id' => '6',
  3783. 'apple_id' => '4',
  3784. 'color' => 'My new appleOrange',
  3785. 'name' => 'My new apple',
  3786. 'created' => '2006-12-25 05:29:39',
  3787. 'date' => '2006-12-25',
  3788. 'modified' => '2006-12-25 05:29:39',
  3789. 'mytime' => '22:57:17'
  3790. ),
  3791. 'Sample' => array(
  3792. 'id' => null,
  3793. 'apple_id' => null,
  3794. 'name' => null
  3795. ),
  3796. 'Child' => array()
  3797. ));
  3798. $this->assertEquals($expected, $result);
  3799. }
  3800. /**
  3801. * testSaveEmpty method
  3802. *
  3803. * @return void
  3804. */
  3805. public function testSaveEmpty() {
  3806. $this->loadFixtures('Thread');
  3807. $TestModel = new Thread();
  3808. $data = array();
  3809. $expected = $TestModel->save($data);
  3810. $this->assertFalse($expected);
  3811. }
  3812. /**
  3813. * testFindAllWithConditionInChildQuery
  3814. *
  3815. * @todo external conditions like this are going to need to be revisited at some point
  3816. * @return void
  3817. */
  3818. public function testFindAllWithConditionInChildQuery() {
  3819. $this->loadFixtures('Basket', 'FilmFile');
  3820. $TestModel = new Basket();
  3821. $recursive = 3;
  3822. $result = $TestModel->find('all', compact('recursive'));
  3823. $expected = array(
  3824. array(
  3825. 'Basket' => array(
  3826. 'id' => 1,
  3827. 'type' => 'nonfile',
  3828. 'name' => 'basket1',
  3829. 'object_id' => 1,
  3830. 'user_id' => 1,
  3831. ),
  3832. 'FilmFile' => array(
  3833. 'id' => '',
  3834. 'name' => '',
  3835. )
  3836. ),
  3837. array(
  3838. 'Basket' => array(
  3839. 'id' => 2,
  3840. 'type' => 'file',
  3841. 'name' => 'basket2',
  3842. 'object_id' => 2,
  3843. 'user_id' => 1,
  3844. ),
  3845. 'FilmFile' => array(
  3846. 'id' => 2,
  3847. 'name' => 'two',
  3848. )
  3849. ),
  3850. );
  3851. $this->assertEquals($expected, $result);
  3852. }
  3853. /**
  3854. * testFindAllWithConditionsHavingMixedDataTypes method
  3855. *
  3856. * @return void
  3857. */
  3858. public function testFindAllWithConditionsHavingMixedDataTypes() {
  3859. $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag');
  3860. $TestModel = new Article();
  3861. $expected = array(
  3862. array(
  3863. 'Article' => array(
  3864. 'id' => 1,
  3865. 'user_id' => 1,
  3866. 'title' => 'First Article',
  3867. 'body' => 'First Article Body',
  3868. 'published' => 'Y',
  3869. 'created' => '2007-03-18 10:39:23',
  3870. 'updated' => '2007-03-18 10:41:31'
  3871. )
  3872. ),
  3873. array(
  3874. 'Article' => array(
  3875. 'id' => 2,
  3876. 'user_id' => 3,
  3877. 'title' => 'Second Article',
  3878. 'body' => 'Second Article Body',
  3879. 'published' => 'Y',
  3880. 'created' => '2007-03-18 10:41:23',
  3881. 'updated' => '2007-03-18 10:43:31'
  3882. )
  3883. )
  3884. );
  3885. $conditions = array('id' => array('1', 2));
  3886. $recursive = -1;
  3887. $order = 'Article.id ASC';
  3888. $result = $TestModel->find('all', compact('conditions', 'recursive', 'order'));
  3889. $this->assertEquals($expected, $result);
  3890. $this->skipIf($this->db instanceof Postgres, 'The rest of testFindAllWithConditionsHavingMixedDataTypes test is not compatible with Postgres.');
  3891. $conditions = array('id' => array('1', 2, '3.0'));
  3892. $order = 'Article.id ASC';
  3893. $result = $TestModel->find('all', compact('recursive', 'conditions', 'order'));
  3894. $expected = array(
  3895. array(
  3896. 'Article' => array(
  3897. 'id' => 1,
  3898. 'user_id' => 1,
  3899. 'title' => 'First Article',
  3900. 'body' => 'First Article Body',
  3901. 'published' => 'Y',
  3902. 'created' => '2007-03-18 10:39:23',
  3903. 'updated' => '2007-03-18 10:41:31'
  3904. )
  3905. ),
  3906. array(
  3907. 'Article' => array(
  3908. 'id' => 2,
  3909. 'user_id' => 3,
  3910. 'title' => 'Second Article',
  3911. 'body' => 'Second Article Body',
  3912. 'published' => 'Y',
  3913. 'created' => '2007-03-18 10:41:23',
  3914. 'updated' => '2007-03-18 10:43:31'
  3915. )
  3916. ),
  3917. array(
  3918. 'Article' => array(
  3919. 'id' => 3,
  3920. 'user_id' => 1,
  3921. 'title' => 'Third Article',
  3922. 'body' => 'Third Article Body',
  3923. 'published' => 'Y',
  3924. 'created' => '2007-03-18 10:43:23',
  3925. 'updated' => '2007-03-18 10:45:31'
  3926. )
  3927. )
  3928. );
  3929. $this->assertEquals($expected, $result);
  3930. }
  3931. /**
  3932. * testBindUnbind method
  3933. *
  3934. * @return void
  3935. */
  3936. public function testBindUnbind() {
  3937. $this->loadFixtures(
  3938. 'User',
  3939. 'Comment',
  3940. 'FeatureSet',
  3941. 'DeviceType',
  3942. 'DeviceTypeCategory',
  3943. 'ExteriorTypeCategory',
  3944. 'Device',
  3945. 'Document',
  3946. 'DocumentDirectory'
  3947. );
  3948. $TestModel = new User();
  3949. $result = $TestModel->hasMany;
  3950. $expected = array();
  3951. $this->assertEquals($expected, $result);
  3952. $result = $TestModel->bindModel(array('hasMany' => array('Comment')));
  3953. $this->assertTrue($result);
  3954. $result = $TestModel->find('all', array(
  3955. 'fields' => 'User.id, User.user'
  3956. ));
  3957. $expected = array(
  3958. array(
  3959. 'User' => array(
  3960. 'id' => '1',
  3961. 'user' => 'mariano'
  3962. ),
  3963. 'Comment' => array(
  3964. array(
  3965. 'id' => '3',
  3966. 'article_id' => '1',
  3967. 'user_id' => '1',
  3968. 'comment' => 'Third Comment for First Article',
  3969. 'published' => 'Y',
  3970. 'created' => '2007-03-18 10:49:23',
  3971. 'updated' => '2007-03-18 10:51:31'
  3972. ),
  3973. array(
  3974. 'id' => '4',
  3975. 'article_id' => '1',
  3976. 'user_id' => '1',
  3977. 'comment' => 'Fourth Comment for First Article',
  3978. 'published' => 'N',
  3979. 'created' => '2007-03-18 10:51:23',
  3980. 'updated' => '2007-03-18 10:53:31'
  3981. ),
  3982. array(
  3983. 'id' => '5',
  3984. 'article_id' => '2',
  3985. 'user_id' => '1',
  3986. 'comment' => 'First Comment for Second Article',
  3987. 'published' => 'Y',
  3988. 'created' => '2007-03-18 10:53:23',
  3989. 'updated' => '2007-03-18 10:55:31'
  3990. ))),
  3991. array(
  3992. 'User' => array(
  3993. 'id' => '2',
  3994. 'user' => 'nate'
  3995. ),
  3996. 'Comment' => array(
  3997. array(
  3998. 'id' => '1',
  3999. 'article_id' => '1',
  4000. 'user_id' => '2',
  4001. 'comment' => 'First Comment for First Article',
  4002. 'published' => 'Y',
  4003. 'created' => '2007-03-18 10:45:23',
  4004. 'updated' => '2007-03-18 10:47:31'
  4005. ),
  4006. array(
  4007. 'id' => '6',
  4008. 'article_id' => '2',
  4009. 'user_id' => '2',
  4010. 'comment' => 'Second Comment for Second Article',
  4011. 'published' => 'Y',
  4012. 'created' => '2007-03-18 10:55:23',
  4013. 'updated' => '2007-03-18 10:57:31'
  4014. ))),
  4015. array(
  4016. 'User' => array(
  4017. 'id' => '3',
  4018. 'user' => 'larry'
  4019. ),
  4020. 'Comment' => array()
  4021. ),
  4022. array(
  4023. 'User' => array(
  4024. 'id' => '4',
  4025. 'user' => 'garrett'
  4026. ),
  4027. 'Comment' => array(
  4028. array(
  4029. 'id' => '2',
  4030. 'article_id' => '1',
  4031. 'user_id' => '4',
  4032. 'comment' => 'Second Comment for First Article',
  4033. 'published' => 'Y',
  4034. 'created' => '2007-03-18 10:47:23',
  4035. 'updated' => '2007-03-18 10:49:31'
  4036. ))));
  4037. $this->assertEquals($expected, $result);
  4038. $TestModel->resetAssociations();
  4039. $result = $TestModel->hasMany;
  4040. $this->assertEquals($result, array());
  4041. $result = $TestModel->bindModel(array('hasMany' => array('Comment')), false);
  4042. $this->assertTrue($result);
  4043. $result = $TestModel->find('all', array(
  4044. 'fields' => 'User.id, User.user'
  4045. ));
  4046. $expected = array(
  4047. array(
  4048. 'User' => array(
  4049. 'id' => '1',
  4050. 'user' => 'mariano'
  4051. ),
  4052. 'Comment' => array(
  4053. array(
  4054. 'id' => '3',
  4055. 'article_id' => '1',
  4056. 'user_id' => '1',
  4057. 'comment' => 'Third Comment for First Article',
  4058. 'published' => 'Y',
  4059. 'created' => '2007-03-18 10:49:23',
  4060. 'updated' => '2007-03-18 10:51:31'
  4061. ),
  4062. array(
  4063. 'id' => '4',
  4064. 'article_id' => '1',
  4065. 'user_id' => '1',
  4066. 'comment' => 'Fourth Comment for First Article',
  4067. 'published' => 'N',
  4068. 'created' => '2007-03-18 10:51:23',
  4069. 'updated' => '2007-03-18 10:53:31'
  4070. ),
  4071. array(
  4072. 'id' => '5',
  4073. 'article_id' => '2',
  4074. 'user_id' => '1',
  4075. 'comment' => 'First Comment for Second Article',
  4076. 'published' => 'Y',
  4077. 'created' => '2007-03-18 10:53:23',
  4078. 'updated' => '2007-03-18 10:55:31'
  4079. ))),
  4080. array(
  4081. 'User' => array(
  4082. 'id' => '2',
  4083. 'user' => 'nate'
  4084. ),
  4085. 'Comment' => array(
  4086. array(
  4087. 'id' => '1',
  4088. 'article_id' => '1',
  4089. 'user_id' => '2',
  4090. 'comment' => 'First Comment for First Article',
  4091. 'published' => 'Y',
  4092. 'created' => '2007-03-18 10:45:23',
  4093. 'updated' => '2007-03-18 10:47:31'
  4094. ),
  4095. array(
  4096. 'id' => '6',
  4097. 'article_id' => '2',
  4098. 'user_id' => '2',
  4099. 'comment' => 'Second Comment for Second Article',
  4100. 'published' => 'Y',
  4101. 'created' => '2007-03-18 10:55:23',
  4102. 'updated' => '2007-03-18 10:57:31'
  4103. ))),
  4104. array(
  4105. 'User' => array(
  4106. 'id' => '3',
  4107. 'user' => 'larry'
  4108. ),
  4109. 'Comment' => array()
  4110. ),
  4111. array(
  4112. 'User' => array(
  4113. 'id' => '4',
  4114. 'user' => 'garrett'
  4115. ),
  4116. 'Comment' => array(
  4117. array(
  4118. 'id' => '2',
  4119. 'article_id' => '1',
  4120. 'user_id' => '4',
  4121. 'comment' => 'Second Comment for First Article',
  4122. 'published' => 'Y',
  4123. 'created' => '2007-03-18 10:47:23',
  4124. 'updated' => '2007-03-18 10:49:31'
  4125. ))));
  4126. $this->assertEquals($expected, $result);
  4127. $result = $TestModel->hasMany;
  4128. $expected = array(
  4129. 'Comment' => array(
  4130. 'className' => 'Comment',
  4131. 'foreignKey' => 'user_id',
  4132. 'conditions' => null,
  4133. 'fields' => null,
  4134. 'order' => null,
  4135. 'limit' => null,
  4136. 'offset' => null,
  4137. 'dependent' => null,
  4138. 'exclusive' => null,
  4139. 'finderQuery' => null,
  4140. 'counterQuery' => null
  4141. ));
  4142. $this->assertEquals($expected, $result);
  4143. $result = $TestModel->unbindModel(array('hasMany' => array('Comment')));
  4144. $this->assertTrue($result);
  4145. $result = $TestModel->hasMany;
  4146. $expected = array();
  4147. $this->assertEquals($expected, $result);
  4148. $result = $TestModel->find('all', array(
  4149. 'fields' => 'User.id, User.user'
  4150. ));
  4151. $expected = array(
  4152. array('User' => array('id' => '1', 'user' => 'mariano')),
  4153. array('User' => array('id' => '2', 'user' => 'nate')),
  4154. array('User' => array('id' => '3', 'user' => 'larry')),
  4155. array('User' => array('id' => '4', 'user' => 'garrett')));
  4156. $this->assertEquals($expected, $result);
  4157. $result = $TestModel->find('all', array(
  4158. 'fields' => 'User.id, User.user'
  4159. ));
  4160. $expected = array(
  4161. array(
  4162. 'User' => array(
  4163. 'id' => '1',
  4164. 'user' => 'mariano'
  4165. ),
  4166. 'Comment' => array(
  4167. array(
  4168. 'id' => '3',
  4169. 'article_id' => '1',
  4170. 'user_id' => '1',
  4171. 'comment' => 'Third Comment for First Article',
  4172. 'published' => 'Y',
  4173. 'created' => '2007-03-18 10:49:23',
  4174. 'updated' => '2007-03-18 10:51:31'
  4175. ),
  4176. array(
  4177. 'id' => '4',
  4178. 'article_id' => '1',
  4179. 'user_id' => '1',
  4180. 'comment' => 'Fourth Comment for First Article',
  4181. 'published' => 'N',
  4182. 'created' => '2007-03-18 10:51:23',
  4183. 'updated' => '2007-03-18 10:53:31'
  4184. ),
  4185. array(
  4186. 'id' => '5',
  4187. 'article_id' => '2',
  4188. 'user_id' => '1',
  4189. 'comment' => 'First Comment for Second Article',
  4190. 'published' => 'Y',
  4191. 'created' => '2007-03-18 10:53:23',
  4192. 'updated' => '2007-03-18 10:55:31'
  4193. ))),
  4194. array(
  4195. 'User' => array(
  4196. 'id' => '2',
  4197. 'user' => 'nate'
  4198. ),
  4199. 'Comment' => array(
  4200. array(
  4201. 'id' => '1',
  4202. 'article_id' => '1',
  4203. 'user_id' => '2',
  4204. 'comment' => 'First Comment for First Article',
  4205. 'published' => 'Y',
  4206. 'created' => '2007-03-18 10:45:23',
  4207. 'updated' => '2007-03-18 10:47:31'
  4208. ),
  4209. array(
  4210. 'id' => '6',
  4211. 'article_id' => '2',
  4212. 'user_id' => '2',
  4213. 'comment' => 'Second Comment for Second Article',
  4214. 'published' => 'Y',
  4215. 'created' => '2007-03-18 10:55:23',
  4216. 'updated' => '2007-03-18 10:57:31'
  4217. ))),
  4218. array(
  4219. 'User' => array(
  4220. 'id' => '3',
  4221. 'user' => 'larry'
  4222. ),
  4223. 'Comment' => array()
  4224. ),
  4225. array(
  4226. 'User' => array(
  4227. 'id' => '4',
  4228. 'user' => 'garrett'
  4229. ),
  4230. 'Comment' => array(
  4231. array(
  4232. 'id' => '2',
  4233. 'article_id' => '1',
  4234. 'user_id' => '4',
  4235. 'comment' =>
  4236. 'Second Comment for First Article',
  4237. 'published' => 'Y',
  4238. 'created' => '2007-03-18 10:47:23',
  4239. 'updated' => '2007-03-18 10:49:31'
  4240. ))));
  4241. $this->assertEquals($expected, $result);
  4242. $result = $TestModel->unbindModel(array('hasMany' => array('Comment')), false);
  4243. $this->assertTrue($result);
  4244. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  4245. $expected = array(
  4246. array('User' => array('id' => '1', 'user' => 'mariano')),
  4247. array('User' => array('id' => '2', 'user' => 'nate')),
  4248. array('User' => array('id' => '3', 'user' => 'larry')),
  4249. array('User' => array('id' => '4', 'user' => 'garrett')));
  4250. $this->assertEquals($expected, $result);
  4251. $result = $TestModel->hasMany;
  4252. $expected = array();
  4253. $this->assertEquals($expected, $result);
  4254. $result = $TestModel->bindModel(array('hasMany' => array(
  4255. 'Comment' => array('className' => 'Comment', 'conditions' => 'Comment.published = \'Y\'')
  4256. )));
  4257. $this->assertTrue($result);
  4258. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  4259. $expected = array(
  4260. array(
  4261. 'User' => array(
  4262. 'id' => '1',
  4263. 'user' => 'mariano'
  4264. ),
  4265. 'Comment' => array(
  4266. array(
  4267. 'id' => '3',
  4268. 'article_id' => '1',
  4269. 'user_id' => '1',
  4270. 'comment' => 'Third Comment for First Article',
  4271. 'published' => 'Y',
  4272. 'created' => '2007-03-18 10:49:23',
  4273. 'updated' => '2007-03-18 10:51:31'
  4274. ),
  4275. array(
  4276. 'id' => '5',
  4277. 'article_id' => '2',
  4278. 'user_id' => '1',
  4279. 'comment' => 'First Comment for Second Article',
  4280. 'published' => 'Y',
  4281. 'created' => '2007-03-18 10:53:23',
  4282. 'updated' => '2007-03-18 10:55:31'
  4283. ))),
  4284. array(
  4285. 'User' => array(
  4286. 'id' => '2',
  4287. 'user' => 'nate'
  4288. ),
  4289. 'Comment' => array(
  4290. array(
  4291. 'id' => '1',
  4292. 'article_id' => '1',
  4293. 'user_id' => '2',
  4294. 'comment' => 'First Comment for First Article',
  4295. 'published' => 'Y',
  4296. 'created' => '2007-03-18 10:45:23',
  4297. 'updated' => '2007-03-18 10:47:31'
  4298. ),
  4299. array(
  4300. 'id' => '6',
  4301. 'article_id' => '2',
  4302. 'user_id' => '2',
  4303. 'comment' => 'Second Comment for Second Article',
  4304. 'published' => 'Y',
  4305. 'created' => '2007-03-18 10:55:23',
  4306. 'updated' => '2007-03-18 10:57:31'
  4307. ))),
  4308. array(
  4309. 'User' => array(
  4310. 'id' => '3',
  4311. 'user' => 'larry'
  4312. ),
  4313. 'Comment' => array()
  4314. ),
  4315. array(
  4316. 'User' => array(
  4317. 'id' => '4',
  4318. 'user' => 'garrett'
  4319. ),
  4320. 'Comment' => array(
  4321. array(
  4322. 'id' => '2',
  4323. 'article_id' => '1',
  4324. 'user_id' => '4',
  4325. 'comment' => 'Second Comment for First Article',
  4326. 'published' => 'Y',
  4327. 'created' => '2007-03-18 10:47:23',
  4328. 'updated' => '2007-03-18 10:49:31'
  4329. ))));
  4330. $this->assertEquals($expected, $result);
  4331. $TestModel2 = new DeviceType();
  4332. $expected = array(
  4333. 'className' => 'FeatureSet',
  4334. 'foreignKey' => 'feature_set_id',
  4335. 'conditions' => '',
  4336. 'fields' => '',
  4337. 'order' => '',
  4338. 'counterCache' => ''
  4339. );
  4340. $this->assertEquals($TestModel2->belongsTo['FeatureSet'], $expected);
  4341. $TestModel2->bindModel(array(
  4342. 'belongsTo' => array(
  4343. 'FeatureSet' => array(
  4344. 'className' => 'FeatureSet',
  4345. 'conditions' => array('active' => true)
  4346. )
  4347. )
  4348. ));
  4349. $expected['conditions'] = array('active' => true);
  4350. $this->assertEquals($TestModel2->belongsTo['FeatureSet'], $expected);
  4351. $TestModel2->bindModel(array(
  4352. 'belongsTo' => array(
  4353. 'FeatureSet' => array(
  4354. 'className' => 'FeatureSet',
  4355. 'foreignKey' => false,
  4356. 'conditions' => array('Feature.name' => 'DeviceType.name')
  4357. )
  4358. )
  4359. ));
  4360. $expected['conditions'] = array('Feature.name' => 'DeviceType.name');
  4361. $expected['foreignKey'] = false;
  4362. $this->assertEquals($TestModel2->belongsTo['FeatureSet'], $expected);
  4363. $TestModel2->bindModel(array(
  4364. 'hasMany' => array(
  4365. 'NewFeatureSet' => array(
  4366. 'className' => 'FeatureSet',
  4367. 'conditions' => array('active' => true)
  4368. )
  4369. )
  4370. ));
  4371. $expected = array(
  4372. 'className' => 'FeatureSet',
  4373. 'conditions' => array('active' => true),
  4374. 'foreignKey' => 'device_type_id',
  4375. 'fields' => '',
  4376. 'order' => '',
  4377. 'limit' => '',
  4378. 'offset' => '',
  4379. 'dependent' => '',
  4380. 'exclusive' => '',
  4381. 'finderQuery' => '',
  4382. 'counterQuery' => ''
  4383. );
  4384. $this->assertEquals($TestModel2->hasMany['NewFeatureSet'], $expected);
  4385. $this->assertTrue(is_object($TestModel2->NewFeatureSet));
  4386. }
  4387. /**
  4388. * testBindMultipleTimes method
  4389. *
  4390. * @return void
  4391. */
  4392. public function testBindMultipleTimes() {
  4393. $this->loadFixtures('User', 'Comment', 'Article', 'Tag', 'ArticlesTag');
  4394. $TestModel = new User();
  4395. $result = $TestModel->hasMany;
  4396. $expected = array();
  4397. $this->assertEquals($expected, $result);
  4398. $result = $TestModel->bindModel(array(
  4399. 'hasMany' => array(
  4400. 'Items' => array('className' => 'Comment')
  4401. )));
  4402. $this->assertTrue($result);
  4403. $result = $TestModel->find('all', array(
  4404. 'fields' => 'User.id, User.user'
  4405. ));
  4406. $expected = array(
  4407. array(
  4408. 'User' => array(
  4409. 'id' => '1',
  4410. 'user' => 'mariano'
  4411. ),
  4412. 'Items' => array(
  4413. array(
  4414. 'id' => '3',
  4415. 'article_id' => '1',
  4416. 'user_id' => '1',
  4417. 'comment' => 'Third Comment for First Article',
  4418. 'published' => 'Y',
  4419. 'created' => '2007-03-18 10:49:23',
  4420. 'updated' => '2007-03-18 10:51:31'
  4421. ),
  4422. array(
  4423. 'id' => '4',
  4424. 'article_id' => '1',
  4425. 'user_id' => '1',
  4426. 'comment' => 'Fourth Comment for First Article',
  4427. 'published' => 'N',
  4428. 'created' => '2007-03-18 10:51:23',
  4429. 'updated' => '2007-03-18 10:53:31'
  4430. ),
  4431. array(
  4432. 'id' => '5',
  4433. 'article_id' => '2',
  4434. 'user_id' => '1',
  4435. 'comment' => 'First Comment for Second Article',
  4436. 'published' => 'Y',
  4437. 'created' => '2007-03-18 10:53:23',
  4438. 'updated' => '2007-03-18 10:55:31'
  4439. ))),
  4440. array(
  4441. 'User' => array(
  4442. 'id' => '2',
  4443. 'user' => 'nate'
  4444. ),
  4445. 'Items' => array(
  4446. array(
  4447. 'id' => '1',
  4448. 'article_id' => '1',
  4449. 'user_id' => '2',
  4450. 'comment' => 'First Comment for First Article',
  4451. 'published' => 'Y',
  4452. 'created' => '2007-03-18 10:45:23',
  4453. 'updated' => '2007-03-18 10:47:31'
  4454. ),
  4455. array(
  4456. 'id' => '6',
  4457. 'article_id' => '2',
  4458. 'user_id' => '2',
  4459. 'comment' => 'Second Comment for Second Article',
  4460. 'published' => 'Y',
  4461. 'created' => '2007-03-18 10:55:23',
  4462. 'updated' => '2007-03-18 10:57:31'
  4463. ))),
  4464. array(
  4465. 'User' => array(
  4466. 'id' => '3',
  4467. 'user' => 'larry'
  4468. ),
  4469. 'Items' => array()
  4470. ),
  4471. array(
  4472. 'User' => array(
  4473. 'id' => '4',
  4474. 'user' => 'garrett'
  4475. ),
  4476. 'Items' => array(
  4477. array(
  4478. 'id' => '2',
  4479. 'article_id' => '1',
  4480. 'user_id' => '4',
  4481. 'comment' => 'Second Comment for First Article',
  4482. 'published' => 'Y',
  4483. 'created' => '2007-03-18 10:47:23',
  4484. 'updated' => '2007-03-18 10:49:31'
  4485. ))));
  4486. $this->assertEquals($expected, $result);
  4487. $result = $TestModel->bindModel(array(
  4488. 'hasMany' => array(
  4489. 'Items' => array('className' => 'Article')
  4490. )));
  4491. $this->assertTrue($result);
  4492. $result = $TestModel->find('all', array(
  4493. 'fields' => 'User.id, User.user'
  4494. ));
  4495. $expected = array(
  4496. array(
  4497. 'User' => array(
  4498. 'id' => '1',
  4499. 'user' => 'mariano'
  4500. ),
  4501. 'Items' => array(
  4502. array(
  4503. 'id' => 1,
  4504. 'user_id' => 1,
  4505. 'title' => 'First Article',
  4506. 'body' => 'First Article Body',
  4507. 'published' => 'Y',
  4508. 'created' => '2007-03-18 10:39:23',
  4509. 'updated' => '2007-03-18 10:41:31'
  4510. ),
  4511. array(
  4512. 'id' => 3,
  4513. 'user_id' => 1,
  4514. 'title' => 'Third Article',
  4515. 'body' => 'Third Article Body',
  4516. 'published' => 'Y',
  4517. 'created' => '2007-03-18 10:43:23',
  4518. 'updated' => '2007-03-18 10:45:31'
  4519. ))),
  4520. array(
  4521. 'User' => array(
  4522. 'id' => '2',
  4523. 'user' => 'nate'
  4524. ),
  4525. 'Items' => array()
  4526. ),
  4527. array(
  4528. 'User' => array(
  4529. 'id' => '3',
  4530. 'user' => 'larry'
  4531. ),
  4532. 'Items' => array(
  4533. array(
  4534. 'id' => 2,
  4535. 'user_id' => 3,
  4536. 'title' => 'Second Article',
  4537. 'body' => 'Second Article Body',
  4538. 'published' => 'Y',
  4539. 'created' => '2007-03-18 10:41:23',
  4540. 'updated' => '2007-03-18 10:43:31'
  4541. ))),
  4542. array(
  4543. 'User' => array(
  4544. 'id' => '4',
  4545. 'user' => 'garrett'
  4546. ),
  4547. 'Items' => array()
  4548. ));
  4549. $this->assertEquals($expected, $result);
  4550. }
  4551. /**
  4552. * test that multiple reset = true calls to bindModel() result in the original associations.
  4553. *
  4554. * @return void
  4555. */
  4556. public function testBindModelMultipleTimesResetCorrectly() {
  4557. $this->loadFixtures('User', 'Comment', 'Article');
  4558. $TestModel = new User();
  4559. $TestModel->bindModel(array('hasMany' => array('Comment')));
  4560. $TestModel->bindModel(array('hasMany' => array('Comment')));
  4561. $TestModel->resetAssociations();
  4562. $this->assertFalse(isset($TestModel->hasMany['Comment']), 'Association left behind');
  4563. }
  4564. /**
  4565. * testBindMultipleTimes method with different reset settings
  4566. *
  4567. * @return void
  4568. */
  4569. public function testBindMultipleTimesWithDifferentResetSettings() {
  4570. $this->loadFixtures('User', 'Comment', 'Article');
  4571. $TestModel = new User();
  4572. $result = $TestModel->hasMany;
  4573. $expected = array();
  4574. $this->assertEquals($expected, $result);
  4575. $result = $TestModel->bindModel(array(
  4576. 'hasMany' => array('Comment')
  4577. ));
  4578. $this->assertTrue($result);
  4579. $result = $TestModel->bindModel(
  4580. array('hasMany' => array('Article')),
  4581. false
  4582. );
  4583. $this->assertTrue($result);
  4584. $result = array_keys($TestModel->hasMany);
  4585. $expected = array('Comment', 'Article');
  4586. $this->assertEquals($expected, $result);
  4587. $TestModel->resetAssociations();
  4588. $result = array_keys($TestModel->hasMany);
  4589. $expected = array('Article');
  4590. $this->assertEquals($expected, $result);
  4591. }
  4592. /**
  4593. * test that bindModel behaves with Custom primary Key associations
  4594. *
  4595. * @return void
  4596. */
  4597. public function testBindWithCustomPrimaryKey() {
  4598. $this->loadFixtures('Story', 'StoriesTag', 'Tag');
  4599. $Model = ClassRegistry::init('StoriesTag');
  4600. $Model->bindModel(array(
  4601. 'belongsTo' => array(
  4602. 'Tag' => array(
  4603. 'className' => 'Tag',
  4604. 'foreignKey' => 'story'
  4605. ))));
  4606. $result = $Model->find('all');
  4607. $this->assertFalse(empty($result));
  4608. }
  4609. /**
  4610. * test that calling unbindModel() with reset == true multiple times
  4611. * leaves associations in the correct state.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testUnbindMultipleTimesResetCorrectly() {
  4616. $this->loadFixtures('User', 'Comment', 'Article');
  4617. $TestModel = new Article10();
  4618. $TestModel->unbindModel(array('hasMany' => array('Comment')));
  4619. $TestModel->unbindModel(array('hasMany' => array('Comment')));
  4620. $TestModel->resetAssociations();
  4621. $this->assertTrue(isset($TestModel->hasMany['Comment']), 'Association permanently removed');
  4622. }
  4623. /**
  4624. * testBindMultipleTimes method with different reset settings
  4625. *
  4626. * @return void
  4627. */
  4628. public function testUnBindMultipleTimesWithDifferentResetSettings() {
  4629. $this->loadFixtures('User', 'Comment', 'Article');
  4630. $TestModel = new Comment();
  4631. $result = array_keys($TestModel->belongsTo);
  4632. $expected = array('Article', 'User');
  4633. $this->assertEquals($expected, $result);
  4634. $result = $TestModel->unbindModel(array(
  4635. 'belongsTo' => array('User')
  4636. ));
  4637. $this->assertTrue($result);
  4638. $result = $TestModel->unbindModel(
  4639. array('belongsTo' => array('Article')),
  4640. false
  4641. );
  4642. $this->assertTrue($result);
  4643. $result = array_keys($TestModel->belongsTo);
  4644. $expected = array();
  4645. $this->assertEquals($expected, $result);
  4646. $TestModel->resetAssociations();
  4647. $result = array_keys($TestModel->belongsTo);
  4648. $expected = array('User');
  4649. $this->assertEquals($expected, $result);
  4650. }
  4651. /**
  4652. * testAssociationAfterFind method
  4653. *
  4654. * @return void
  4655. */
  4656. public function testAssociationAfterFind() {
  4657. $this->loadFixtures('Post', 'Author', 'Comment');
  4658. $TestModel = new Post();
  4659. $result = $TestModel->find('all');
  4660. $expected = array(
  4661. array(
  4662. 'Post' => array(
  4663. 'id' => '1',
  4664. 'author_id' => '1',
  4665. 'title' => 'First Post',
  4666. 'body' => 'First Post Body',
  4667. 'published' => 'Y',
  4668. 'created' => '2007-03-18 10:39:23',
  4669. 'updated' => '2007-03-18 10:41:31'
  4670. ),
  4671. 'Author' => array(
  4672. 'id' => '1',
  4673. 'user' => 'mariano',
  4674. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4675. 'created' => '2007-03-17 01:16:23',
  4676. 'updated' => '2007-03-17 01:18:31',
  4677. 'test' => 'working'
  4678. )),
  4679. array(
  4680. 'Post' => array(
  4681. 'id' => '2',
  4682. 'author_id' => '3',
  4683. 'title' => 'Second Post',
  4684. 'body' => 'Second Post Body',
  4685. 'published' => 'Y',
  4686. 'created' => '2007-03-18 10:41:23',
  4687. 'updated' => '2007-03-18 10:43:31'
  4688. ),
  4689. 'Author' => array(
  4690. 'id' => '3',
  4691. 'user' => 'larry',
  4692. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4693. 'created' => '2007-03-17 01:20:23',
  4694. 'updated' => '2007-03-17 01:22:31',
  4695. 'test' => 'working'
  4696. )),
  4697. array(
  4698. 'Post' => array(
  4699. 'id' => '3',
  4700. 'author_id' => '1',
  4701. 'title' => 'Third Post',
  4702. 'body' => 'Third Post Body',
  4703. 'published' => 'Y',
  4704. 'created' => '2007-03-18 10:43:23',
  4705. 'updated' => '2007-03-18 10:45:31'
  4706. ),
  4707. 'Author' => array(
  4708. 'id' => '1',
  4709. 'user' => 'mariano',
  4710. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4711. 'created' => '2007-03-17 01:16:23',
  4712. 'updated' => '2007-03-17 01:18:31',
  4713. 'test' => 'working'
  4714. )));
  4715. $this->assertEquals($expected, $result);
  4716. unset($TestModel);
  4717. $Author = new Author();
  4718. $Author->Post->bindModel(array(
  4719. 'hasMany' => array(
  4720. 'Comment' => array(
  4721. 'className' => 'ModifiedComment',
  4722. 'foreignKey' => 'article_id',
  4723. )
  4724. )));
  4725. $result = $Author->find('all', array(
  4726. 'conditions' => array('Author.id' => 1),
  4727. 'recursive' => 2
  4728. ));
  4729. $expected = array(
  4730. 'id' => 1,
  4731. 'article_id' => 1,
  4732. 'user_id' => 2,
  4733. 'comment' => 'First Comment for First Article',
  4734. 'published' => 'Y',
  4735. 'created' => '2007-03-18 10:45:23',
  4736. 'updated' => '2007-03-18 10:47:31',
  4737. 'callback' => 'Fire'
  4738. );
  4739. $this->assertEquals($result[0]['Post'][0]['Comment'][0], $expected);
  4740. }
  4741. /**
  4742. * Tests that callbacks can be properly disabled
  4743. *
  4744. * @return void
  4745. */
  4746. public function testCallbackDisabling() {
  4747. $this->loadFixtures('Author');
  4748. $TestModel = new ModifiedAuthor();
  4749. $result = Set::extract($TestModel->find('all'), '/Author/user');
  4750. $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
  4751. $this->assertEquals($expected, $result);
  4752. $result = Set::extract($TestModel->find('all', array('callbacks' => 'after')), '/Author/user');
  4753. $expected = array('mariano (CakePHP)', 'nate (CakePHP)', 'larry (CakePHP)', 'garrett (CakePHP)');
  4754. $this->assertEquals($expected, $result);
  4755. $result = Set::extract($TestModel->find('all', array('callbacks' => 'before')), '/Author/user');
  4756. $expected = array('mariano', 'nate', 'larry', 'garrett');
  4757. $this->assertEquals($expected, $result);
  4758. $result = Set::extract($TestModel->find('all', array('callbacks' => false)), '/Author/user');
  4759. $expected = array('mariano', 'nate', 'larry', 'garrett');
  4760. $this->assertEquals($expected, $result);
  4761. }
  4762. /**
  4763. * testAssociationAfterFindCallbacksDisabled method
  4764. *
  4765. * @return void
  4766. */
  4767. public function testAssociationAfterFindCalbacksDisabled() {
  4768. $this->loadFixtures('Post', 'Author', 'Comment');
  4769. $TestModel = new Post();
  4770. $result = $TestModel->find('all', array('callbacks' => false));
  4771. $expected = array(
  4772. array(
  4773. 'Post' => array(
  4774. 'id' => '1',
  4775. 'author_id' => '1',
  4776. 'title' => 'First Post',
  4777. 'body' => 'First Post Body',
  4778. 'published' => 'Y',
  4779. 'created' => '2007-03-18 10:39:23',
  4780. 'updated' => '2007-03-18 10:41:31'
  4781. ),
  4782. 'Author' => array(
  4783. 'id' => '1',
  4784. 'user' => 'mariano',
  4785. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4786. 'created' => '2007-03-17 01:16:23',
  4787. 'updated' => '2007-03-17 01:18:31'
  4788. )),
  4789. array(
  4790. 'Post' => array(
  4791. 'id' => '2',
  4792. 'author_id' => '3',
  4793. 'title' => 'Second Post',
  4794. 'body' => 'Second Post Body',
  4795. 'published' => 'Y',
  4796. 'created' => '2007-03-18 10:41:23',
  4797. 'updated' => '2007-03-18 10:43:31'
  4798. ),
  4799. 'Author' => array(
  4800. 'id' => '3',
  4801. 'user' => 'larry',
  4802. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4803. 'created' => '2007-03-17 01:20:23',
  4804. 'updated' => '2007-03-17 01:22:31'
  4805. )),
  4806. array(
  4807. 'Post' => array(
  4808. 'id' => '3',
  4809. 'author_id' => '1',
  4810. 'title' => 'Third Post',
  4811. 'body' => 'Third Post Body',
  4812. 'published' => 'Y',
  4813. 'created' => '2007-03-18 10:43:23',
  4814. 'updated' => '2007-03-18 10:45:31'
  4815. ),
  4816. 'Author' => array(
  4817. 'id' => '1',
  4818. 'user' => 'mariano',
  4819. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  4820. 'created' => '2007-03-17 01:16:23',
  4821. 'updated' => '2007-03-17 01:18:31'
  4822. )));
  4823. $this->assertEquals($expected, $result);
  4824. unset($TestModel);
  4825. $Author = new Author();
  4826. $Author->Post->bindModel(array(
  4827. 'hasMany' => array(
  4828. 'Comment' => array(
  4829. 'className' => 'ModifiedComment',
  4830. 'foreignKey' => 'article_id',
  4831. )
  4832. )));
  4833. $result = $Author->find('all', array(
  4834. 'conditions' => array('Author.id' => 1),
  4835. 'recursive' => 2,
  4836. 'callbacks' => false
  4837. ));
  4838. $expected = array(
  4839. 'id' => 1,
  4840. 'article_id' => 1,
  4841. 'user_id' => 2,
  4842. 'comment' => 'First Comment for First Article',
  4843. 'published' => 'Y',
  4844. 'created' => '2007-03-18 10:45:23',
  4845. 'updated' => '2007-03-18 10:47:31'
  4846. );
  4847. $this->assertEquals($result[0]['Post'][0]['Comment'][0], $expected);
  4848. }
  4849. /**
  4850. * Tests that the database configuration assigned to the model can be changed using
  4851. * (before|after)Find callbacks
  4852. *
  4853. * @return void
  4854. */
  4855. public function testCallbackSourceChange() {
  4856. $this->loadFixtures('Post');
  4857. $TestModel = new Post();
  4858. $this->assertEquals(3, count($TestModel->find('all')));
  4859. }
  4860. /**
  4861. * testCallbackSourceChangeUnknownDatasource method
  4862. *
  4863. * @expectedException MissingDatasourceConfigException
  4864. * @return void
  4865. */
  4866. public function testCallbackSourceChangeUnknownDatasource() {
  4867. $this->loadFixtures('Post');
  4868. $TestModel = new Post();
  4869. $this->assertFalse($TestModel->find('all', array('connection' => 'foo')));
  4870. }
  4871. /**
  4872. * testMultipleBelongsToWithSameClass method
  4873. *
  4874. * @return void
  4875. */
  4876. public function testMultipleBelongsToWithSameClass() {
  4877. $this->loadFixtures(
  4878. 'DeviceType',
  4879. 'DeviceTypeCategory',
  4880. 'FeatureSet',
  4881. 'ExteriorTypeCategory',
  4882. 'Document',
  4883. 'Device',
  4884. 'DocumentDirectory'
  4885. );
  4886. $DeviceType = new DeviceType();
  4887. $DeviceType->recursive = 2;
  4888. $result = $DeviceType->read(null, 1);
  4889. $expected = array(
  4890. 'DeviceType' => array(
  4891. 'id' => 1,
  4892. 'device_type_category_id' => 1,
  4893. 'feature_set_id' => 1,
  4894. 'exterior_type_category_id' => 1,
  4895. 'image_id' => 1,
  4896. 'extra1_id' => 1,
  4897. 'extra2_id' => 1,
  4898. 'name' => 'DeviceType 1',
  4899. 'order' => 0
  4900. ),
  4901. 'Image' => array(
  4902. 'id' => 1,
  4903. 'document_directory_id' => 1,
  4904. 'name' => 'Document 1',
  4905. 'DocumentDirectory' => array(
  4906. 'id' => 1,
  4907. 'name' => 'DocumentDirectory 1'
  4908. )),
  4909. 'Extra1' => array(
  4910. 'id' => 1,
  4911. 'document_directory_id' => 1,
  4912. 'name' => 'Document 1',
  4913. 'DocumentDirectory' => array(
  4914. 'id' => 1,
  4915. 'name' => 'DocumentDirectory 1'
  4916. )),
  4917. 'Extra2' => array(
  4918. 'id' => 1,
  4919. 'document_directory_id' => 1,
  4920. 'name' => 'Document 1',
  4921. 'DocumentDirectory' => array(
  4922. 'id' => 1,
  4923. 'name' => 'DocumentDirectory 1'
  4924. )),
  4925. 'DeviceTypeCategory' => array(
  4926. 'id' => 1,
  4927. 'name' => 'DeviceTypeCategory 1'
  4928. ),
  4929. 'FeatureSet' => array(
  4930. 'id' => 1,
  4931. 'name' => 'FeatureSet 1'
  4932. ),
  4933. 'ExteriorTypeCategory' => array(
  4934. 'id' => 1,
  4935. 'image_id' => 1,
  4936. 'name' => 'ExteriorTypeCategory 1',
  4937. 'Image' => array(
  4938. 'id' => 1,
  4939. 'device_type_id' => 1,
  4940. 'name' => 'Device 1',
  4941. 'typ' => 1
  4942. )),
  4943. 'Device' => array(
  4944. array(
  4945. 'id' => 1,
  4946. 'device_type_id' => 1,
  4947. 'name' => 'Device 1',
  4948. 'typ' => 1
  4949. ),
  4950. array(
  4951. 'id' => 2,
  4952. 'device_type_id' => 1,
  4953. 'name' => 'Device 2',
  4954. 'typ' => 1
  4955. ),
  4956. array(
  4957. 'id' => 3,
  4958. 'device_type_id' => 1,
  4959. 'name' => 'Device 3',
  4960. 'typ' => 2
  4961. )));
  4962. $this->assertEquals($expected, $result);
  4963. }
  4964. /**
  4965. * testHabtmRecursiveBelongsTo method
  4966. *
  4967. * @return void
  4968. */
  4969. public function testHabtmRecursiveBelongsTo() {
  4970. $this->loadFixtures('Portfolio', 'Item', 'ItemsPortfolio', 'Syfile', 'Image');
  4971. $Portfolio = new Portfolio();
  4972. $result = $Portfolio->find('first', array('conditions' => array('id' => 2), 'recursive' => 3));
  4973. $expected = array(
  4974. 'Portfolio' => array(
  4975. 'id' => 2,
  4976. 'seller_id' => 1,
  4977. 'name' => 'Portfolio 2'
  4978. ),
  4979. 'Item' => array(
  4980. array(
  4981. 'id' => 2,
  4982. 'syfile_id' => 2,
  4983. 'published' => false,
  4984. 'name' => 'Item 2',
  4985. 'ItemsPortfolio' => array(
  4986. 'id' => 2,
  4987. 'item_id' => 2,
  4988. 'portfolio_id' => 2
  4989. ),
  4990. 'Syfile' => array(
  4991. 'id' => 2,
  4992. 'image_id' => 2,
  4993. 'name' => 'Syfile 2',
  4994. 'item_count' => null,
  4995. 'Image' => array(
  4996. 'id' => 2,
  4997. 'name' => 'Image 2'
  4998. )
  4999. )),
  5000. array(
  5001. 'id' => 6,
  5002. 'syfile_id' => 6,
  5003. 'published' => false,
  5004. 'name' => 'Item 6',
  5005. 'ItemsPortfolio' => array(
  5006. 'id' => 6,
  5007. 'item_id' => 6,
  5008. 'portfolio_id' => 2
  5009. ),
  5010. 'Syfile' => array(
  5011. 'id' => 6,
  5012. 'image_id' => null,
  5013. 'name' => 'Syfile 6',
  5014. 'item_count' => null,
  5015. 'Image' => array()
  5016. ))));
  5017. $this->assertEquals($expected, $result);
  5018. }
  5019. /**
  5020. * testNonNumericHabtmJoinKey method
  5021. *
  5022. * @return void
  5023. */
  5024. public function testNonNumericHabtmJoinKey() {
  5025. $this->loadFixtures('Post', 'Tag', 'PostsTag', 'Author');
  5026. $Post = new Post();
  5027. $Post->bindModel(array(
  5028. 'hasAndBelongsToMany' => array('Tag')
  5029. ));
  5030. $Post->Tag->primaryKey = 'tag';
  5031. $result = $Post->find('all');
  5032. $expected = array(
  5033. array(
  5034. 'Post' => array(
  5035. 'id' => '1',
  5036. 'author_id' => '1',
  5037. 'title' => 'First Post',
  5038. 'body' => 'First Post Body',
  5039. 'published' => 'Y',
  5040. 'created' => '2007-03-18 10:39:23',
  5041. 'updated' => '2007-03-18 10:41:31'
  5042. ),
  5043. 'Author' => array(
  5044. 'id' => 1,
  5045. 'user' => 'mariano',
  5046. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5047. 'created' => '2007-03-17 01:16:23',
  5048. 'updated' => '2007-03-17 01:18:31',
  5049. 'test' => 'working'
  5050. ),
  5051. 'Tag' => array(
  5052. array(
  5053. 'id' => '1',
  5054. 'tag' => 'tag1',
  5055. 'created' => '2007-03-18 12:22:23',
  5056. 'updated' => '2007-03-18 12:24:31'
  5057. ),
  5058. array(
  5059. 'id' => '2',
  5060. 'tag' => 'tag2',
  5061. 'created' => '2007-03-18 12:24:23',
  5062. 'updated' => '2007-03-18 12:26:31'
  5063. ))),
  5064. array(
  5065. 'Post' => array(
  5066. 'id' => '2',
  5067. 'author_id' => '3',
  5068. 'title' => 'Second Post',
  5069. 'body' => 'Second Post Body',
  5070. 'published' => 'Y',
  5071. 'created' => '2007-03-18 10:41:23',
  5072. 'updated' => '2007-03-18 10:43:31'
  5073. ),
  5074. 'Author' => array(
  5075. 'id' => 3,
  5076. 'user' => 'larry',
  5077. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5078. 'created' => '2007-03-17 01:20:23',
  5079. 'updated' => '2007-03-17 01:22:31',
  5080. 'test' => 'working'
  5081. ),
  5082. 'Tag' => array(
  5083. array(
  5084. 'id' => '1',
  5085. 'tag' => 'tag1',
  5086. 'created' => '2007-03-18 12:22:23',
  5087. 'updated' => '2007-03-18 12:24:31'
  5088. ),
  5089. array(
  5090. 'id' => '3',
  5091. 'tag' => 'tag3',
  5092. 'created' => '2007-03-18 12:26:23',
  5093. 'updated' => '2007-03-18 12:28:31'
  5094. ))),
  5095. array(
  5096. 'Post' => array(
  5097. 'id' => '3',
  5098. 'author_id' => '1',
  5099. 'title' => 'Third Post',
  5100. 'body' => 'Third Post Body',
  5101. 'published' => 'Y',
  5102. 'created' => '2007-03-18 10:43:23',
  5103. 'updated' => '2007-03-18 10:45:31'
  5104. ),
  5105. 'Author' => array(
  5106. 'id' => 1,
  5107. 'user' => 'mariano',
  5108. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5109. 'created' => '2007-03-17 01:16:23',
  5110. 'updated' => '2007-03-17 01:18:31',
  5111. 'test' => 'working'
  5112. ),
  5113. 'Tag' => array()
  5114. ));
  5115. $this->assertEquals($expected, $result);
  5116. }
  5117. /**
  5118. * testHabtmFinderQuery method
  5119. *
  5120. * @return void
  5121. */
  5122. public function testHabtmFinderQuery() {
  5123. $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
  5124. $Article = new Article();
  5125. $sql = $this->db->buildStatement(
  5126. array(
  5127. 'fields' => $this->db->fields($Article->Tag, null, array(
  5128. 'Tag.id', 'Tag.tag', 'ArticlesTag.article_id', 'ArticlesTag.tag_id'
  5129. )),
  5130. 'table' => $this->db->fullTableName('tags'),
  5131. 'alias' => 'Tag',
  5132. 'limit' => null,
  5133. 'offset' => null,
  5134. 'group' => null,
  5135. 'joins' => array(array(
  5136. 'alias' => 'ArticlesTag',
  5137. 'table' => 'articles_tags',
  5138. 'conditions' => array(
  5139. array("ArticlesTag.article_id" => '{$__cakeID__$}'),
  5140. array("ArticlesTag.tag_id" => $this->db->identifier('Tag.id'))
  5141. )
  5142. )),
  5143. 'conditions' => array(),
  5144. 'order' => null
  5145. ),
  5146. $Article
  5147. );
  5148. $Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql;
  5149. $result = $Article->find('first');
  5150. $expected = array(
  5151. array(
  5152. 'id' => '1',
  5153. 'tag' => 'tag1'
  5154. ),
  5155. array(
  5156. 'id' => '2',
  5157. 'tag' => 'tag2'
  5158. ));
  5159. $this->assertEquals($result['Tag'], $expected);
  5160. }
  5161. /**
  5162. * testHabtmLimitOptimization method
  5163. *
  5164. * @return void
  5165. */
  5166. public function testHabtmLimitOptimization() {
  5167. $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
  5168. $TestModel = new Article();
  5169. $TestModel->hasAndBelongsToMany['Tag']['limit'] = 2;
  5170. $result = $TestModel->read(null, 2);
  5171. $expected = array(
  5172. 'Article' => array(
  5173. 'id' => '2',
  5174. 'user_id' => '3',
  5175. 'title' => 'Second Article',
  5176. 'body' => 'Second Article Body',
  5177. 'published' => 'Y',
  5178. 'created' => '2007-03-18 10:41:23',
  5179. 'updated' => '2007-03-18 10:43:31'
  5180. ),
  5181. 'User' => array(
  5182. 'id' => '3',
  5183. 'user' => 'larry',
  5184. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5185. 'created' => '2007-03-17 01:20:23',
  5186. 'updated' => '2007-03-17 01:22:31'
  5187. ),
  5188. 'Comment' => array(
  5189. array(
  5190. 'id' => '5',
  5191. 'article_id' => '2',
  5192. 'user_id' => '1',
  5193. 'comment' => 'First Comment for Second Article',
  5194. 'published' => 'Y',
  5195. 'created' => '2007-03-18 10:53:23',
  5196. 'updated' => '2007-03-18 10:55:31'
  5197. ),
  5198. array(
  5199. 'id' => '6',
  5200. 'article_id' => '2',
  5201. 'user_id' => '2',
  5202. 'comment' => 'Second Comment for Second Article',
  5203. 'published' => 'Y',
  5204. 'created' => '2007-03-18 10:55:23',
  5205. 'updated' => '2007-03-18 10:57:31'
  5206. )),
  5207. 'Tag' => array(
  5208. array(
  5209. 'id' => '1',
  5210. 'tag' => 'tag1',
  5211. 'created' => '2007-03-18 12:22:23',
  5212. 'updated' => '2007-03-18 12:24:31'
  5213. ),
  5214. array(
  5215. 'id' => '3',
  5216. 'tag' => 'tag3',
  5217. 'created' => '2007-03-18 12:26:23',
  5218. 'updated' => '2007-03-18 12:28:31'
  5219. )));
  5220. $this->assertEquals($expected, $result);
  5221. $TestModel->hasAndBelongsToMany['Tag']['limit'] = 1;
  5222. $result = $TestModel->read(null, 2);
  5223. unset($expected['Tag'][1]);
  5224. $this->assertEquals($expected, $result);
  5225. }
  5226. /**
  5227. * testHasManyLimitOptimization method
  5228. *
  5229. * @return void
  5230. */
  5231. public function testHasManyLimitOptimization() {
  5232. $this->loadFixtures('Project', 'Thread', 'Message', 'Bid');
  5233. $Project = new Project();
  5234. $Project->recursive = 3;
  5235. $result = $Project->find('all');
  5236. $expected = array(
  5237. array(
  5238. 'Project' => array(
  5239. 'id' => 1,
  5240. 'name' => 'Project 1'
  5241. ),
  5242. 'Thread' => array(
  5243. array(
  5244. 'id' => 1,
  5245. 'project_id' => 1,
  5246. 'name' => 'Project 1, Thread 1',
  5247. 'Project' => array(
  5248. 'id' => 1,
  5249. 'name' => 'Project 1',
  5250. 'Thread' => array(
  5251. array(
  5252. 'id' => 1,
  5253. 'project_id' => 1,
  5254. 'name' => 'Project 1, Thread 1'
  5255. ),
  5256. array(
  5257. 'id' => 2,
  5258. 'project_id' => 1,
  5259. 'name' => 'Project 1, Thread 2'
  5260. ))),
  5261. 'Message' => array(
  5262. array(
  5263. 'id' => 1,
  5264. 'thread_id' => 1,
  5265. 'name' => 'Thread 1, Message 1',
  5266. 'Bid' => array(
  5267. 'id' => 1,
  5268. 'message_id' => 1,
  5269. 'name' => 'Bid 1.1'
  5270. )))),
  5271. array(
  5272. 'id' => 2,
  5273. 'project_id' => 1,
  5274. 'name' => 'Project 1, Thread 2',
  5275. 'Project' => array(
  5276. 'id' => 1,
  5277. 'name' => 'Project 1',
  5278. 'Thread' => array(
  5279. array(
  5280. 'id' => 1,
  5281. 'project_id' => 1,
  5282. 'name' => 'Project 1, Thread 1'
  5283. ),
  5284. array(
  5285. 'id' => 2,
  5286. 'project_id' => 1,
  5287. 'name' => 'Project 1, Thread 2'
  5288. ))),
  5289. 'Message' => array(
  5290. array(
  5291. 'id' => 2,
  5292. 'thread_id' => 2,
  5293. 'name' => 'Thread 2, Message 1',
  5294. 'Bid' => array(
  5295. 'id' => 4,
  5296. 'message_id' => 2,
  5297. 'name' => 'Bid 2.1'
  5298. )))))),
  5299. array(
  5300. 'Project' => array(
  5301. 'id' => 2,
  5302. 'name' => 'Project 2'
  5303. ),
  5304. 'Thread' => array(
  5305. array(
  5306. 'id' => 3,
  5307. 'project_id' => 2,
  5308. 'name' => 'Project 2, Thread 1',
  5309. 'Project' => array(
  5310. 'id' => 2,
  5311. 'name' => 'Project 2',
  5312. 'Thread' => array(
  5313. array(
  5314. 'id' => 3,
  5315. 'project_id' => 2,
  5316. 'name' => 'Project 2, Thread 1'
  5317. ))),
  5318. 'Message' => array(
  5319. array(
  5320. 'id' => 3,
  5321. 'thread_id' => 3,
  5322. 'name' => 'Thread 3, Message 1',
  5323. 'Bid' => array(
  5324. 'id' => 3,
  5325. 'message_id' => 3,
  5326. 'name' => 'Bid 3.1'
  5327. )))))),
  5328. array(
  5329. 'Project' => array(
  5330. 'id' => 3,
  5331. 'name' => 'Project 3'
  5332. ),
  5333. 'Thread' => array()
  5334. ));
  5335. $this->assertEquals($expected, $result);
  5336. }
  5337. /**
  5338. * testFindAllRecursiveSelfJoin method
  5339. *
  5340. * @return void
  5341. */
  5342. public function testFindAllRecursiveSelfJoin() {
  5343. $this->loadFixtures('Home', 'AnotherArticle', 'Advertisement');
  5344. $TestModel = new Home();
  5345. $TestModel->recursive = 2;
  5346. $result = $TestModel->find('all');
  5347. $expected = array(
  5348. array(
  5349. 'Home' => array(
  5350. 'id' => '1',
  5351. 'another_article_id' => '1',
  5352. 'advertisement_id' => '1',
  5353. 'title' => 'First Home',
  5354. 'created' => '2007-03-18 10:39:23',
  5355. 'updated' => '2007-03-18 10:41:31'
  5356. ),
  5357. 'AnotherArticle' => array(
  5358. 'id' => '1',
  5359. 'title' => 'First Article',
  5360. 'created' => '2007-03-18 10:39:23',
  5361. 'updated' => '2007-03-18 10:41:31',
  5362. 'Home' => array(
  5363. array(
  5364. 'id' => '1',
  5365. 'another_article_id' => '1',
  5366. 'advertisement_id' => '1',
  5367. 'title' => 'First Home',
  5368. 'created' => '2007-03-18 10:39:23',
  5369. 'updated' => '2007-03-18 10:41:31'
  5370. ))),
  5371. 'Advertisement' => array(
  5372. 'id' => '1',
  5373. 'title' => 'First Ad',
  5374. 'created' => '2007-03-18 10:39:23',
  5375. 'updated' => '2007-03-18 10:41:31',
  5376. 'Home' => array(
  5377. array(
  5378. 'id' => '1',
  5379. 'another_article_id' => '1',
  5380. 'advertisement_id' => '1',
  5381. 'title' => 'First Home',
  5382. 'created' => '2007-03-18 10:39:23',
  5383. 'updated' => '2007-03-18 10:41:31'
  5384. ),
  5385. array(
  5386. 'id' => '2',
  5387. 'another_article_id' => '3',
  5388. 'advertisement_id' => '1',
  5389. 'title' => 'Second Home',
  5390. 'created' => '2007-03-18 10:41:23',
  5391. 'updated' => '2007-03-18 10:43:31'
  5392. )))),
  5393. array(
  5394. 'Home' => array(
  5395. 'id' => '2',
  5396. 'another_article_id' => '3',
  5397. 'advertisement_id' => '1',
  5398. 'title' => 'Second Home',
  5399. 'created' => '2007-03-18 10:41:23',
  5400. 'updated' => '2007-03-18 10:43:31'
  5401. ),
  5402. 'AnotherArticle' => array(
  5403. 'id' => '3',
  5404. 'title' => 'Third Article',
  5405. 'created' => '2007-03-18 10:43:23',
  5406. 'updated' => '2007-03-18 10:45:31',
  5407. 'Home' => array(
  5408. array(
  5409. 'id' => '2',
  5410. 'another_article_id' => '3',
  5411. 'advertisement_id' => '1',
  5412. 'title' => 'Second Home',
  5413. 'created' => '2007-03-18 10:41:23',
  5414. 'updated' => '2007-03-18 10:43:31'
  5415. ))),
  5416. 'Advertisement' => array(
  5417. 'id' => '1',
  5418. 'title' => 'First Ad',
  5419. 'created' => '2007-03-18 10:39:23',
  5420. 'updated' => '2007-03-18 10:41:31',
  5421. 'Home' => array(
  5422. array(
  5423. 'id' => '1',
  5424. 'another_article_id' => '1',
  5425. 'advertisement_id' => '1',
  5426. 'title' => 'First Home',
  5427. 'created' => '2007-03-18 10:39:23',
  5428. 'updated' => '2007-03-18 10:41:31'
  5429. ),
  5430. array(
  5431. 'id' => '2',
  5432. 'another_article_id' => '3',
  5433. 'advertisement_id' => '1',
  5434. 'title' => 'Second Home',
  5435. 'created' => '2007-03-18 10:41:23',
  5436. 'updated' => '2007-03-18 10:43:31'
  5437. )))));
  5438. $this->assertEquals($expected, $result);
  5439. }
  5440. /**
  5441. * testFindAllRecursiveWithHabtm method
  5442. *
  5443. * @return void
  5444. */
  5445. public function testFindAllRecursiveWithHabtm() {
  5446. $this->loadFixtures(
  5447. 'MyCategoriesMyUsers',
  5448. 'MyCategoriesMyProducts',
  5449. 'MyCategory',
  5450. 'MyUser',
  5451. 'MyProduct'
  5452. );
  5453. $MyUser = new MyUser();
  5454. $MyUser->recursive = 2;
  5455. $result = $MyUser->find('all');
  5456. $expected = array(
  5457. array(
  5458. 'MyUser' => array('id' => '1', 'firstname' => 'userA'),
  5459. 'MyCategory' => array(
  5460. array(
  5461. 'id' => '1',
  5462. 'name' => 'A',
  5463. 'MyProduct' => array(
  5464. array(
  5465. 'id' => '1',
  5466. 'name' => 'book'
  5467. ))),
  5468. array(
  5469. 'id' => '3',
  5470. 'name' => 'C',
  5471. 'MyProduct' => array(
  5472. array(
  5473. 'id' => '2',
  5474. 'name' => 'computer'
  5475. ))))),
  5476. array(
  5477. 'MyUser' => array(
  5478. 'id' => '2',
  5479. 'firstname' => 'userB'
  5480. ),
  5481. 'MyCategory' => array(
  5482. array(
  5483. 'id' => '1',
  5484. 'name' => 'A',
  5485. 'MyProduct' => array(
  5486. array(
  5487. 'id' => '1',
  5488. 'name' => 'book'
  5489. ))),
  5490. array(
  5491. 'id' => '2',
  5492. 'name' => 'B',
  5493. 'MyProduct' => array(
  5494. array(
  5495. 'id' => '1',
  5496. 'name' => 'book'
  5497. ),
  5498. array(
  5499. 'id' => '2',
  5500. 'name' => 'computer'
  5501. ))))));
  5502. $this->assertEquals($expected, $result);
  5503. }
  5504. /**
  5505. * testReadFakeThread method
  5506. *
  5507. * @return void
  5508. */
  5509. public function testReadFakeThread() {
  5510. $this->loadFixtures('CategoryThread');
  5511. $TestModel = new CategoryThread();
  5512. $fullDebug = $this->db->fullDebug;
  5513. $this->db->fullDebug = true;
  5514. $TestModel->recursive = 6;
  5515. $TestModel->id = 7;
  5516. $result = $TestModel->read();
  5517. $expected = array(
  5518. 'CategoryThread' => array(
  5519. 'id' => 7,
  5520. 'parent_id' => 6,
  5521. 'name' => 'Category 2.1',
  5522. 'created' => '2007-03-18 15:30:23',
  5523. 'updated' => '2007-03-18 15:32:31'
  5524. ),
  5525. 'ParentCategory' => array(
  5526. 'id' => 6,
  5527. 'parent_id' => 5,
  5528. 'name' => 'Category 2',
  5529. 'created' => '2007-03-18 15:30:23',
  5530. 'updated' => '2007-03-18 15:32:31',
  5531. 'ParentCategory' => array(
  5532. 'id' => 5,
  5533. 'parent_id' => 4,
  5534. 'name' => 'Category 1.1.1.1',
  5535. 'created' => '2007-03-18 15:30:23',
  5536. 'updated' => '2007-03-18 15:32:31',
  5537. 'ParentCategory' => array(
  5538. 'id' => 4,
  5539. 'parent_id' => 3,
  5540. 'name' => 'Category 1.1.2',
  5541. 'created' => '2007-03-18 15:30:23',
  5542. 'updated' => '2007-03-18 15:32:31',
  5543. 'ParentCategory' => array(
  5544. 'id' => 3,
  5545. 'parent_id' => 2,
  5546. 'name' => 'Category 1.1.1',
  5547. 'created' => '2007-03-18 15:30:23',
  5548. 'updated' => '2007-03-18 15:32:31',
  5549. 'ParentCategory' => array(
  5550. 'id' => 2,
  5551. 'parent_id' => 1,
  5552. 'name' => 'Category 1.1',
  5553. 'created' => '2007-03-18 15:30:23',
  5554. 'updated' => '2007-03-18 15:32:31',
  5555. 'ParentCategory' => array(
  5556. 'id' => 1,
  5557. 'parent_id' => 0,
  5558. 'name' => 'Category 1',
  5559. 'created' => '2007-03-18 15:30:23',
  5560. 'updated' => '2007-03-18 15:32:31'
  5561. )))))));
  5562. $this->db->fullDebug = $fullDebug;
  5563. $this->assertEquals($expected, $result);
  5564. }
  5565. /**
  5566. * testFindFakeThread method
  5567. *
  5568. * @return void
  5569. */
  5570. public function testFindFakeThread() {
  5571. $this->loadFixtures('CategoryThread');
  5572. $TestModel = new CategoryThread();
  5573. $fullDebug = $this->db->fullDebug;
  5574. $this->db->fullDebug = true;
  5575. $TestModel->recursive = 6;
  5576. $result = $TestModel->find('first', array('conditions' => array('CategoryThread.id' => 7)));
  5577. $expected = array(
  5578. 'CategoryThread' => array(
  5579. 'id' => 7,
  5580. 'parent_id' => 6,
  5581. 'name' => 'Category 2.1',
  5582. 'created' => '2007-03-18 15:30:23',
  5583. 'updated' => '2007-03-18 15:32:31'
  5584. ),
  5585. 'ParentCategory' => array(
  5586. 'id' => 6,
  5587. 'parent_id' => 5,
  5588. 'name' => 'Category 2',
  5589. 'created' => '2007-03-18 15:30:23',
  5590. 'updated' => '2007-03-18 15:32:31',
  5591. 'ParentCategory' => array(
  5592. 'id' => 5,
  5593. 'parent_id' => 4,
  5594. 'name' => 'Category 1.1.1.1',
  5595. 'created' => '2007-03-18 15:30:23',
  5596. 'updated' => '2007-03-18 15:32:31',
  5597. 'ParentCategory' => array(
  5598. 'id' => 4,
  5599. 'parent_id' => 3,
  5600. 'name' => 'Category 1.1.2',
  5601. 'created' => '2007-03-18 15:30:23',
  5602. 'updated' => '2007-03-18 15:32:31',
  5603. 'ParentCategory' => array(
  5604. 'id' => 3,
  5605. 'parent_id' => 2,
  5606. 'name' => 'Category 1.1.1',
  5607. 'created' => '2007-03-18 15:30:23',
  5608. 'updated' => '2007-03-18 15:32:31',
  5609. 'ParentCategory' => array(
  5610. 'id' => 2,
  5611. 'parent_id' => 1,
  5612. 'name' => 'Category 1.1',
  5613. 'created' => '2007-03-18 15:30:23',
  5614. 'updated' => '2007-03-18 15:32:31',
  5615. 'ParentCategory' => array(
  5616. 'id' => 1,
  5617. 'parent_id' => 0,
  5618. 'name' => 'Category 1',
  5619. 'created' => '2007-03-18 15:30:23',
  5620. 'updated' => '2007-03-18 15:32:31'
  5621. )))))));
  5622. $this->db->fullDebug = $fullDebug;
  5623. $this->assertEquals($expected, $result);
  5624. }
  5625. /**
  5626. * testFindAllFakeThread method
  5627. *
  5628. * @return void
  5629. */
  5630. public function testFindAllFakeThread() {
  5631. $this->loadFixtures('CategoryThread');
  5632. $TestModel = new CategoryThread();
  5633. $fullDebug = $this->db->fullDebug;
  5634. $this->db->fullDebug = true;
  5635. $TestModel->recursive = 6;
  5636. $result = $TestModel->find('all', null, null, 'CategoryThread.id ASC');
  5637. $expected = array(
  5638. array(
  5639. 'CategoryThread' => array(
  5640. 'id' => 1,
  5641. 'parent_id' => 0,
  5642. 'name' => 'Category 1',
  5643. 'created' => '2007-03-18 15:30:23',
  5644. 'updated' => '2007-03-18 15:32:31'
  5645. ),
  5646. 'ParentCategory' => array(
  5647. 'id' => null,
  5648. 'parent_id' => null,
  5649. 'name' => null,
  5650. 'created' => null,
  5651. 'updated' => null,
  5652. 'ParentCategory' => array()
  5653. )),
  5654. array(
  5655. 'CategoryThread' => array(
  5656. 'id' => 2,
  5657. 'parent_id' => 1,
  5658. 'name' => 'Category 1.1',
  5659. 'created' => '2007-03-18 15:30:23',
  5660. 'updated' => '2007-03-18 15:32:31'
  5661. ),
  5662. 'ParentCategory' => array(
  5663. 'id' => 1,
  5664. 'parent_id' => 0,
  5665. 'name' => 'Category 1',
  5666. 'created' => '2007-03-18 15:30:23',
  5667. 'updated' => '2007-03-18 15:32:31',
  5668. 'ParentCategory' => array()
  5669. )),
  5670. array(
  5671. 'CategoryThread' => array(
  5672. 'id' => 3,
  5673. 'parent_id' => 2,
  5674. 'name' => 'Category 1.1.1',
  5675. 'created' => '2007-03-18 15:30:23',
  5676. 'updated' => '2007-03-18 15:32:31'
  5677. ),
  5678. 'ParentCategory' => array(
  5679. 'id' => 2,
  5680. 'parent_id' => 1,
  5681. 'name' => 'Category 1.1',
  5682. 'created' => '2007-03-18 15:30:23',
  5683. 'updated' => '2007-03-18 15:32:31',
  5684. 'ParentCategory' => array(
  5685. 'id' => 1,
  5686. 'parent_id' => 0,
  5687. 'name' => 'Category 1',
  5688. 'created' => '2007-03-18 15:30:23',
  5689. 'updated' => '2007-03-18 15:32:31',
  5690. 'ParentCategory' => array()
  5691. ))),
  5692. array(
  5693. 'CategoryThread' => array(
  5694. 'id' => 4,
  5695. 'parent_id' => 3,
  5696. 'name' => 'Category 1.1.2',
  5697. 'created' => '2007-03-18 15:30:23',
  5698. 'updated' => '2007-03-18 15:32:31'
  5699. ),
  5700. 'ParentCategory' => array(
  5701. 'id' => 3,
  5702. 'parent_id' => 2,
  5703. 'name' => 'Category 1.1.1',
  5704. 'created' => '2007-03-18 15:30:23',
  5705. 'updated' => '2007-03-18 15:32:31',
  5706. 'ParentCategory' => array(
  5707. 'id' => 2,
  5708. 'parent_id' => 1,
  5709. 'name' => 'Category 1.1',
  5710. 'created' => '2007-03-18 15:30:23',
  5711. 'updated' => '2007-03-18 15:32:31',
  5712. 'ParentCategory' => array(
  5713. 'id' => 1,
  5714. 'parent_id' => 0,
  5715. 'name' => 'Category 1',
  5716. 'created' => '2007-03-18 15:30:23',
  5717. 'updated' => '2007-03-18 15:32:31',
  5718. 'ParentCategory' => array()
  5719. )))),
  5720. array(
  5721. 'CategoryThread' => array(
  5722. 'id' => 5,
  5723. 'parent_id' => 4,
  5724. 'name' => 'Category 1.1.1.1',
  5725. 'created' => '2007-03-18 15:30:23',
  5726. 'updated' => '2007-03-18 15:32:31'
  5727. ),
  5728. 'ParentCategory' => array(
  5729. 'id' => 4,
  5730. 'parent_id' => 3,
  5731. 'name' => 'Category 1.1.2',
  5732. 'created' => '2007-03-18 15:30:23',
  5733. 'updated' => '2007-03-18 15:32:31',
  5734. 'ParentCategory' => array(
  5735. 'id' => 3,
  5736. 'parent_id' => 2,
  5737. 'name' => 'Category 1.1.1',
  5738. 'created' => '2007-03-18 15:30:23',
  5739. 'updated' => '2007-03-18 15:32:31',
  5740. 'ParentCategory' => array(
  5741. 'id' => 2,
  5742. 'parent_id' => 1,
  5743. 'name' => 'Category 1.1',
  5744. 'created' => '2007-03-18 15:30:23',
  5745. 'updated' => '2007-03-18 15:32:31',
  5746. 'ParentCategory' => array(
  5747. 'id' => 1,
  5748. 'parent_id' => 0,
  5749. 'name' => 'Category 1',
  5750. 'created' => '2007-03-18 15:30:23',
  5751. 'updated' => '2007-03-18 15:32:31',
  5752. 'ParentCategory' => array()
  5753. ))))),
  5754. array(
  5755. 'CategoryThread' => array(
  5756. 'id' => 6,
  5757. 'parent_id' => 5,
  5758. 'name' => 'Category 2',
  5759. 'created' => '2007-03-18 15:30:23',
  5760. 'updated' => '2007-03-18 15:32:31'
  5761. ),
  5762. 'ParentCategory' => array(
  5763. 'id' => 5,
  5764. 'parent_id' => 4,
  5765. 'name' => 'Category 1.1.1.1',
  5766. 'created' => '2007-03-18 15:30:23',
  5767. 'updated' => '2007-03-18 15:32:31',
  5768. 'ParentCategory' => array(
  5769. 'id' => 4,
  5770. 'parent_id' => 3,
  5771. 'name' => 'Category 1.1.2',
  5772. 'created' => '2007-03-18 15:30:23',
  5773. 'updated' => '2007-03-18 15:32:31',
  5774. 'ParentCategory' => array(
  5775. 'id' => 3,
  5776. 'parent_id' => 2,
  5777. 'name' => 'Category 1.1.1',
  5778. 'created' => '2007-03-18 15:30:23',
  5779. 'updated' => '2007-03-18 15:32:31',
  5780. 'ParentCategory' => array(
  5781. 'id' => 2,
  5782. 'parent_id' => 1,
  5783. 'name' => 'Category 1.1',
  5784. 'created' => '2007-03-18 15:30:23',
  5785. 'updated' => '2007-03-18 15:32:31',
  5786. 'ParentCategory' => array(
  5787. 'id' => 1,
  5788. 'parent_id' => 0,
  5789. 'name' => 'Category 1',
  5790. 'created' => '2007-03-18 15:30:23',
  5791. 'updated' => '2007-03-18 15:32:31',
  5792. 'ParentCategory' => array()
  5793. )))))),
  5794. array(
  5795. 'CategoryThread' => array(
  5796. 'id' => 7,
  5797. 'parent_id' => 6,
  5798. 'name' => 'Category 2.1',
  5799. 'created' => '2007-03-18 15:30:23',
  5800. 'updated' => '2007-03-18 15:32:31'
  5801. ),
  5802. 'ParentCategory' => array(
  5803. 'id' => 6,
  5804. 'parent_id' => 5,
  5805. 'name' => 'Category 2',
  5806. 'created' => '2007-03-18 15:30:23',
  5807. 'updated' => '2007-03-18 15:32:31',
  5808. 'ParentCategory' => array(
  5809. 'id' => 5,
  5810. 'parent_id' => 4,
  5811. 'name' => 'Category 1.1.1.1',
  5812. 'created' => '2007-03-18 15:30:23',
  5813. 'updated' => '2007-03-18 15:32:31',
  5814. 'ParentCategory' => array(
  5815. 'id' => 4,
  5816. 'parent_id' => 3,
  5817. 'name' => 'Category 1.1.2',
  5818. 'created' => '2007-03-18 15:30:23',
  5819. 'updated' => '2007-03-18 15:32:31',
  5820. 'ParentCategory' => array(
  5821. 'id' => 3,
  5822. 'parent_id' => 2,
  5823. 'name' => 'Category 1.1.1',
  5824. 'created' => '2007-03-18 15:30:23',
  5825. 'updated' => '2007-03-18 15:32:31',
  5826. 'ParentCategory' => array(
  5827. 'id' => 2,
  5828. 'parent_id' => 1,
  5829. 'name' => 'Category 1.1',
  5830. 'created' => '2007-03-18 15:30:23',
  5831. 'updated' => '2007-03-18 15:32:31',
  5832. 'ParentCategory' => array(
  5833. 'id' => 1,
  5834. 'parent_id' => 0,
  5835. 'name' => 'Category 1',
  5836. 'created' => '2007-03-18 15:30:23',
  5837. 'updated' => '2007-03-18 15:32:31'
  5838. ))))))));
  5839. $this->db->fullDebug = $fullDebug;
  5840. $this->assertEquals($expected, $result);
  5841. }
  5842. /**
  5843. * testConditionalNumerics method
  5844. *
  5845. * @return void
  5846. */
  5847. public function testConditionalNumerics() {
  5848. $this->loadFixtures('NumericArticle');
  5849. $NumericArticle = new NumericArticle();
  5850. $data = array('conditions' => array('title' => '12345abcde'));
  5851. $result = $NumericArticle->find('first', $data);
  5852. $this->assertTrue(!empty($result));
  5853. $data = array('conditions' => array('title' => '12345'));
  5854. $result = $NumericArticle->find('first', $data);
  5855. $this->assertTrue(empty($result));
  5856. }
  5857. /**
  5858. * test buildQuery()
  5859. *
  5860. * @return void
  5861. */
  5862. public function testBuildQuery() {
  5863. $this->loadFixtures('User');
  5864. $TestModel = new User();
  5865. $TestModel->cacheQueries = false;
  5866. $expected = array(
  5867. 'conditions' => array(
  5868. 'user' => 'larry'),
  5869. 'fields' => NULL,
  5870. 'joins' => array(),
  5871. 'limit' => NULL,
  5872. 'offset' => NULL,
  5873. 'order' => array(
  5874. 0 => NULL),
  5875. 'page' => 1,
  5876. 'group' => NULL,
  5877. 'callbacks' => true,
  5878. 'returnQuery' => true);
  5879. $result = $TestModel->buildQuery('all', array('returnQuery' => true, 'conditions' => array('user' => 'larry')));
  5880. $this->assertEquals($expected, $result);
  5881. }
  5882. /**
  5883. * test find('all') method
  5884. *
  5885. * @return void
  5886. */
  5887. public function testFindAll() {
  5888. $this->loadFixtures('User');
  5889. $TestModel = new User();
  5890. $TestModel->cacheQueries = false;
  5891. $result = $TestModel->find('all');
  5892. $expected = array(
  5893. array(
  5894. 'User' => array(
  5895. 'id' => '1',
  5896. 'user' => 'mariano',
  5897. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5898. 'created' => '2007-03-17 01:16:23',
  5899. 'updated' => '2007-03-17 01:18:31'
  5900. )),
  5901. array(
  5902. 'User' => array(
  5903. 'id' => '2',
  5904. 'user' => 'nate',
  5905. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5906. 'created' => '2007-03-17 01:18:23',
  5907. 'updated' => '2007-03-17 01:20:31'
  5908. )),
  5909. array(
  5910. 'User' => array(
  5911. 'id' => '3',
  5912. 'user' => 'larry',
  5913. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5914. 'created' => '2007-03-17 01:20:23',
  5915. 'updated' => '2007-03-17 01:22:31'
  5916. )),
  5917. array(
  5918. 'User' => array(
  5919. 'id' => '4',
  5920. 'user' => 'garrett',
  5921. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5922. 'created' => '2007-03-17 01:22:23',
  5923. 'updated' => '2007-03-17 01:24:31'
  5924. )));
  5925. $this->assertEquals($expected, $result);
  5926. $result = $TestModel->find('all', array('conditions' => 'User.id > 2'));
  5927. $expected = array(
  5928. array(
  5929. 'User' => array(
  5930. 'id' => '3',
  5931. 'user' => 'larry',
  5932. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5933. 'created' => '2007-03-17 01:20:23',
  5934. 'updated' => '2007-03-17 01:22:31'
  5935. )),
  5936. array(
  5937. 'User' => array(
  5938. 'id' => '4',
  5939. 'user' => 'garrett',
  5940. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5941. 'created' => '2007-03-17 01:22:23',
  5942. 'updated' => '2007-03-17 01:24:31'
  5943. )));
  5944. $this->assertEquals($expected, $result);
  5945. $result = $TestModel->find('all', array(
  5946. 'conditions' => array('User.id !=' => '0', 'User.user LIKE' => '%arr%')
  5947. ));
  5948. $expected = array(
  5949. array(
  5950. 'User' => array(
  5951. 'id' => '3',
  5952. 'user' => 'larry',
  5953. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5954. 'created' => '2007-03-17 01:20:23',
  5955. 'updated' => '2007-03-17 01:22:31'
  5956. )),
  5957. array(
  5958. 'User' => array(
  5959. 'id' => '4',
  5960. 'user' => 'garrett',
  5961. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5962. 'created' => '2007-03-17 01:22:23',
  5963. 'updated' => '2007-03-17 01:24:31'
  5964. )));
  5965. $this->assertEquals($expected, $result);
  5966. $result = $TestModel->find('all', array('conditions' => array('User.id' => '0')));
  5967. $expected = array();
  5968. $this->assertEquals($expected, $result);
  5969. $result = $TestModel->find('all', array(
  5970. 'conditions' => array('or' => array('User.id' => '0', 'User.user LIKE' => '%a%')
  5971. )));
  5972. $expected = array(
  5973. array(
  5974. 'User' => array(
  5975. 'id' => '1',
  5976. 'user' => 'mariano',
  5977. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5978. 'created' => '2007-03-17 01:16:23',
  5979. 'updated' => '2007-03-17 01:18:31'
  5980. )),
  5981. array(
  5982. 'User' => array(
  5983. 'id' => '2',
  5984. 'user' => 'nate',
  5985. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5986. 'created' => '2007-03-17 01:18:23',
  5987. 'updated' => '2007-03-17 01:20:31'
  5988. )),
  5989. array(
  5990. 'User' => array(
  5991. 'id' => '3',
  5992. 'user' => 'larry',
  5993. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  5994. 'created' => '2007-03-17 01:20:23',
  5995. 'updated' => '2007-03-17 01:22:31'
  5996. )),
  5997. array(
  5998. 'User' => array(
  5999. 'id' => '4',
  6000. 'user' => 'garrett',
  6001. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6002. 'created' => '2007-03-17 01:22:23',
  6003. 'updated' => '2007-03-17 01:24:31'
  6004. )));
  6005. $this->assertEquals($expected, $result);
  6006. $result = $TestModel->find('all', array('fields' => 'User.id, User.user'));
  6007. $expected = array(
  6008. array('User' => array('id' => '1', 'user' => 'mariano')),
  6009. array('User' => array('id' => '2', 'user' => 'nate')),
  6010. array('User' => array('id' => '3', 'user' => 'larry')),
  6011. array('User' => array('id' => '4', 'user' => 'garrett')));
  6012. $this->assertEquals($expected, $result);
  6013. $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user ASC'));
  6014. $expected = array(
  6015. array('User' => array('user' => 'garrett')),
  6016. array('User' => array('user' => 'larry')),
  6017. array('User' => array('user' => 'mariano')),
  6018. array('User' => array('user' => 'nate')));
  6019. $this->assertEquals($expected, $result);
  6020. $result = $TestModel->find('all', array('fields' => 'User.user', 'order' => 'User.user DESC'));
  6021. $expected = array(
  6022. array('User' => array('user' => 'nate')),
  6023. array('User' => array('user' => 'mariano')),
  6024. array('User' => array('user' => 'larry')),
  6025. array('User' => array('user' => 'garrett')));
  6026. $this->assertEquals($expected, $result);
  6027. $result = $TestModel->find('all', array('limit' => 3, 'page' => 1));
  6028. $expected = array(
  6029. array(
  6030. 'User' => array(
  6031. 'id' => '1',
  6032. 'user' => 'mariano',
  6033. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6034. 'created' => '2007-03-17 01:16:23',
  6035. 'updated' => '2007-03-17 01:18:31'
  6036. )),
  6037. array(
  6038. 'User' => array(
  6039. 'id' => '2',
  6040. 'user' => 'nate',
  6041. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6042. 'created' => '2007-03-17 01:18:23',
  6043. 'updated' => '2007-03-17 01:20:31'
  6044. )),
  6045. array(
  6046. 'User' => array(
  6047. 'id' => '3',
  6048. 'user' => 'larry',
  6049. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6050. 'created' => '2007-03-17 01:20:23',
  6051. 'updated' => '2007-03-17 01:22:31'
  6052. )));
  6053. $this->assertEquals($expected, $result);
  6054. $ids = array(4 => 1, 5 => 3);
  6055. $result = $TestModel->find('all', array(
  6056. 'conditions' => array('User.id' => $ids),
  6057. 'order' => 'User.id'
  6058. ));
  6059. $expected = array(
  6060. array(
  6061. 'User' => array(
  6062. 'id' => '1',
  6063. 'user' => 'mariano',
  6064. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6065. 'created' => '2007-03-17 01:16:23',
  6066. 'updated' => '2007-03-17 01:18:31'
  6067. )),
  6068. array(
  6069. 'User' => array(
  6070. 'id' => '3',
  6071. 'user' => 'larry',
  6072. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6073. 'created' => '2007-03-17 01:20:23',
  6074. 'updated' => '2007-03-17 01:22:31'
  6075. )));
  6076. $this->assertEquals($expected, $result);
  6077. // These tests are expected to fail on SQL Server since the LIMIT/OFFSET
  6078. // hack can't handle small record counts.
  6079. if (!($this->db instanceof Sqlserver)) {
  6080. $result = $TestModel->find('all', array('limit' => 3, 'page' => 2));
  6081. $expected = array(
  6082. array(
  6083. 'User' => array(
  6084. 'id' => '4',
  6085. 'user' => 'garrett',
  6086. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6087. 'created' => '2007-03-17 01:22:23',
  6088. 'updated' => '2007-03-17 01:24:31'
  6089. )));
  6090. $this->assertEquals($expected, $result);
  6091. $result = $TestModel->find('all', array('limit' => 3, 'page' => 3));
  6092. $expected = array();
  6093. $this->assertEquals($expected, $result);
  6094. }
  6095. }
  6096. /**
  6097. * test find('list') method
  6098. *
  6099. * @return void
  6100. */
  6101. public function testGenerateFindList() {
  6102. $this->loadFixtures('Article', 'Apple', 'Post', 'Author', 'User', 'Comment');
  6103. $TestModel = new Article();
  6104. $TestModel->displayField = 'title';
  6105. $result = $TestModel->find('list', array(
  6106. 'order' => 'Article.title ASC'
  6107. ));
  6108. $expected = array(
  6109. 1 => 'First Article',
  6110. 2 => 'Second Article',
  6111. 3 => 'Third Article'
  6112. );
  6113. $this->assertEquals($expected, $result);
  6114. $db = ConnectionManager::getDataSource('test');
  6115. if ($db instanceof Mysql) {
  6116. $result = $TestModel->find('list', array(
  6117. 'order' => array('FIELD(Article.id, 3, 2) ASC', 'Article.title ASC')
  6118. ));
  6119. $expected = array(
  6120. 1 => 'First Article',
  6121. 3 => 'Third Article',
  6122. 2 => 'Second Article'
  6123. );
  6124. $this->assertEquals($expected, $result);
  6125. }
  6126. $result = Set::combine(
  6127. $TestModel->find('all', array(
  6128. 'order' => 'Article.title ASC',
  6129. 'fields' => array('id', 'title')
  6130. )),
  6131. '{n}.Article.id', '{n}.Article.title'
  6132. );
  6133. $expected = array(
  6134. 1 => 'First Article',
  6135. 2 => 'Second Article',
  6136. 3 => 'Third Article'
  6137. );
  6138. $this->assertEquals($expected, $result);
  6139. $result = Set::combine(
  6140. $TestModel->find('all', array(
  6141. 'order' => 'Article.title ASC'
  6142. )),
  6143. '{n}.Article.id', '{n}.Article'
  6144. );
  6145. $expected = array(
  6146. 1 => array(
  6147. 'id' => 1,
  6148. 'user_id' => 1,
  6149. 'title' => 'First Article',
  6150. 'body' => 'First Article Body',
  6151. 'published' => 'Y',
  6152. 'created' => '2007-03-18 10:39:23',
  6153. 'updated' => '2007-03-18 10:41:31'
  6154. ),
  6155. 2 => array(
  6156. 'id' => 2,
  6157. 'user_id' => 3,
  6158. 'title' => 'Second Article',
  6159. 'body' => 'Second Article Body',
  6160. 'published' => 'Y',
  6161. 'created' => '2007-03-18 10:41:23',
  6162. 'updated' => '2007-03-18 10:43:31'
  6163. ),
  6164. 3 => array(
  6165. 'id' => 3,
  6166. 'user_id' => 1,
  6167. 'title' => 'Third Article',
  6168. 'body' => 'Third Article Body',
  6169. 'published' => 'Y',
  6170. 'created' => '2007-03-18 10:43:23',
  6171. 'updated' => '2007-03-18 10:45:31'
  6172. ));
  6173. $this->assertEquals($expected, $result);
  6174. $result = Set::combine(
  6175. $TestModel->find('all', array(
  6176. 'order' => 'Article.title ASC'
  6177. )),
  6178. '{n}.Article.id', '{n}.Article', '{n}.Article.user_id'
  6179. );
  6180. $expected = array(
  6181. 1 => array(
  6182. 1 => array(
  6183. 'id' => 1,
  6184. 'user_id' => 1,
  6185. 'title' => 'First Article',
  6186. 'body' => 'First Article Body',
  6187. 'published' => 'Y',
  6188. 'created' => '2007-03-18 10:39:23',
  6189. 'updated' => '2007-03-18 10:41:31'
  6190. ),
  6191. 3 => array(
  6192. 'id' => 3,
  6193. 'user_id' => 1,
  6194. 'title' => 'Third Article',
  6195. 'body' => 'Third Article Body',
  6196. 'published' => 'Y',
  6197. 'created' => '2007-03-18 10:43:23',
  6198. 'updated' => '2007-03-18 10:45:31'
  6199. )),
  6200. 3 => array(
  6201. 2 => array(
  6202. 'id' => 2,
  6203. 'user_id' => 3,
  6204. 'title' => 'Second Article',
  6205. 'body' => 'Second Article Body',
  6206. 'published' => 'Y',
  6207. 'created' => '2007-03-18 10:41:23',
  6208. 'updated' => '2007-03-18 10:43:31'
  6209. )));
  6210. $this->assertEquals($expected, $result);
  6211. $result = Set::combine(
  6212. $TestModel->find('all', array(
  6213. 'order' => 'Article.title ASC',
  6214. 'fields' => array('id', 'title', 'user_id')
  6215. )),
  6216. '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id'
  6217. );
  6218. $expected = array(
  6219. 1 => array(
  6220. 1 => 'First Article',
  6221. 3 => 'Third Article'
  6222. ),
  6223. 3 => array(
  6224. 2 => 'Second Article'
  6225. ));
  6226. $this->assertEquals($expected, $result);
  6227. $TestModel = new Apple();
  6228. $expected = array(
  6229. 1 => 'Red Apple 1',
  6230. 2 => 'Bright Red Apple',
  6231. 3 => 'green blue',
  6232. 4 => 'Test Name',
  6233. 5 => 'Blue Green',
  6234. 6 => 'My new apple',
  6235. 7 => 'Some odd color'
  6236. );
  6237. $this->assertEquals($TestModel->find('list'), $expected);
  6238. $this->assertEquals($TestModel->Parent->find('list'), $expected);
  6239. $TestModel = new Post();
  6240. $result = $TestModel->find('list', array(
  6241. 'fields' => 'Post.title'
  6242. ));
  6243. $expected = array(
  6244. 1 => 'First Post',
  6245. 2 => 'Second Post',
  6246. 3 => 'Third Post'
  6247. );
  6248. $this->assertEquals($expected, $result);
  6249. $result = $TestModel->find('list', array(
  6250. 'fields' => 'title'
  6251. ));
  6252. $expected = array(
  6253. 1 => 'First Post',
  6254. 2 => 'Second Post',
  6255. 3 => 'Third Post'
  6256. );
  6257. $this->assertEquals($expected, $result);
  6258. $result = $TestModel->find('list', array(
  6259. 'fields' => array('title', 'id')
  6260. ));
  6261. $expected = array(
  6262. 'First Post' => '1',
  6263. 'Second Post' => '2',
  6264. 'Third Post' => '3'
  6265. );
  6266. $this->assertEquals($expected, $result);
  6267. $result = $TestModel->find('list', array(
  6268. 'fields' => array('title', 'id', 'created')
  6269. ));
  6270. $expected = array(
  6271. '2007-03-18 10:39:23' => array(
  6272. 'First Post' => '1'
  6273. ),
  6274. '2007-03-18 10:41:23' => array(
  6275. 'Second Post' => '2'
  6276. ),
  6277. '2007-03-18 10:43:23' => array(
  6278. 'Third Post' => '3'
  6279. ),
  6280. );
  6281. $this->assertEquals($expected, $result);
  6282. $result = $TestModel->find('list', array(
  6283. 'fields' => array('Post.body')
  6284. ));
  6285. $expected = array(
  6286. 1 => 'First Post Body',
  6287. 2 => 'Second Post Body',
  6288. 3 => 'Third Post Body'
  6289. );
  6290. $this->assertEquals($expected, $result);
  6291. $result = $TestModel->find('list', array(
  6292. 'fields' => array('Post.title', 'Post.body')
  6293. ));
  6294. $expected = array(
  6295. 'First Post' => 'First Post Body',
  6296. 'Second Post' => 'Second Post Body',
  6297. 'Third Post' => 'Third Post Body'
  6298. );
  6299. $this->assertEquals($expected, $result);
  6300. $result = $TestModel->find('list', array(
  6301. 'fields' => array('Post.id', 'Post.title', 'Author.user'),
  6302. 'recursive' => 1
  6303. ));
  6304. $expected = array(
  6305. 'mariano' => array(
  6306. 1 => 'First Post',
  6307. 3 => 'Third Post'
  6308. ),
  6309. 'larry' => array(
  6310. 2 => 'Second Post'
  6311. ));
  6312. $this->assertEquals($expected, $result);
  6313. $TestModel = new User();
  6314. $result = $TestModel->find('list', array(
  6315. 'fields' => array('User.user', 'User.password')
  6316. ));
  6317. $expected = array(
  6318. 'mariano' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6319. 'nate' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6320. 'larry' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6321. 'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99'
  6322. );
  6323. $this->assertEquals($expected, $result);
  6324. $TestModel = new ModifiedAuthor();
  6325. $result = $TestModel->find('list', array(
  6326. 'fields' => array('Author.id', 'Author.user')
  6327. ));
  6328. $expected = array(
  6329. 1 => 'mariano (CakePHP)',
  6330. 2 => 'nate (CakePHP)',
  6331. 3 => 'larry (CakePHP)',
  6332. 4 => 'garrett (CakePHP)'
  6333. );
  6334. $this->assertEquals($expected, $result);
  6335. $TestModel = new Article();
  6336. $TestModel->displayField = 'title';
  6337. $result = $TestModel->find('list', array(
  6338. 'conditions' => array('User.user' => 'mariano'),
  6339. 'recursive' => 0
  6340. ));
  6341. $expected = array(
  6342. 1 => 'First Article',
  6343. 3 => 'Third Article'
  6344. );
  6345. $this->assertEquals($expected, $result);
  6346. }
  6347. /**
  6348. * testFindField method
  6349. *
  6350. * @return void
  6351. */
  6352. public function testFindField() {
  6353. $this->loadFixtures('User');
  6354. $TestModel = new User();
  6355. $TestModel->id = 1;
  6356. $result = $TestModel->field('user');
  6357. $this->assertEquals($result, 'mariano');
  6358. $result = $TestModel->field('User.user');
  6359. $this->assertEquals($result, 'mariano');
  6360. $TestModel->id = false;
  6361. $result = $TestModel->field('user', array(
  6362. 'user' => 'mariano'
  6363. ));
  6364. $this->assertEquals($result, 'mariano');
  6365. $result = $TestModel->field('COUNT(*) AS count', true);
  6366. $this->assertEquals($result, 4);
  6367. $result = $TestModel->field('COUNT(*)', true);
  6368. $this->assertEquals($result, 4);
  6369. }
  6370. /**
  6371. * testFindUnique method
  6372. *
  6373. * @return void
  6374. */
  6375. public function testFindUnique() {
  6376. $this->loadFixtures('User');
  6377. $TestModel = new User();
  6378. $this->assertFalse($TestModel->isUnique(array(
  6379. 'user' => 'nate'
  6380. )));
  6381. $TestModel->id = 2;
  6382. $this->assertTrue($TestModel->isUnique(array(
  6383. 'user' => 'nate'
  6384. )));
  6385. $this->assertFalse($TestModel->isUnique(array(
  6386. 'user' => 'nate',
  6387. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'
  6388. )));
  6389. }
  6390. /**
  6391. * test find('count') method
  6392. *
  6393. * @return void
  6394. */
  6395. public function testFindCount() {
  6396. $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag');
  6397. $TestModel = new User();
  6398. $this->db->getLog(false, true);
  6399. $result = $TestModel->find('count');
  6400. $this->assertEquals($result, 4);
  6401. $this->db->getLog(false, true);
  6402. $fullDebug = $this->db->fullDebug;
  6403. $this->db->fullDebug = true;
  6404. $TestModel->order = 'User.id';
  6405. $result = $TestModel->find('count');
  6406. $this->assertEquals($result, 4);
  6407. $log = $this->db->getLog();
  6408. $this->assertTrue(isset($log['log'][0]['query']));
  6409. $this->assertNotRegExp('/ORDER\s+BY/', $log['log'][0]['query']);
  6410. $Article = new Article();
  6411. $Article->recursive = -1;
  6412. $expected = count($Article->find('all', array(
  6413. 'fields' => array('Article.user_id'),
  6414. 'group' => 'Article.user_id')
  6415. ));
  6416. $result = $Article->find('count', array('group' => array('Article.user_id')));
  6417. $this->assertEquals($expected, $result);
  6418. }
  6419. /**
  6420. * Test that find('first') does not use the id set to the object.
  6421. *
  6422. * @return void
  6423. */
  6424. public function testFindFirstNoIdUsed() {
  6425. $this->loadFixtures('Project');
  6426. $Project = new Project();
  6427. $Project->id = 3;
  6428. $result = $Project->find('first');
  6429. $this->assertEquals($result['Project']['name'], 'Project 1', 'Wrong record retrieved');
  6430. }
  6431. /**
  6432. * test find with COUNT(DISTINCT field)
  6433. *
  6434. * @return void
  6435. */
  6436. public function testFindCountDistinct() {
  6437. $this->skipIf($this->db instanceof Sqlite, 'SELECT COUNT(DISTINCT field) is not compatible with SQLite.');
  6438. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  6439. $this->loadFixtures('Project');
  6440. $TestModel = new Project();
  6441. $TestModel->create(array('name' => 'project')) && $TestModel->save();
  6442. $TestModel->create(array('name' => 'project')) && $TestModel->save();
  6443. $TestModel->create(array('name' => 'project')) && $TestModel->save();
  6444. $result = $TestModel->find('count', array('fields' => 'DISTINCT name'));
  6445. $this->assertEquals($result, 4);
  6446. }
  6447. /**
  6448. * Test find(count) with Db::expression
  6449. *
  6450. * @return void
  6451. */
  6452. public function testFindCountWithDbExpressions() {
  6453. $this->skipIf($this->db instanceof Postgres, 'testFindCountWithDbExpressions is not compatible with Postgres.');
  6454. $this->loadFixtures('Project', 'Thread');
  6455. $db = ConnectionManager::getDataSource('test');
  6456. $TestModel = new Project();
  6457. $result = $TestModel->find('count', array('conditions' => array(
  6458. $db->expression('Project.name = \'Project 3\'')
  6459. )));
  6460. $this->assertEquals($result, 1);
  6461. $result = $TestModel->find('count', array('conditions' => array(
  6462. 'Project.name' => $db->expression('\'Project 3\'')
  6463. )));
  6464. $this->assertEquals($result, 1);
  6465. }
  6466. /**
  6467. * testFindMagic method
  6468. *
  6469. * @return void
  6470. */
  6471. public function testFindMagic() {
  6472. $this->loadFixtures('User');
  6473. $TestModel = new User();
  6474. $result = $TestModel->findByUser('mariano');
  6475. $expected = array(
  6476. 'User' => array(
  6477. 'id' => '1',
  6478. 'user' => 'mariano',
  6479. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6480. 'created' => '2007-03-17 01:16:23',
  6481. 'updated' => '2007-03-17 01:18:31'
  6482. ));
  6483. $this->assertEquals($expected, $result);
  6484. $result = $TestModel->findByPassword('5f4dcc3b5aa765d61d8327deb882cf99');
  6485. $expected = array('User' => array(
  6486. 'id' => '1',
  6487. 'user' => 'mariano',
  6488. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6489. 'created' => '2007-03-17 01:16:23',
  6490. 'updated' => '2007-03-17 01:18:31'
  6491. ));
  6492. $this->assertEquals($expected, $result);
  6493. }
  6494. /**
  6495. * testRead method
  6496. *
  6497. * @return void
  6498. */
  6499. public function testRead() {
  6500. $this->loadFixtures('User', 'Article');
  6501. $TestModel = new User();
  6502. $result = $TestModel->read();
  6503. $this->assertFalse($result);
  6504. $TestModel->id = 2;
  6505. $result = $TestModel->read();
  6506. $expected = array(
  6507. 'User' => array(
  6508. 'id' => '2',
  6509. 'user' => 'nate',
  6510. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6511. 'created' => '2007-03-17 01:18:23',
  6512. 'updated' => '2007-03-17 01:20:31'
  6513. ));
  6514. $this->assertEquals($expected, $result);
  6515. $result = $TestModel->read(null, 2);
  6516. $expected = array(
  6517. 'User' => array(
  6518. 'id' => '2',
  6519. 'user' => 'nate',
  6520. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6521. 'created' => '2007-03-17 01:18:23',
  6522. 'updated' => '2007-03-17 01:20:31'
  6523. ));
  6524. $this->assertEquals($expected, $result);
  6525. $TestModel->id = 2;
  6526. $result = $TestModel->read(array('id', 'user'));
  6527. $expected = array('User' => array('id' => '2', 'user' => 'nate'));
  6528. $this->assertEquals($expected, $result);
  6529. $result = $TestModel->read('id, user', 2);
  6530. $expected = array(
  6531. 'User' => array(
  6532. 'id' => '2',
  6533. 'user' => 'nate'
  6534. ));
  6535. $this->assertEquals($expected, $result);
  6536. $result = $TestModel->bindModel(array('hasMany' => array('Article')));
  6537. $this->assertTrue($result);
  6538. $TestModel->id = 1;
  6539. $result = $TestModel->read('id, user');
  6540. $expected = array(
  6541. 'User' => array(
  6542. 'id' => '1',
  6543. 'user' => 'mariano'
  6544. ),
  6545. 'Article' => array(
  6546. array(
  6547. 'id' => '1',
  6548. 'user_id' => '1',
  6549. 'title' => 'First Article',
  6550. 'body' => 'First Article Body',
  6551. 'published' => 'Y',
  6552. 'created' => '2007-03-18 10:39:23',
  6553. 'updated' => '2007-03-18 10:41:31'
  6554. ),
  6555. array(
  6556. 'id' => '3',
  6557. 'user_id' => '1',
  6558. 'title' => 'Third Article',
  6559. 'body' => 'Third Article Body',
  6560. 'published' => 'Y',
  6561. 'created' => '2007-03-18 10:43:23',
  6562. 'updated' => '2007-03-18 10:45:31'
  6563. )));
  6564. $this->assertEquals($expected, $result);
  6565. }
  6566. /**
  6567. * testRecursiveRead method
  6568. *
  6569. * @return void
  6570. */
  6571. public function testRecursiveRead() {
  6572. $this->loadFixtures(
  6573. 'User',
  6574. 'Article',
  6575. 'Comment',
  6576. 'Tag',
  6577. 'ArticlesTag',
  6578. 'Featured',
  6579. 'ArticleFeatured'
  6580. );
  6581. $TestModel = new User();
  6582. $result = $TestModel->bindModel(array('hasMany' => array('Article')), false);
  6583. $this->assertTrue($result);
  6584. $TestModel->recursive = 0;
  6585. $result = $TestModel->read('id, user', 1);
  6586. $expected = array(
  6587. 'User' => array('id' => '1', 'user' => 'mariano'),
  6588. );
  6589. $this->assertEquals($expected, $result);
  6590. $TestModel->recursive = 1;
  6591. $result = $TestModel->read('id, user', 1);
  6592. $expected = array(
  6593. 'User' => array(
  6594. 'id' => '1',
  6595. 'user' => 'mariano'
  6596. ),
  6597. 'Article' => array(
  6598. array(
  6599. 'id' => '1',
  6600. 'user_id' => '1',
  6601. 'title' => 'First Article',
  6602. 'body' => 'First Article Body',
  6603. 'published' => 'Y',
  6604. 'created' => '2007-03-18 10:39:23',
  6605. 'updated' => '2007-03-18 10:41:31'
  6606. ),
  6607. array(
  6608. 'id' => '3',
  6609. 'user_id' => '1',
  6610. 'title' => 'Third Article',
  6611. 'body' => 'Third Article Body',
  6612. 'published' => 'Y',
  6613. 'created' => '2007-03-18 10:43:23',
  6614. 'updated' => '2007-03-18 10:45:31'
  6615. )));
  6616. $this->assertEquals($expected, $result);
  6617. $TestModel->recursive = 2;
  6618. $result = $TestModel->read('id, user', 3);
  6619. $expected = array(
  6620. 'User' => array(
  6621. 'id' => '3',
  6622. 'user' => 'larry'
  6623. ),
  6624. 'Article' => array(
  6625. array(
  6626. 'id' => '2',
  6627. 'user_id' => '3',
  6628. 'title' => 'Second Article',
  6629. 'body' => 'Second Article Body',
  6630. 'published' => 'Y',
  6631. 'created' => '2007-03-18 10:41:23',
  6632. 'updated' => '2007-03-18 10:43:31',
  6633. 'User' => array(
  6634. 'id' => '3',
  6635. 'user' => 'larry',
  6636. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6637. 'created' => '2007-03-17 01:20:23',
  6638. 'updated' => '2007-03-17 01:22:31'
  6639. ),
  6640. 'Comment' => array(
  6641. array(
  6642. 'id' => '5',
  6643. 'article_id' => '2',
  6644. 'user_id' => '1',
  6645. 'comment' => 'First Comment for Second Article',
  6646. 'published' => 'Y',
  6647. 'created' => '2007-03-18 10:53:23',
  6648. 'updated' => '2007-03-18 10:55:31'
  6649. ),
  6650. array(
  6651. 'id' => '6',
  6652. 'article_id' => '2',
  6653. 'user_id' => '2',
  6654. 'comment' => 'Second Comment for Second Article',
  6655. 'published' => 'Y',
  6656. 'created' => '2007-03-18 10:55:23',
  6657. 'updated' => '2007-03-18 10:57:31'
  6658. )),
  6659. 'Tag' => array(
  6660. array(
  6661. 'id' => '1',
  6662. 'tag' => 'tag1',
  6663. 'created' => '2007-03-18 12:22:23',
  6664. 'updated' => '2007-03-18 12:24:31'
  6665. ),
  6666. array(
  6667. 'id' => '3',
  6668. 'tag' => 'tag3',
  6669. 'created' => '2007-03-18 12:26:23',
  6670. 'updated' => '2007-03-18 12:28:31'
  6671. )))));
  6672. $this->assertEquals($expected, $result);
  6673. }
  6674. public function testRecursiveFindAll() {
  6675. $this->loadFixtures(
  6676. 'User',
  6677. 'Article',
  6678. 'Comment',
  6679. 'Tag',
  6680. 'ArticlesTag',
  6681. 'Attachment',
  6682. 'ArticleFeatured',
  6683. 'ArticleFeaturedsTags',
  6684. 'Featured',
  6685. 'Category'
  6686. );
  6687. $TestModel = new Article();
  6688. $result = $TestModel->find('all', array('conditions' => array('Article.user_id' => 1)));
  6689. $expected = array(
  6690. array(
  6691. 'Article' => array(
  6692. 'id' => '1',
  6693. 'user_id' => '1',
  6694. 'title' => 'First Article',
  6695. 'body' => 'First Article Body',
  6696. 'published' => 'Y',
  6697. 'created' => '2007-03-18 10:39:23',
  6698. 'updated' => '2007-03-18 10:41:31'
  6699. ),
  6700. 'User' => array(
  6701. 'id' => '1',
  6702. 'user' => 'mariano',
  6703. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6704. 'created' => '2007-03-17 01:16:23',
  6705. 'updated' => '2007-03-17 01:18:31'
  6706. ),
  6707. 'Comment' => array(
  6708. array(
  6709. 'id' => '1',
  6710. 'article_id' => '1',
  6711. 'user_id' => '2',
  6712. 'comment' => 'First Comment for First Article',
  6713. 'published' => 'Y',
  6714. 'created' => '2007-03-18 10:45:23',
  6715. 'updated' => '2007-03-18 10:47:31'
  6716. ),
  6717. array(
  6718. 'id' => '2',
  6719. 'article_id' => '1',
  6720. 'user_id' => '4',
  6721. 'comment' => 'Second Comment for First Article',
  6722. 'published' => 'Y',
  6723. 'created' => '2007-03-18 10:47:23',
  6724. 'updated' => '2007-03-18 10:49:31'
  6725. ),
  6726. array(
  6727. 'id' => '3',
  6728. 'article_id' => '1',
  6729. 'user_id' => '1',
  6730. 'comment' => 'Third Comment for First Article',
  6731. 'published' => 'Y',
  6732. 'created' => '2007-03-18 10:49:23',
  6733. 'updated' => '2007-03-18 10:51:31'
  6734. ),
  6735. array(
  6736. 'id' => '4',
  6737. 'article_id' => '1',
  6738. 'user_id' => '1',
  6739. 'comment' => 'Fourth Comment for First Article',
  6740. 'published' => 'N',
  6741. 'created' => '2007-03-18 10:51:23',
  6742. 'updated' => '2007-03-18 10:53:31'
  6743. )
  6744. ),
  6745. 'Tag' => array(
  6746. array(
  6747. 'id' => '1',
  6748. 'tag' => 'tag1',
  6749. 'created' => '2007-03-18 12:22:23',
  6750. 'updated' => '2007-03-18 12:24:31'
  6751. ),
  6752. array(
  6753. 'id' => '2',
  6754. 'tag' => 'tag2',
  6755. 'created' => '2007-03-18 12:24:23',
  6756. 'updated' => '2007-03-18 12:26:31'
  6757. ))),
  6758. array(
  6759. 'Article' => array(
  6760. 'id' => '3',
  6761. 'user_id' => '1',
  6762. 'title' => 'Third Article',
  6763. 'body' => 'Third Article Body',
  6764. 'published' => 'Y',
  6765. 'created' => '2007-03-18 10:43:23',
  6766. 'updated' => '2007-03-18 10:45:31'
  6767. ),
  6768. 'User' => array(
  6769. 'id' => '1',
  6770. 'user' => 'mariano',
  6771. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6772. 'created' => '2007-03-17 01:16:23',
  6773. 'updated' => '2007-03-17 01:18:31'
  6774. ),
  6775. 'Comment' => array(),
  6776. 'Tag' => array()
  6777. )
  6778. );
  6779. $this->assertEquals($expected, $result);
  6780. $result = $TestModel->find('all', array(
  6781. 'conditions' => array('Article.user_id' => 3),
  6782. 'limit' => 1,
  6783. 'recursive' => 2
  6784. ));
  6785. $expected = array(
  6786. array(
  6787. 'Article' => array(
  6788. 'id' => '2',
  6789. 'user_id' => '3',
  6790. 'title' => 'Second Article',
  6791. 'body' => 'Second Article Body',
  6792. 'published' => 'Y',
  6793. 'created' => '2007-03-18 10:41:23',
  6794. 'updated' => '2007-03-18 10:43:31'
  6795. ),
  6796. 'User' => array(
  6797. 'id' => '3',
  6798. 'user' => 'larry',
  6799. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6800. 'created' => '2007-03-17 01:20:23',
  6801. 'updated' => '2007-03-17 01:22:31'
  6802. ),
  6803. 'Comment' => array(
  6804. array(
  6805. 'id' => '5',
  6806. 'article_id' => '2',
  6807. 'user_id' => '1',
  6808. 'comment' => 'First Comment for Second Article',
  6809. 'published' => 'Y',
  6810. 'created' => '2007-03-18 10:53:23',
  6811. 'updated' => '2007-03-18 10:55:31',
  6812. 'Article' => array(
  6813. 'id' => '2',
  6814. 'user_id' => '3',
  6815. 'title' => 'Second Article',
  6816. 'body' => 'Second Article Body',
  6817. 'published' => 'Y',
  6818. 'created' => '2007-03-18 10:41:23',
  6819. 'updated' => '2007-03-18 10:43:31'
  6820. ),
  6821. 'User' => array(
  6822. 'id' => '1',
  6823. 'user' => 'mariano',
  6824. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6825. 'created' => '2007-03-17 01:16:23',
  6826. 'updated' => '2007-03-17 01:18:31'
  6827. ),
  6828. 'Attachment' => array(
  6829. 'id' => '1',
  6830. 'comment_id' => 5,
  6831. 'attachment' => 'attachment.zip',
  6832. 'created' => '2007-03-18 10:51:23',
  6833. 'updated' => '2007-03-18 10:53:31'
  6834. )
  6835. ),
  6836. array(
  6837. 'id' => '6',
  6838. 'article_id' => '2',
  6839. 'user_id' => '2',
  6840. 'comment' => 'Second Comment for Second Article',
  6841. 'published' => 'Y',
  6842. 'created' => '2007-03-18 10:55:23',
  6843. 'updated' => '2007-03-18 10:57:31',
  6844. 'Article' => array(
  6845. 'id' => '2',
  6846. 'user_id' => '3',
  6847. 'title' => 'Second Article',
  6848. 'body' => 'Second Article Body',
  6849. 'published' => 'Y',
  6850. 'created' => '2007-03-18 10:41:23',
  6851. 'updated' => '2007-03-18 10:43:31'
  6852. ),
  6853. 'User' => array(
  6854. 'id' => '2',
  6855. 'user' => 'nate',
  6856. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6857. 'created' => '2007-03-17 01:18:23',
  6858. 'updated' => '2007-03-17 01:20:31'
  6859. ),
  6860. 'Attachment' => array()
  6861. )
  6862. ),
  6863. 'Tag' => array(
  6864. array(
  6865. 'id' => '1',
  6866. 'tag' => 'tag1',
  6867. 'created' => '2007-03-18 12:22:23',
  6868. 'updated' => '2007-03-18 12:24:31'
  6869. ),
  6870. array(
  6871. 'id' => '3',
  6872. 'tag' => 'tag3',
  6873. 'created' => '2007-03-18 12:26:23',
  6874. 'updated' => '2007-03-18 12:28:31'
  6875. ))));
  6876. $this->assertEquals($expected, $result);
  6877. $Featured = new Featured();
  6878. $Featured->recursive = 2;
  6879. $Featured->bindModel(array(
  6880. 'belongsTo' => array(
  6881. 'ArticleFeatured' => array(
  6882. 'conditions' => "ArticleFeatured.published = 'Y'",
  6883. 'fields' => 'id, title, user_id, published'
  6884. )
  6885. )
  6886. ));
  6887. $Featured->ArticleFeatured->unbindModel(array(
  6888. 'hasMany' => array('Attachment', 'Comment'),
  6889. 'hasAndBelongsToMany' => array('Tag'))
  6890. );
  6891. $orderBy = 'ArticleFeatured.id ASC';
  6892. $result = $Featured->find('all', array(
  6893. 'order' => $orderBy, 'limit' => 3
  6894. ));
  6895. $expected = array(
  6896. array(
  6897. 'Featured' => array(
  6898. 'id' => '1',
  6899. 'article_featured_id' => '1',
  6900. 'category_id' => '1',
  6901. 'published_date' => '2007-03-31 10:39:23',
  6902. 'end_date' => '2007-05-15 10:39:23',
  6903. 'created' => '2007-03-18 10:39:23',
  6904. 'updated' => '2007-03-18 10:41:31'
  6905. ),
  6906. 'ArticleFeatured' => array(
  6907. 'id' => '1',
  6908. 'title' => 'First Article',
  6909. 'user_id' => '1',
  6910. 'published' => 'Y',
  6911. 'User' => array(
  6912. 'id' => '1',
  6913. 'user' => 'mariano',
  6914. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6915. 'created' => '2007-03-17 01:16:23',
  6916. 'updated' => '2007-03-17 01:18:31'
  6917. ),
  6918. 'Category' => array(),
  6919. 'Featured' => array(
  6920. 'id' => '1',
  6921. 'article_featured_id' => '1',
  6922. 'category_id' => '1',
  6923. 'published_date' => '2007-03-31 10:39:23',
  6924. 'end_date' => '2007-05-15 10:39:23',
  6925. 'created' => '2007-03-18 10:39:23',
  6926. 'updated' => '2007-03-18 10:41:31'
  6927. )),
  6928. 'Category' => array(
  6929. 'id' => '1',
  6930. 'parent_id' => '0',
  6931. 'name' => 'Category 1',
  6932. 'created' => '2007-03-18 15:30:23',
  6933. 'updated' => '2007-03-18 15:32:31'
  6934. )),
  6935. array(
  6936. 'Featured' => array(
  6937. 'id' => '2',
  6938. 'article_featured_id' => '2',
  6939. 'category_id' => '1',
  6940. 'published_date' => '2007-03-31 10:39:23',
  6941. 'end_date' => '2007-05-15 10:39:23',
  6942. 'created' => '2007-03-18 10:39:23',
  6943. 'updated' => '2007-03-18 10:41:31'
  6944. ),
  6945. 'ArticleFeatured' => array(
  6946. 'id' => '2',
  6947. 'title' => 'Second Article',
  6948. 'user_id' => '3',
  6949. 'published' => 'Y',
  6950. 'User' => array(
  6951. 'id' => '3',
  6952. 'user' => 'larry',
  6953. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  6954. 'created' => '2007-03-17 01:20:23',
  6955. 'updated' => '2007-03-17 01:22:31'
  6956. ),
  6957. 'Category' => array(),
  6958. 'Featured' => array(
  6959. 'id' => '2',
  6960. 'article_featured_id' => '2',
  6961. 'category_id' => '1',
  6962. 'published_date' => '2007-03-31 10:39:23',
  6963. 'end_date' => '2007-05-15 10:39:23',
  6964. 'created' => '2007-03-18 10:39:23',
  6965. 'updated' => '2007-03-18 10:41:31'
  6966. )),
  6967. 'Category' => array(
  6968. 'id' => '1',
  6969. 'parent_id' => '0',
  6970. 'name' => 'Category 1',
  6971. 'created' => '2007-03-18 15:30:23',
  6972. 'updated' => '2007-03-18 15:32:31'
  6973. )));
  6974. $this->assertEquals($expected, $result);
  6975. }
  6976. /**
  6977. * testRecursiveFindAllWithLimit method
  6978. *
  6979. * @return void
  6980. */
  6981. public function testRecursiveFindAllWithLimit() {
  6982. $this->loadFixtures('Article', 'User', 'Tag', 'ArticlesTag', 'Comment', 'Attachment');
  6983. $TestModel = new Article();
  6984. $TestModel->hasMany['Comment']['limit'] = 2;
  6985. $result = $TestModel->find('all', array(
  6986. 'conditions' => array('Article.user_id' => 1)
  6987. ));
  6988. $expected = array(
  6989. array(
  6990. 'Article' => array(
  6991. 'id' => '1',
  6992. 'user_id' => '1',
  6993. 'title' => 'First Article',
  6994. 'body' => 'First Article Body',
  6995. 'published' => 'Y',
  6996. 'created' => '2007-03-18 10:39:23',
  6997. 'updated' => '2007-03-18 10:41:31'
  6998. ),
  6999. 'User' => array(
  7000. 'id' => '1',
  7001. 'user' => 'mariano',
  7002. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  7003. 'created' => '2007-03-17 01:16:23',
  7004. 'updated' => '2007-03-17 01:18:31'
  7005. ),
  7006. 'Comment' => array(
  7007. array(
  7008. 'id' => '1',
  7009. 'article_id' => '1',
  7010. 'user_id' => '2',
  7011. 'comment' => 'First Comment for First Article',
  7012. 'published' => 'Y',
  7013. 'created' => '2007-03-18 10:45:23',
  7014. 'updated' => '2007-03-18 10:47:31'
  7015. ),
  7016. array(
  7017. 'id' => '2',
  7018. 'article_id' => '1',
  7019. 'user_id' => '4',
  7020. 'comment' => 'Second Comment for First Article',
  7021. 'published' => 'Y',
  7022. 'created' => '2007-03-18 10:47:23',
  7023. 'updated' => '2007-03-18 10:49:31'
  7024. ),
  7025. ),
  7026. 'Tag' => array(
  7027. array(
  7028. 'id' => '1',
  7029. 'tag' => 'tag1',
  7030. 'created' => '2007-03-18 12:22:23',
  7031. 'updated' => '2007-03-18 12:24:31'
  7032. ),
  7033. array(
  7034. 'id' => '2',
  7035. 'tag' => 'tag2',
  7036. 'created' => '2007-03-18 12:24:23',
  7037. 'updated' => '2007-03-18 12:26:31'
  7038. ))),
  7039. array(
  7040. 'Article' => array(
  7041. 'id' => '3',
  7042. 'user_id' => '1',
  7043. 'title' => 'Third Article',
  7044. 'body' => 'Third Article Body',
  7045. 'published' => 'Y',
  7046. 'created' => '2007-03-18 10:43:23',
  7047. 'updated' => '2007-03-18 10:45:31'
  7048. ),
  7049. 'User' => array(
  7050. 'id' => '1',
  7051. 'user' => 'mariano',
  7052. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  7053. 'created' => '2007-03-17 01:16:23',
  7054. 'updated' => '2007-03-17 01:18:31'
  7055. ),
  7056. 'Comment' => array(),
  7057. 'Tag' => array()
  7058. )
  7059. );
  7060. $this->assertEquals($expected, $result);
  7061. $TestModel->hasMany['Comment']['limit'] = 1;
  7062. $result = $TestModel->find('all', array(
  7063. 'conditions' => array('Article.user_id' => 3),
  7064. 'limit' => 1,
  7065. 'recursive' => 2
  7066. ));
  7067. $expected = array(
  7068. array(
  7069. 'Article' => array(
  7070. 'id' => '2',
  7071. 'user_id' => '3',
  7072. 'title' => 'Second Article',
  7073. 'body' => 'Second Article Body',
  7074. 'published' => 'Y',
  7075. 'created' => '2007-03-18 10:41:23',
  7076. 'updated' => '2007-03-18 10:43:31'
  7077. ),
  7078. 'User' => array(
  7079. 'id' => '3',
  7080. 'user' => 'larry',
  7081. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  7082. 'created' => '2007-03-17 01:20:23',
  7083. 'updated' => '2007-03-17 01:22:31'
  7084. ),
  7085. 'Comment' => array(
  7086. array(
  7087. 'id' => '5',
  7088. 'article_id' => '2',
  7089. 'user_id' => '1',
  7090. 'comment' => 'First Comment for Second Article',
  7091. 'published' => 'Y',
  7092. 'created' => '2007-03-18 10:53:23',
  7093. 'updated' => '2007-03-18 10:55:31',
  7094. 'Article' => array(
  7095. 'id' => '2',
  7096. 'user_id' => '3',
  7097. 'title' => 'Second Article',
  7098. 'body' => 'Second Article Body',
  7099. 'published' => 'Y',
  7100. 'created' => '2007-03-18 10:41:23',
  7101. 'updated' => '2007-03-18 10:43:31'
  7102. ),
  7103. 'User' => array(
  7104. 'id' => '1',
  7105. 'user' => 'mariano',
  7106. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  7107. 'created' => '2007-03-17 01:16:23',
  7108. 'updated' => '2007-03-17 01:18:31'
  7109. ),
  7110. 'Attachment' => array(
  7111. 'id' => '1',
  7112. 'comment_id' => 5,
  7113. 'attachment' => 'attachment.zip',
  7114. 'created' => '2007-03-18 10:51:23',
  7115. 'updated' => '2007-03-18 10:53:31'
  7116. )
  7117. )
  7118. ),
  7119. 'Tag' => array(
  7120. array(
  7121. 'id' => '1',
  7122. 'tag' => 'tag1',
  7123. 'created' => '2007-03-18 12:22:23',
  7124. 'updated' => '2007-03-18 12:24:31'
  7125. ),
  7126. array(
  7127. 'id' => '3',
  7128. 'tag' => 'tag3',
  7129. 'created' => '2007-03-18 12:26:23',
  7130. 'updated' => '2007-03-18 12:28:31'
  7131. )
  7132. )
  7133. )
  7134. );
  7135. $this->assertEquals($expected, $result);
  7136. }
  7137. /**
  7138. * Testing availability of $this->findQueryType in Model callbacks
  7139. *
  7140. * @return void
  7141. */
  7142. public function testFindQueryTypeInCallbacks() {
  7143. $this->loadFixtures('Comment');
  7144. $Comment = new AgainModifiedComment();
  7145. $comments = $Comment->find('all');
  7146. $this->assertEquals($comments[0]['Comment']['querytype'], 'all');
  7147. $comments = $Comment->find('first');
  7148. $this->assertEquals($comments['Comment']['querytype'], 'first');
  7149. }
  7150. /**
  7151. * testVirtualFields()
  7152. *
  7153. * Test correct fetching of virtual fields
  7154. * currently is not possible to do Relation.virtualField
  7155. *
  7156. * @return void
  7157. */
  7158. public function testVirtualFields() {
  7159. $this->loadFixtures('Post', 'Author');
  7160. $Post = ClassRegistry::init('Post');
  7161. $Post->virtualFields = array('two' => "1 + 1");
  7162. $result = $Post->find('first');
  7163. $this->assertEquals($result['Post']['two'], 2);
  7164. // SQL Server does not support operators in expressions
  7165. if (!($this->db instanceof Sqlserver)) {
  7166. $Post->Author->virtualFields = array('false' => '1 = 2');
  7167. $result = $Post->find('first');
  7168. $this->assertEquals($result['Post']['two'], 2);
  7169. $this->assertFalse((bool)$result['Author']['false']);
  7170. }
  7171. $result = $Post->find('first',array('fields' => array('author_id')));
  7172. $this->assertFalse(isset($result['Post']['two']));
  7173. $this->assertFalse(isset($result['Author']['false']));
  7174. $result = $Post->find('first',array('fields' => array('author_id', 'two')));
  7175. $this->assertEquals($result['Post']['two'], 2);
  7176. $this->assertFalse(isset($result['Author']['false']));
  7177. $result = $Post->find('first',array('fields' => array('two')));
  7178. $this->assertEquals($result['Post']['two'], 2);
  7179. $Post->id = 1;
  7180. $result = $Post->field('two');
  7181. $this->assertEquals($result, 2);
  7182. $result = $Post->find('first',array(
  7183. 'conditions' => array('two' => 2),
  7184. 'limit' => 1
  7185. ));
  7186. $this->assertEquals($result['Post']['two'], 2);
  7187. $result = $Post->find('first',array(
  7188. 'conditions' => array('two <' => 3),
  7189. 'limit' => 1
  7190. ));
  7191. $this->assertEquals($result['Post']['two'], 2);
  7192. $result = $Post->find('first',array(
  7193. 'conditions' => array('NOT' => array('two >' => 3)),
  7194. 'limit' => 1
  7195. ));
  7196. $this->assertEquals($result['Post']['two'], 2);
  7197. $dbo = $Post->getDataSource();
  7198. $Post->virtualFields = array('other_field' => 'Post.id + 1');
  7199. $result = $Post->find('first', array(
  7200. 'conditions' => array('other_field' => 3),
  7201. 'limit' => 1
  7202. ));
  7203. $this->assertEquals($result['Post']['id'], 2);
  7204. $Post->virtualFields = array('other_field' => 'Post.id + 1');
  7205. $result = $Post->find('all', array(
  7206. 'fields' => array($dbo->calculate($Post, 'max', array('other_field')))
  7207. ));
  7208. $this->assertEquals($result[0][0]['other_field'], 4);
  7209. ClassRegistry::flush();
  7210. $Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'), 'Model');
  7211. $Writing->virtualFields = array('two' => "1 + 1");
  7212. $result = $Writing->find('first');
  7213. $this->assertEquals($result['Writing']['two'], 2);
  7214. $Post->create();
  7215. $Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
  7216. $result = $Post->field('other_field');
  7217. $this->assertEquals($result, 4);
  7218. }
  7219. /**
  7220. * testVirtualFieldsOrder()
  7221. *
  7222. * Test correct order on virtual fields
  7223. *
  7224. * @return void
  7225. */
  7226. public function testVirtualFieldsOrder() {
  7227. $this->loadFixtures('Post', 'Author');
  7228. $Post = ClassRegistry::init('Post');
  7229. $Post->virtualFields = array('other_field' => '10 - Post.id');
  7230. $result = $Post->find('list', array('order' => array('Post.other_field' => 'ASC')));
  7231. $expected = array(
  7232. '3' => 'Third Post',
  7233. '2' => 'Second Post',
  7234. '1' => 'First Post'
  7235. );
  7236. $this->assertEquals($expected, $result);
  7237. $result = $Post->find('list', array('order' => array('Post.other_field' => 'DESC')));
  7238. $expected = array(
  7239. '1' => 'First Post',
  7240. '2' => 'Second Post',
  7241. '3' => 'Third Post'
  7242. );
  7243. $this->assertEquals($expected, $result);
  7244. $Post->Author->virtualFields = array('joined' => 'Post.id * Author.id');
  7245. $result = $Post->find('all');
  7246. $result = Set::extract('{n}.Author.joined', $result);
  7247. $expected = array(1, 6, 3);
  7248. $this->assertEquals($expected, $result);
  7249. $result = $Post->find('all', array('order' => array('Author.joined' => 'ASC')));
  7250. $result = Set::extract('{n}.Author.joined', $result);
  7251. $expected = array(1, 3, 6);
  7252. $this->assertEquals($expected, $result);
  7253. $result = $Post->find('all', array('order' => array('Author.joined' => 'DESC')));
  7254. $result = Set::extract('{n}.Author.joined', $result);
  7255. $expected = array(6, 3, 1);
  7256. $this->assertEquals($expected, $result);
  7257. }
  7258. /**
  7259. * testVirtualFieldsMysql()
  7260. *
  7261. * Test correct fetching of virtual fields
  7262. * currently is not possible to do Relation.virtualField
  7263. *
  7264. */
  7265. public function testVirtualFieldsMysql() {
  7266. $this->skipIf(!($this->db instanceof Mysql), 'The rest of virtualFieds test only compatible with Mysql.');
  7267. $this->loadFixtures('Post', 'Author');
  7268. $Post = ClassRegistry::init('Post');
  7269. $Post->create();
  7270. $Post->virtualFields = array(
  7271. 'low_title' => 'lower(Post.title)',
  7272. 'unique_test_field' => 'COUNT(Post.id)'
  7273. );
  7274. $expectation = array(
  7275. 'Post' => array(
  7276. 'low_title' => 'first post',
  7277. 'unique_test_field' => 1
  7278. )
  7279. );
  7280. $result = $Post->find('first', array(
  7281. 'fields' => array_keys($Post->virtualFields),
  7282. 'group' => array('low_title')
  7283. ));
  7284. $this->assertEquals($result, $expectation);
  7285. $Author = ClassRegistry::init('Author');
  7286. $Author->virtualFields = array(
  7287. 'full_name' => 'CONCAT(Author.user, " ", Author.id)'
  7288. );
  7289. $result = $Author->find('first', array(
  7290. 'conditions' => array('Author.user' => 'mariano'),
  7291. 'fields' => array('Author.password', 'Author.full_name'),
  7292. 'recursive' => -1
  7293. ));
  7294. $this->assertTrue(isset($result['Author']['full_name']));
  7295. $result = $Author->find('first', array(
  7296. 'conditions' => array('Author.user' => 'mariano'),
  7297. 'fields' => array('Author.full_name', 'Author.password'),
  7298. 'recursive' => -1
  7299. ));
  7300. $this->assertTrue(isset($result['Author']['full_name']));
  7301. }
  7302. /**
  7303. * test that virtual fields work when they don't contain functions.
  7304. *
  7305. * @return void
  7306. */
  7307. public function testVirtualFieldAsAString() {
  7308. $this->loadFixtures('Post', 'Author');
  7309. $Post = new Post();
  7310. $Post->virtualFields = array(
  7311. 'writer' => 'Author.user'
  7312. );
  7313. $result = $Post->find('first');
  7314. $this->assertTrue(isset($result['Post']['writer']), 'virtual field not fetched %s');
  7315. }
  7316. /**
  7317. * test that isVirtualField will accept both aliased and non aliased fieldnames
  7318. *
  7319. * @return void
  7320. */
  7321. public function testIsVirtualField() {
  7322. $this->loadFixtures('Post');
  7323. $Post = ClassRegistry::init('Post');
  7324. $Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
  7325. $this->assertTrue($Post->isVirtualField('other_field'));
  7326. $this->assertTrue($Post->isVirtualField('Post.other_field'));
  7327. $this->assertFalse($Post->isVirtualField('Comment.other_field'), 'Other models should not match.');
  7328. $this->assertFalse($Post->isVirtualField('id'));
  7329. $this->assertFalse($Post->isVirtualField('Post.id'));
  7330. $this->assertFalse($Post->isVirtualField(array()));
  7331. }
  7332. /**
  7333. * test that getting virtual fields works with and without model alias attached
  7334. *
  7335. * @return void
  7336. */
  7337. public function testGetVirtualField() {
  7338. $this->loadFixtures('Post');
  7339. $Post = ClassRegistry::init('Post');
  7340. $Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
  7341. $this->assertEquals($Post->getVirtualField('other_field'), $Post->virtualFields['other_field']);
  7342. $this->assertEquals($Post->getVirtualField('Post.other_field'), $Post->virtualFields['other_field']);
  7343. }
  7344. }