PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/framework/db/ar/CActiveRecordTest.php

https://bitbucket.org/penkoh/yii
PHP | 1404 lines | 1128 code | 204 blank | 72 comment | 3 complexity | d6c662445ba3de67ebf100267ce81d8e MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. Yii::import('system.db.CDbConnection');
  3. Yii::import('system.db.ar.CActiveRecord');
  4. require_once(dirname(__FILE__).'/../data/models.php');
  5. class CActiveRecordTest extends CTestCase
  6. {
  7. protected $backupStaticAttributes = true;
  8. private $_connection;
  9. protected function setUp()
  10. {
  11. if(!extension_loaded('pdo') || !extension_loaded('pdo_sqlite'))
  12. $this->markTestSkipped('PDO and SQLite extensions are required.');
  13. $this->_connection=new CDbConnection('sqlite::memory:');
  14. $this->_connection->active=true;
  15. $this->_connection->pdoInstance->exec(file_get_contents(dirname(__FILE__).'/../data/sqlite.sql'));
  16. CActiveRecord::$db=$this->_connection;
  17. }
  18. protected function tearDown()
  19. {
  20. $this->_connection->active=false;
  21. }
  22. public function testModel()
  23. {
  24. $model=Post::model();
  25. $this->assertTrue($model instanceof Post);
  26. $this->assertTrue($model->dbConnection===$this->_connection);
  27. $this->assertTrue($model->dbConnection->active);
  28. $this->assertEquals('posts',$model->tableName());
  29. $this->assertEquals('id',$model->tableSchema->primaryKey);
  30. $this->assertTrue($model->tableSchema->sequenceName==='');
  31. $this->assertEquals(array(),$model->attributeLabels());
  32. $this->assertEquals('Id',$model->getAttributeLabel('id'));
  33. $this->assertEquals('Author Id',$model->getAttributeLabel('author_id'));
  34. $this->assertTrue($model->getActiveRelation('author') instanceof CBelongsToRelation);
  35. $this->assertTrue($model->tableSchema instanceof CDbTableSchema);
  36. $this->assertTrue($model->commandBuilder instanceof CDbCommandBuilder);
  37. $this->assertTrue($model->hasAttribute('id'));
  38. $this->assertFalse($model->hasAttribute('comments'));
  39. $this->assertFalse($model->hasAttribute('foo'));
  40. $this->assertEquals(array('id'=>null,'title'=>null,'create_time'=>null,'author_id'=>null,'content'=>null),$model->attributes);
  41. $post=new Post;
  42. $this->assertNull($post->id);
  43. $this->assertNull($post->title);
  44. $post->setAttributes(array('id'=>3,'title'=>'test title'));
  45. $this->assertNull($post->id);
  46. $this->assertEquals('test title',$post->title);
  47. }
  48. public function testFind()
  49. {
  50. // test find() with various parameters
  51. $post=Post::model()->find();
  52. $this->assertTrue($post instanceof Post);
  53. $this->assertEquals(1,$post->id);
  54. $post=Post::model()->find('id=5');
  55. $this->assertTrue($post instanceof Post);
  56. $this->assertEquals(5,$post->id);
  57. $post=Post::model()->find('id=:id',array(':id'=>2));
  58. $this->assertTrue($post instanceof Post);
  59. $this->assertEquals(2,$post->id);
  60. $post=Post::model()->find(array('condition'=>'id=:id','params'=>array(':id'=>3)));
  61. $this->assertTrue($post instanceof Post);
  62. $this->assertEquals(3,$post->id);
  63. // test find() without result
  64. $post=Post::model()->find('id=6');
  65. $this->assertNull($post);
  66. // test findAll() with various parameters
  67. $posts=Post::model()->findAll();
  68. $this->assertEquals(5,count($posts));
  69. $this->assertTrue($posts[3] instanceof Post);
  70. $this->assertEquals(4,$posts[3]->id);
  71. $posts=Post::model()->findAll(new CDbCriteria(array('limit'=>3,'offset'=>1)));
  72. $this->assertEquals(3,count($posts));
  73. $this->assertTrue($posts[2] instanceof Post);
  74. $this->assertEquals(4,$posts[2]->id);
  75. // test findAll() without result
  76. $posts=Post::model()->findAll('id=6');
  77. $this->assertTrue($posts===array());
  78. // test findByPk
  79. $post=Post::model()->findByPk(2);
  80. $this->assertEquals(2,$post->id);
  81. $post=Post::model()->findByPk(array(3,2));
  82. $this->assertEquals(2,$post->id);
  83. $post=Post::model()->findByPk(array());
  84. $this->assertNull($post);
  85. $post=Post::model()->findByPk(null);
  86. $this->assertNull($post);
  87. $post=Post::model()->findByPk(6);
  88. $this->assertNull($post);
  89. // test findAllByPk
  90. $posts=Post::model()->findAllByPk(2);
  91. $this->assertEquals(1,count($posts));
  92. $this->assertEquals(2,$posts[0]->id);
  93. $posts=Post::model()->findAllByPk(array(4,3,2),'id<4');
  94. $this->assertEquals(2,count($posts));
  95. $this->assertEquals(2,$posts[0]->id);
  96. $this->assertEquals(3,$posts[1]->id);
  97. $posts=Post::model()->findAllByPk(array());
  98. $this->assertTrue($posts===array());
  99. // test findByAttributes
  100. $post=Post::model()->findByAttributes(array('author_id'=>2),array('order'=>'id DESC'));
  101. $this->assertEquals(4,$post->id);
  102. // test findAllByAttributes
  103. $posts=Post::model()->findAllByAttributes(array('author_id'=>2));
  104. $this->assertEquals(3,count($posts));
  105. // test findBySql
  106. $post=Post::model()->findBySql('select * from posts where id=:id',array(':id'=>2));
  107. $this->assertEquals(2,$post->id);
  108. // test findAllBySql
  109. $posts=Post::model()->findAllBySql('select * from posts where id>:id',array(':id'=>2));
  110. $this->assertEquals(3,count($posts));
  111. // test count
  112. $this->assertEquals(5,Post::model()->count());
  113. $this->assertEquals(3,Post::model()->count(array('condition'=>'id>2')));
  114. // test countBySql
  115. $this->assertEquals(1,Post::model()->countBySql('select id from posts limit 1'));
  116. // test exists
  117. $this->assertTrue(Post::model()->exists('id=:id',array(':id'=>1)));
  118. $this->assertFalse(Post::model()->exists('id=:id',array(':id'=>6)));
  119. }
  120. public function testInsert()
  121. {
  122. $post=new Post;
  123. $this->assertEquals(array('id'=>null,'title'=>null,'create_time'=>null,'author_id'=>null,'content'=>null),$post->attributes);
  124. $post->title='test post 1';
  125. $post->create_time=time();
  126. $post->author_id=1;
  127. $post->content='test post content 1';
  128. $this->assertTrue($post->isNewRecord);
  129. $this->assertNull($post->id);
  130. $this->assertTrue($post->save());
  131. $this->assertEquals(array(
  132. 'id'=>6,
  133. 'title'=>'test post 1',
  134. 'create_time'=>$post->create_time,
  135. 'author_id'=>1,
  136. 'content'=>'test post content 1'),$post->attributes);
  137. $this->assertFalse($post->isNewRecord);
  138. $this->assertEquals($post->attributes,Post::model()->findByPk($post->id)->attributes);
  139. }
  140. public function testUpdate()
  141. {
  142. // test save
  143. $post=Post::model()->findByPk(1);
  144. $this->assertFalse($post->isNewRecord);
  145. $this->assertEquals('post 1',$post->title);
  146. $post->title='test post 1';
  147. $this->assertTrue($post->save());
  148. $this->assertFalse($post->isNewRecord);
  149. $this->assertEquals('test post 1',$post->title);
  150. $this->assertEquals('test post 1',Post::model()->findByPk(1)->title);
  151. // test updateByPk
  152. $this->assertEquals(2,Post::model()->updateByPk(array(4,5),array('title'=>'test post')));
  153. $this->assertEquals('post 2',Post::model()->findByPk(2)->title);
  154. $this->assertEquals('test post',Post::model()->findByPk(4)->title);
  155. $this->assertEquals('test post',Post::model()->findByPk(5)->title);
  156. // test updateAll
  157. $this->assertEquals(1,Post::model()->updateAll(array('title'=>'test post'),'id=1'));
  158. $this->assertEquals('test post',Post::model()->findByPk(1)->title);
  159. // test updateCounters
  160. $this->assertEquals(2,Post::model()->findByPk(2)->author_id);
  161. $this->assertEquals(2,Post::model()->findByPk(3)->author_id);
  162. $this->assertEquals(2,Post::model()->findByPk(4)->author_id);
  163. $this->assertEquals(3,Post::model()->updateCounters(array('author_id'=>-1),'id>2'));
  164. $this->assertEquals(2,Post::model()->findByPk(2)->author_id);
  165. $this->assertEquals(1,Post::model()->findByPk(3)->author_id);
  166. }
  167. public function testSaveCounters()
  168. {
  169. $post=Post::model()->findByPk(2);
  170. $this->assertEquals(2, $post->author_id);
  171. $result=$post->saveCounters(array('author_id'=>-1));
  172. $this->assertTrue($result);
  173. $this->assertEquals(1, $post->author_id);
  174. $this->assertEquals(1, Post::model()->findByPk(2)->author_id);
  175. $this->assertEquals(2, Post::model()->findByPk(3)->author_id);
  176. }
  177. public function testDelete()
  178. {
  179. $post=Post::model()->findByPk(1);
  180. $this->assertTrue($post->delete());
  181. $this->assertNull(Post::model()->findByPk(1));
  182. $this->assertTrue(Post::model()->findByPk(2) instanceof Post);
  183. $this->assertTrue(Post::model()->findByPk(3) instanceof Post);
  184. $this->assertEquals(2,Post::model()->deleteByPk(array(2,3)));
  185. $this->assertNull(Post::model()->findByPk(2));
  186. $this->assertNull(Post::model()->findByPk(3));
  187. $this->assertTrue(Post::model()->findByPk(5) instanceof Post);
  188. $this->assertEquals(1,Post::model()->deleteAll('id=5'));
  189. $this->assertNull(Post::model()->findByPk(5));
  190. }
  191. public function testRefresh()
  192. {
  193. $post=Post::model()->findByPk(1);
  194. $post2=Post::model()->findByPk(1);
  195. $post2->title='new post';
  196. $post2->save();
  197. $this->assertEquals('post 1',$post->title);
  198. $this->assertTrue($post->refresh());
  199. $this->assertEquals('new post',$post->title);
  200. $post = new Post();
  201. $this->assertFalse($post->refresh());
  202. $post->id = 1;
  203. $this->assertTrue($post->refresh());
  204. $this->assertEquals('new post',$post->title);
  205. }
  206. public function testEquals()
  207. {
  208. $post=Post::model()->findByPk(1);
  209. $post2=Post::model()->findByPk(1);
  210. $post3=Post::model()->findByPk(3);
  211. $this->assertEquals(1,$post->primaryKey);
  212. $this->assertTrue($post->equals($post2));
  213. $this->assertTrue($post2->equals($post));
  214. $this->assertFalse($post->equals($post3));
  215. $this->assertFalse($post3->equals($post));
  216. }
  217. public function testValidation()
  218. {
  219. $user=new User;
  220. $user->password='passtest';
  221. $this->assertFalse($user->hasErrors());
  222. $this->assertEquals(array(),$user->errors);
  223. $this->assertEquals(array(),$user->getErrors('username'));
  224. $this->assertFalse($user->save());
  225. $this->assertNull($user->id);
  226. $this->assertTrue($user->isNewRecord);
  227. $this->assertTrue($user->hasErrors());
  228. $this->assertTrue($user->hasErrors('username'));
  229. $this->assertTrue($user->hasErrors('email'));
  230. $this->assertFalse($user->hasErrors('password'));
  231. $this->assertEquals(1,count($user->getErrors('username')));
  232. $this->assertEquals(1,count($user->getErrors('email')));
  233. $this->assertEquals(2,count($user->errors));
  234. $user->clearErrors();
  235. $this->assertFalse($user->hasErrors());
  236. $this->assertEquals(array(),$user->errors);
  237. }
  238. public function testCompositeKey()
  239. {
  240. $order=new Order;
  241. $this->assertEquals(array('key1','key2'),$order->tableSchema->primaryKey);
  242. $order=Order::model()->findByPk(array('key1'=>2,'key2'=>1));
  243. $this->assertEquals('order 21',$order->name);
  244. $orders=Order::model()->findAllByPk(array(array('key1'=>2,'key2'=>1),array('key1'=>1,'key2'=>3)));
  245. $this->assertEquals('order 13',$orders[0]->name);
  246. $this->assertEquals('order 21',$orders[1]->name);
  247. }
  248. public function testDefault()
  249. {
  250. $type=new ComplexType;
  251. $this->assertEquals(1,$type->int_col2);
  252. $this->assertEquals('something',$type->char_col2);
  253. $this->assertEquals(1.23,$type->float_col2);
  254. $this->assertEquals(33.22,$type->numeric_col);
  255. $this->assertEquals(123,$type->time);
  256. $this->assertNull($type->bool_col);
  257. $this->assertTrue($type->bool_col2);
  258. }
  259. public function testPublicAttribute()
  260. {
  261. $post=new PostExt;
  262. $this->assertEquals(array('id'=>null,'title'=>'default title','create_time'=>null,'author_id'=>null,'content'=>null),$post->attributes);
  263. $post=Post::model()->findByPk(1);
  264. $this->assertEquals(array(
  265. 'id'=>1,
  266. 'title'=>'post 1',
  267. 'create_time'=>100000,
  268. 'author_id'=>1,
  269. 'content'=>'content 1'),$post->attributes);
  270. $post=new PostExt;
  271. $post->title='test post';
  272. $post->create_time=1000000;
  273. $post->author_id=1;
  274. $post->content='test';
  275. $post->save();
  276. $this->assertEquals(array(
  277. 'id'=>6,
  278. 'title'=>'test post',
  279. 'create_time'=>1000000,
  280. 'author_id'=>1,
  281. 'content'=>'test'),$post->attributes);
  282. }
  283. public function testLazyRelation()
  284. {
  285. // test belongsTo
  286. $post=Post::model()->findByPk(2);
  287. $this->assertTrue($post->author instanceof User);
  288. $this->assertEquals(array(
  289. 'id'=>2,
  290. 'username'=>'user2',
  291. 'password'=>'pass2',
  292. 'email'=>'email2'),$post->author->attributes);
  293. // test hasOne
  294. $post=Post::model()->findByPk(2);
  295. $this->assertTrue($post->firstComment instanceof Comment);
  296. $this->assertEquals(array(
  297. 'id'=>4,
  298. 'content'=>'comment 4',
  299. 'post_id'=>2,
  300. 'author_id'=>2),$post->firstComment->attributes);
  301. $post=Post::model()->findByPk(4);
  302. $this->assertNull($post->firstComment);
  303. // test hasMany
  304. $post=Post::model()->findByPk(2);
  305. $this->assertEquals(2,count($post->comments));
  306. $this->assertEquals(array(
  307. 'id'=>5,
  308. 'content'=>'comment 5',
  309. 'post_id'=>2,
  310. 'author_id'=>2),$post->comments[0]->attributes);
  311. $this->assertEquals(array(
  312. 'id'=>4,
  313. 'content'=>'comment 4',
  314. 'post_id'=>2,
  315. 'author_id'=>2),$post->comments[1]->attributes);
  316. $post=Post::model()->findByPk(4);
  317. $this->assertEquals(array(),$post->comments);
  318. // test manyMany
  319. $post=Post::model()->findByPk(2);
  320. $this->assertEquals(2,count($post->categories));
  321. $this->assertEquals(array(
  322. 'id'=>4,
  323. 'name'=>'cat 4',
  324. 'parent_id'=>1),$post->categories[0]->attributes);
  325. $this->assertEquals(array(
  326. 'id'=>1,
  327. 'name'=>'cat 1',
  328. 'parent_id'=>null),$post->categories[1]->attributes);
  329. $post=Post::model()->findByPk(4);
  330. $this->assertEquals(array(),$post->categories);
  331. // test self join
  332. $category=Category::model()->findByPk(5);
  333. $this->assertEquals(array(),$category->posts);
  334. $this->assertEquals(2,count($category->children));
  335. $this->assertEquals(array(
  336. 'id'=>6,
  337. 'name'=>'cat 6',
  338. 'parent_id'=>5),$category->children[0]->attributes);
  339. $this->assertEquals(array(
  340. 'id'=>7,
  341. 'name'=>'cat 7',
  342. 'parent_id'=>5),$category->children[1]->attributes);
  343. $this->assertTrue($category->parent instanceof Category);
  344. $this->assertEquals(array(
  345. 'id'=>1,
  346. 'name'=>'cat 1',
  347. 'parent_id'=>null),$category->parent->attributes);
  348. $category=Category::model()->findByPk(2);
  349. $this->assertEquals(1,count($category->posts));
  350. $this->assertEquals(array(),$category->children);
  351. $this->assertNull($category->parent);
  352. // test composite key
  353. $order=Order::model()->findByPk(array('key1'=>1,'key2'=>2));
  354. $this->assertEquals(2,count($order->items));
  355. $order=Order::model()->findByPk(array('key1'=>2,'key2'=>1));
  356. $this->assertEquals(0,count($order->items));
  357. $item=Item::model()->findByPk(4);
  358. $this->assertTrue($item->order instanceof Order);
  359. $this->assertEquals(array(
  360. 'key1'=>2,
  361. 'key2'=>2,
  362. 'name'=>'order 22'),$item->order->attributes);
  363. }
  364. public function testEagerRelation2()
  365. {
  366. $post=Post::model()->with('author','firstComment','comments','categories')->findByPk(2);
  367. }
  368. private function checkEagerLoadedModel($post)
  369. {
  370. $this->assertEquals(array(
  371. 'id'=>2,
  372. 'username'=>'user2',
  373. 'password'=>'pass2',
  374. 'email'=>'email2'),$post->author->attributes);
  375. $this->assertTrue($post->firstComment instanceof Comment);
  376. $this->assertEquals(array(
  377. 'id'=>4,
  378. 'content'=>'comment 4',
  379. 'post_id'=>2,
  380. 'author_id'=>2),$post->firstComment->attributes);
  381. $this->assertEquals(2,count($post->comments));
  382. $this->assertEquals(array(
  383. 'id'=>5,
  384. 'content'=>'comment 5',
  385. 'post_id'=>2,
  386. 'author_id'=>2),$post->comments[0]->attributes);
  387. $this->assertEquals(array(
  388. 'id'=>4,
  389. 'content'=>'comment 4',
  390. 'post_id'=>2,
  391. 'author_id'=>2),$post->comments[1]->attributes);
  392. $this->assertEquals(2,count($post->categories));
  393. $this->assertEquals(array(
  394. 'id'=>4,
  395. 'name'=>'cat 4',
  396. 'parent_id'=>1),$post->categories[0]->attributes);
  397. $this->assertEquals(array(
  398. 'id'=>1,
  399. 'name'=>'cat 1',
  400. 'parent_id'=>null),$post->categories[1]->attributes);
  401. }
  402. public function testEagerRelation()
  403. {
  404. $post=Post::model()->with('author','firstComment','comments','categories')->findByPk(2);
  405. $this->checkEagerLoadedModel($post);
  406. $post=Post::model()->findByPk(2,array(
  407. 'with'=>array('author','firstComment','comments','categories'),
  408. ));
  409. $this->checkEagerLoadedModel($post);
  410. $post=Post::model()->with('author','firstComment','comments','categories')->findByPk(4);
  411. $this->assertEquals(array(
  412. 'id'=>2,
  413. 'username'=>'user2',
  414. 'password'=>'pass2',
  415. 'email'=>'email2'),$post->author->attributes);
  416. $this->assertNull($post->firstComment);
  417. $this->assertEquals(array(),$post->comments);
  418. $this->assertEquals(array(),$post->categories);
  419. }
  420. public function testLazyRecursiveRelation()
  421. {
  422. $post=PostExt::model()->findByPk(2);
  423. $this->assertEquals(2,count($post->comments));
  424. $this->assertTrue($post->comments[0]->post instanceof Post);
  425. $this->assertTrue($post->comments[1]->post instanceof Post);
  426. $this->assertTrue($post->comments[0]->author instanceof User);
  427. $this->assertTrue($post->comments[1]->author instanceof User);
  428. $this->assertEquals(3,count($post->comments[0]->author->posts));
  429. $this->assertEquals(3,count($post->comments[1]->author->posts));
  430. $this->assertTrue($post->comments[0]->author->posts[1]->author instanceof User);
  431. // test self join
  432. $category=Category::model()->findByPk(1);
  433. $this->assertEquals(2,count($category->nodes));
  434. $this->assertTrue($category->nodes[0]->parent instanceof Category);
  435. $this->assertTrue($category->nodes[1]->parent instanceof Category);
  436. $this->assertEquals(0,count($category->nodes[0]->children));
  437. $this->assertEquals(2,count($category->nodes[1]->children));
  438. }
  439. public function testEagerRecursiveRelation()
  440. {
  441. //$post=Post::model()->with(array('comments'=>'author','categories'))->findByPk(2);
  442. $post=Post::model()->with('comments.author','categories')->findByPk(2);
  443. $this->assertEquals(2,count($post->comments));
  444. $this->assertEquals(2,count($post->categories));
  445. $posts=PostExt::model()->with('comments')->findAll();
  446. $this->assertEquals(5,count($posts));
  447. }
  448. public function testRelationWithCondition()
  449. {
  450. $posts=Post::model()->with('comments')->findAllByPk(array(2,3,4),array('order'=>'t.id'));
  451. $this->assertEquals(3,count($posts));
  452. $this->assertEquals(2,count($posts[0]->comments));
  453. $this->assertEquals(4,count($posts[1]->comments));
  454. $this->assertEquals(0,count($posts[2]->comments));
  455. $post=Post::model()->with('comments')->findByAttributes(array('id'=>2));
  456. $this->assertTrue($post instanceof Post);
  457. $this->assertEquals(2,count($post->comments));
  458. $posts=Post::model()->with('comments')->findAllByAttributes(array('id'=>2));
  459. $this->assertEquals(1,count($posts));
  460. $post=Post::model()->with('comments')->findBySql('select * from posts where id=:id',array(':id'=>2));
  461. $this->assertTrue($post instanceof Post);
  462. $posts=Post::model()->with('comments')->findAllBySql('select * from posts where id=:id1 OR id=:id2',array(':id1'=>2,':id2'=>3));
  463. $this->assertEquals(2,count($posts));
  464. $post=Post::model()->with('comments','author')->find('t.id=:id',array(':id'=>2));
  465. $this->assertTrue($post instanceof Post);
  466. $posts=Post::model()->with('comments','author')->findAll(array(
  467. 'select'=>'title',
  468. 'condition'=>'t.id=:id',
  469. 'limit'=>1,
  470. 'offset'=>0,
  471. 'order'=>'t.title',
  472. 'group'=>'t.id',
  473. 'params'=>array(':id'=>2)));
  474. $this->assertTrue($posts[0] instanceof Post);
  475. $posts=Post::model()->with('comments','author')->findAll(array(
  476. 'select'=>'title',
  477. 'condition'=>'t.id=:id',
  478. 'limit'=>1,
  479. 'offset'=>2,
  480. 'order'=>'t.title',
  481. 'params'=>array(':id'=>2)));
  482. $this->assertTrue($posts===array());
  483. }
  484. public function testRelationWithColumnAlias()
  485. {
  486. $users=User::model()->with('posts')->findAll(array(
  487. 'select'=>'id, username AS username2',
  488. 'order'=>'username2',
  489. ));
  490. $this->assertEquals(4,count($users));
  491. $this->assertEquals($users[1]->username,null);
  492. $this->assertEquals($users[1]->username2,'user2');
  493. }
  494. public function testRelationalWithoutFK()
  495. {
  496. $users=UserNoFk::model()->with('posts')->findAll();
  497. $this->assertEquals(4,count($users));
  498. $this->assertEquals(3,count($users[1]->posts));
  499. $posts=PostNoFk::model()->with('author')->findAll();
  500. $this->assertEquals(5,count($posts));
  501. $this->assertTrue($posts[2]->author instanceof UserNoFk);
  502. }
  503. public function testRelationWithNewRecord()
  504. {
  505. $user=new User;
  506. $posts=$user->posts;
  507. $this->assertTrue(is_array($posts) && empty($posts));
  508. $post=new Post;
  509. $author=$post->author;
  510. $this->assertNull($author);
  511. }
  512. public function testRelationWithDynamicCondition()
  513. {
  514. $user=User::model()->with('posts')->findByPk(2);
  515. $this->assertEquals($user->posts[0]->id,2);
  516. $this->assertEquals($user->posts[1]->id,3);
  517. $this->assertEquals($user->posts[2]->id,4);
  518. $user=User::model()->with(array('posts'=>array('order'=>'posts.id DESC')))->findByPk(2);
  519. $this->assertEquals($user->posts[0]->id,4);
  520. $this->assertEquals($user->posts[1]->id,3);
  521. $this->assertEquals($user->posts[2]->id,2);
  522. }
  523. public function testEagerTogetherRelation()
  524. {
  525. $post=Post::model()->with('author','firstComment','comments','categories')->findByPk(2);
  526. $comments=$post->comments;
  527. $this->assertEquals(array(
  528. 'id'=>2,
  529. 'username'=>'user2',
  530. 'password'=>'pass2',
  531. 'email'=>'email2'),$post->author->attributes);
  532. $this->assertTrue($post->firstComment instanceof Comment);
  533. $this->assertEquals(array(
  534. 'id'=>4,
  535. 'content'=>'comment 4',
  536. 'post_id'=>2,
  537. 'author_id'=>2),$post->firstComment->attributes);
  538. $this->assertEquals(2,count($post->comments));
  539. $this->assertEquals(array(
  540. 'id'=>5,
  541. 'content'=>'comment 5',
  542. 'post_id'=>2,
  543. 'author_id'=>2),$post->comments[0]->attributes);
  544. $this->assertEquals(array(
  545. 'id'=>4,
  546. 'content'=>'comment 4',
  547. 'post_id'=>2,
  548. 'author_id'=>2),$post->comments[1]->attributes);
  549. $this->assertEquals(2,count($post->categories));
  550. $this->assertEquals(array(
  551. 'id'=>4,
  552. 'name'=>'cat 4',
  553. 'parent_id'=>1),$post->categories[0]->attributes);
  554. $this->assertEquals(array(
  555. 'id'=>1,
  556. 'name'=>'cat 1',
  557. 'parent_id'=>null),$post->categories[1]->attributes);
  558. $post=Post::model()->with('author','firstComment','comments','categories')->findByPk(4);
  559. $this->assertEquals(array(
  560. 'id'=>2,
  561. 'username'=>'user2',
  562. 'password'=>'pass2',
  563. 'email'=>'email2'),$post->author->attributes);
  564. $this->assertNull($post->firstComment);
  565. $this->assertEquals(array(),$post->comments);
  566. $this->assertEquals(array(),$post->categories);
  567. }
  568. public function testRelationalCount()
  569. {
  570. $count=Post::model()->with('author','firstComment','comments','categories')->count();
  571. $this->assertEquals(5,$count);
  572. $count=Post::model()->count(array('with'=>array('author','firstComment','comments','categories')));
  573. $this->assertEquals(5,$count);
  574. $count=Post::model()->with('author','firstComment','comments','categories')->count('t.id=4');
  575. $this->assertEquals(1,$count);
  576. $count=Post::model()->with('author','firstComment','comments','categories')->count('t.id=14');
  577. $this->assertEquals(0,$count);
  578. }
  579. public function testRelationalStat()
  580. {
  581. $users=User::model()->with('postCount')->findAll();
  582. $this->assertEquals(4,count($users));
  583. $this->assertEquals(1,$users[0]->postCount);
  584. $this->assertEquals(3,$users[1]->postCount);
  585. $this->assertEquals(1,$users[2]->postCount);
  586. $users=User::model()->findAll();
  587. $this->assertEquals(4,count($users));
  588. $this->assertEquals(1,$users[0]->postCount);
  589. $this->assertEquals(3,$users[1]->postCount);
  590. $this->assertEquals(1,$users[2]->postCount);
  591. $orders=Order::model()->with('itemCount')->findAll();
  592. $this->assertEquals(4,count($orders));
  593. $this->assertEquals(2,$orders[0]->itemCount);
  594. $this->assertEquals(1,$orders[1]->itemCount);
  595. $this->assertEquals(0,$orders[2]->itemCount);
  596. $this->assertEquals(2,$orders[3]->itemCount);
  597. $orders=Order::model()->findAll();
  598. $this->assertEquals(4,count($orders));
  599. $this->assertEquals(2,$orders[0]->itemCount);
  600. $this->assertEquals(1,$orders[1]->itemCount);
  601. $this->assertEquals(0,$orders[2]->itemCount);
  602. $this->assertEquals(2,$orders[3]->itemCount);
  603. $categories=Category::model()->with('postCount')->findAll();
  604. $this->assertEquals(7,count($categories));
  605. $this->assertEquals(3,$categories[0]->postCount);
  606. $this->assertEquals(1,$categories[1]->postCount);
  607. $this->assertEquals(1,$categories[2]->postCount);
  608. $this->assertEquals(1,$categories[3]->postCount);
  609. $this->assertEquals(0,$categories[4]->postCount);
  610. $this->assertEquals(0,$categories[5]->postCount);
  611. $this->assertEquals(0,$categories[6]->postCount);
  612. $categories=Category::model()->findAll();
  613. $this->assertEquals(7,count($categories));
  614. $this->assertEquals(3,$categories[0]->postCount);
  615. $this->assertEquals(1,$categories[1]->postCount);
  616. $this->assertEquals(1,$categories[2]->postCount);
  617. $this->assertEquals(1,$categories[3]->postCount);
  618. $this->assertEquals(0,$categories[4]->postCount);
  619. $this->assertEquals(0,$categories[5]->postCount);
  620. $this->assertEquals(0,$categories[6]->postCount);
  621. $users=User::model()->with('postCount','posts.commentCount')->findAll();
  622. $this->assertEquals(4,count($users));
  623. }
  624. public function testLazyLoadingWithConditions()
  625. {
  626. $user=User::model()->findByPk(2);
  627. $posts=$user->posts;
  628. $this->assertEquals(3,count($posts));
  629. $posts=$user->posts(array('condition'=>'posts.id>=3', 'alias'=>'posts'));
  630. $this->assertEquals(2,count($posts));
  631. }
  632. public function testDuplicateLazyLoadingBug()
  633. {
  634. $user=User::model()->with(array(
  635. 'posts'=>array('on'=>'posts.id=-1')
  636. ))->findByPk(1);
  637. // with the bug, an eager loading for 'posts' would be trigger in the following
  638. // and result with non-empty posts
  639. $this->assertTrue($user->posts===array());
  640. }
  641. public function testTogether()
  642. {
  643. // test without together
  644. $users=UserNoTogether::model()->with('posts.comments')->findAll();
  645. $postCount=0;
  646. $commentCount=0;
  647. foreach($users as $user)
  648. {
  649. $postCount+=count($user->posts);
  650. foreach($posts=$user->posts as $post)
  651. $commentCount+=count($post->comments);
  652. }
  653. $this->assertEquals(4,count($users));
  654. $this->assertEquals(5,$postCount);
  655. $this->assertEquals(10,$commentCount);
  656. // test with together
  657. $users=UserNoTogether::model()->with('posts.comments')->together()->findAll();
  658. $postCount=0;
  659. $commentCount=0;
  660. foreach($users as $user)
  661. {
  662. $postCount+=count($user->posts);
  663. foreach($posts=$user->posts as $post)
  664. $commentCount+=count($post->comments);
  665. }
  666. $this->assertEquals(3,count($users));
  667. $this->assertEquals(4,$postCount);
  668. $this->assertEquals(10,$commentCount);
  669. }
  670. public function testTogetherWithOption()
  671. {
  672. // test with together off option
  673. $users=User::model()->with(array(
  674. 'posts'=>array(
  675. 'with'=>array(
  676. 'comments'=>array(
  677. 'joinType'=>'INNER JOIN',
  678. 'together'=>false,
  679. ),
  680. ),
  681. 'joinType'=>'INNER JOIN',
  682. 'together'=>false,
  683. ),
  684. ))->findAll();
  685. $postCount=0;
  686. $commentCount=0;
  687. foreach($users as $user)
  688. {
  689. $postCount+=count($user->posts);
  690. foreach($posts=$user->posts as $post)
  691. $commentCount+=count($post->comments);
  692. }
  693. $this->assertEquals(4,count($users));
  694. $this->assertEquals(5,$postCount);
  695. $this->assertEquals(10,$commentCount);
  696. // test with together on option
  697. $users=User::model()->with(array(
  698. 'posts'=>array(
  699. 'with'=>array(
  700. 'comments'=>array(
  701. 'joinType'=>'INNER JOIN',
  702. 'together'=>true,
  703. ),
  704. ),
  705. 'joinType'=>'INNER JOIN',
  706. 'together'=>true,
  707. ),
  708. ))->findAll();
  709. $postCount=0;
  710. $commentCount=0;
  711. foreach($users as $user)
  712. {
  713. $postCount+=count($user->posts);
  714. foreach($posts=$user->posts as $post)
  715. $commentCount+=count($post->comments);
  716. }
  717. $this->assertEquals(3,count($users));
  718. $this->assertEquals(4,$postCount);
  719. $this->assertEquals(10,$commentCount);
  720. }
  721. public function testCountByAttributes()
  722. {
  723. $n=Post::model()->countByAttributes(array('author_id'=>2));
  724. $this->assertEquals(3,$n);
  725. }
  726. public function testScopes()
  727. {
  728. $models1=Post::model()->post23()->findAll();
  729. $models2=Post::model()->findAll(array('scopes'=>'post23'));
  730. foreach(array($models1,$models2) as $models)
  731. {
  732. $this->assertEquals(2,count($models));
  733. $this->assertEquals(2,$models[0]->id);
  734. $this->assertEquals(3,$models[1]->id);
  735. }
  736. $model1=Post::model()->post23()->find();
  737. $model2=Post::model()->find(array('scopes'=>'post23'));
  738. foreach(array($model1,$model2) as $model)
  739. $this->assertEquals(2,$model->id);
  740. $models1=Post::model()->post23()->post3()->findAll();
  741. $models2=Post::model()->findAll(array('scopes'=>array('post23','post3')));
  742. foreach(array($models1,$models2) as $models)
  743. {
  744. $this->assertEquals(1,count($models));
  745. $this->assertEquals(3,$models[0]->id);
  746. }
  747. $models1=Post::model()->post23()->findAll('id=3');
  748. $models2=Post::model()->post23()->findAll(array('condition'=>'id=3','scopes'=>'post23'));
  749. foreach(array($models1,$models2) as $models)
  750. {
  751. $this->assertEquals(1,count($models));
  752. $this->assertEquals(3,$models[0]->id);
  753. }
  754. $models1=Post::model()->recent()->with('author')->findAll();
  755. $models2=Post::model()->with('author')->findAll(array('scopes'=>'recent'));
  756. $models3=Post::model()->with('author')->findAll(array('scopes'=>array('recent')));
  757. $models4=Post::model()->with('author')->findAll(array('scopes'=>array(array('recent'=>array()))));
  758. foreach(array($models1,$models2,$models3,$models4) as $models)
  759. {
  760. $this->assertEquals(5,count($models));
  761. $this->assertEquals(5,$models[0]->id);
  762. $this->assertEquals(4,$models[1]->id);
  763. }
  764. $models1=Post::model()->recent(3)->findAll();
  765. $models2=Post::model()->findAll(array('scopes'=>array('recent'=>3)));
  766. $models3=Post::model()->findAll(array('scopes'=>array(array('recent'=>3))));
  767. foreach(array($models1,$models2,$models3) as $models)
  768. {
  769. $this->assertEquals(3,count($models));
  770. $this->assertEquals(5,$models[0]->id);
  771. $this->assertEquals(4,$models[1]->id);
  772. }
  773. //default scope
  774. $models=PostSpecial::model()->findAll();
  775. $this->assertEquals(2,count($models));
  776. $this->assertEquals(2,$models[0]->id);
  777. $this->assertEquals(3,$models[1]->id);
  778. //default scope + scope
  779. $models1=PostSpecial::model()->desc()->findAll();
  780. $models2=PostSpecial::model()->findAll(array('scopes'=>'desc'));
  781. foreach(array($models1,$models2) as $models)
  782. {
  783. $this->assertEquals(2,count($models));
  784. $this->assertEquals(3,$models[0]->id);
  785. $this->assertEquals(2,$models[1]->id);
  786. }
  787. //behavior scope
  788. $models=Post::model()->findAll(array('scopes'=>'behaviorPost23'));
  789. $this->assertEquals(2,count($models));
  790. $this->assertEquals(2,$models[0]->id);
  791. $this->assertEquals(3,$models[1]->id);
  792. //behavior parametrized scope
  793. $models=Post::model()->findAll(array('scopes'=>array('behaviorRecent'=>3)));
  794. $this->assertEquals(3,count($models));
  795. $this->assertEquals(5,$models[0]->id);
  796. $this->assertEquals(4,$models[1]->id);
  797. }
  798. public function testScopeWithRelations()
  799. {
  800. $user1=User::model()->with('posts:post23')->findByPk(2);
  801. $user2=User::model()->with(array('posts'=>array('scopes'=>'post23')))->findByPk(2);
  802. $user3=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>'post23'))));
  803. //ensure alais overloading work correctly
  804. $user4=User::model()->with(array('posts:post23A'=>array('alias'=>'alias')))->findByPk(2);
  805. $user5=User::model()->with(array('posts'=>array('scopes'=>'post23A','alias'=>'alias')))->findByPk(2);
  806. $user6=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>'post23A','alias'=>'alias'))));
  807. foreach(array($user1,$user2,$user3,$user4,$user5,$user6) as $user)
  808. {
  809. $this->assertEquals(2,count($user->posts));
  810. $this->assertEquals(2,$user->posts[0]->id);
  811. $this->assertEquals(3,$user->posts[1]->id);
  812. }
  813. $user1=User::model()->with(array('posts'=>array('scopes'=>array('p'=>4))))->findByPk(2);
  814. $user2=User::model()->with(array('posts'=>array('scopes'=>array('p'=>array(4)))))->findByPk(2);
  815. $user3=User::model()->with(array('posts'=>array('scopes'=>array(array('p'=>4)))))->findByPk(2);
  816. $user4=User::model()->with(array('posts'=>array('scopes'=>array(array('p'=>array(4))))))->findByPk(2);
  817. $user5=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('p'=>4)))));
  818. $user6=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('p'=>array(4))))));
  819. $user7=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('p'=>4))))));
  820. $user8=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('p'=>array(4)))))));
  821. foreach(array($user1,$user2,$user3,$user4,$user5,$user6,$user7,$user8) as $user)
  822. {
  823. $this->assertEquals(1,count($user->posts));
  824. $this->assertEquals(4,$user->posts[0]->id);
  825. }
  826. $user=UserSpecial::model()->findByPk(2);
  827. $posts=$user->posts;
  828. $this->assertEquals(2,count($posts));
  829. $this->assertEquals(2,$posts[0]->id);
  830. $this->assertEquals(3,$posts[1]->id);
  831. $user=UserSpecial::model()->findByPk(2);
  832. $posts=$user->posts(array('params'=>array(':id1'=>4),'order'=>'posts.id DESC'));
  833. $this->assertEquals(2,count($posts));
  834. $this->assertEquals(4,$posts[0]->id);
  835. $this->assertEquals(3,$posts[1]->id);
  836. $user=User::model()->with('posts:post23')->findByPk(2);
  837. $posts=$user->posts(array('scopes'=>'post23'));
  838. $this->assertEquals(2,count($posts));
  839. $this->assertEquals(2,$posts[0]->id);
  840. $this->assertEquals(3,$posts[1]->id);
  841. //related model behavior scope
  842. $user1=User::model()->with('posts:behaviorPost23')->findByPk(2);
  843. $user2=User::model()->with(array('posts'=>array('scopes'=>'behaviorPost23')))->findByPk(2);
  844. $user3=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>'behaviorPost23'))));
  845. foreach(array($user1,$user2,$user3) as $user)
  846. {
  847. $this->assertEquals(2,count($user->posts));
  848. $this->assertEquals(2,$user->posts[0]->id);
  849. $this->assertEquals(3,$user->posts[1]->id);
  850. }
  851. //related model with behavior parametrized scope
  852. $user1=User::model()->with(array('posts'=>array('scopes'=>array('behaviorP'=>4))))->findByPk(2);
  853. $user2=User::model()->with(array('posts'=>array('scopes'=>array('behaviorP'=>array(4)))))->findByPk(2);
  854. $user3=User::model()->with(array('posts'=>array('scopes'=>array(array('behaviorP'=>4)))))->findByPk(2);
  855. $user4=User::model()->with(array('posts'=>array('scopes'=>array(array('behaviorP'=>array(4))))))->findByPk(2);
  856. $user5=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('behaviorP'=>4)))));
  857. $user6=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array('behaviorP'=>array(4))))));
  858. $user7=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('behaviorP'=>4))))));
  859. $user8=User::model()->findByPk(2,array('with'=>array('posts'=>array('scopes'=>array(array('behaviorP'=>array(4)))))));
  860. foreach(array($user1,$user2,$user3,$user4,$user5,$user6,$user7,$user8) as $user)
  861. {
  862. $this->assertEquals(1,count($user->posts));
  863. $this->assertEquals(4,$user->posts[0]->id);
  864. }
  865. //related model with 'scopes' as relation option
  866. $user=User::model()->with('postsOrderDescFormat1')->findByPk(2);
  867. $this->assertEquals(3,count($user->postsOrderDescFormat1));
  868. $this->assertEquals(array(4,3,2),array(
  869. $user->postsOrderDescFormat1[0]->id,
  870. $user->postsOrderDescFormat1[1]->id,
  871. $user->postsOrderDescFormat1[2]->id,
  872. ));
  873. $user=User::model()->with('postsOrderDescFormat2')->findByPk(2);
  874. $this->assertEquals(3,count($user->postsOrderDescFormat2));
  875. $this->assertEquals(array(4,3,2),array(
  876. $user->postsOrderDescFormat2[0]->id,
  877. $user->postsOrderDescFormat2[1]->id,
  878. $user->postsOrderDescFormat2[2]->id,
  879. ));
  880. }
  881. public function testResetScope()
  882. {
  883. // resetting named scope
  884. $posts=Post::model()->post23()->resetScope()->findAll();
  885. $this->assertEquals(5,count($posts));
  886. // resetting default scope
  887. $posts=PostSpecial::model()->resetScope()->findAll();
  888. $this->assertEquals(5,count($posts));
  889. }
  890. public function testJoinWithoutSelect()
  891. {
  892. // 1:1 test
  893. $groups=Group::model()->findAll(array(
  894. 'with'=>array(
  895. 'description'=>array(
  896. 'select'=>false,
  897. 'joinType'=>'INNER JOIN',
  898. ),
  899. ),
  900. ));
  901. $result=array();
  902. foreach($groups as $group)
  903. {
  904. // there should be nothing in relation
  905. $this->assertFalse($group->hasRelated('description'));
  906. $result[]=array($group->id,$group->name);
  907. }
  908. $this->assertEquals(array(
  909. array(1,'group1'),
  910. array(2,'group2'),
  911. array(3,'group3'),
  912. array(4,'group4'),
  913. ),$result);
  914. // 1:M test
  915. $users=User::model()->findAll(array(
  916. 'with'=>array(
  917. 'roles'=>array(
  918. 'select'=>false,
  919. 'joinType'=>'INNER JOIN',
  920. ),
  921. ),
  922. ));
  923. $result=array();
  924. foreach($users as $user)
  925. {
  926. // there should be nothing in relation
  927. $this->assertFalse($user->hasRelated('roles'));
  928. $result[]=array($user->id,$user->username,$user->email);
  929. }
  930. $this->assertEquals(array(
  931. array(1,'user1','email1'),
  932. array(2,'user2','email2'),
  933. ),$result);
  934. }
  935. public function testHasManyThroughEager()
  936. {
  937. // just bridge
  938. $user=User::model()->with('groups')->findByPk(1);
  939. $result=array();
  940. foreach($user->groups as $group)
  941. $result[]=array($user->username,$group->name);
  942. $this->assertEquals(array(
  943. array('user1','group1'),
  944. array('user1','group2'),
  945. ),$result);
  946. // just bridge, base limited
  947. $users=User::model()->with('groups')->findAll(array('limit'=>1));
  948. $result=array();
  949. foreach($users as $user)
  950. {
  951. foreach($user->groups as $group)
  952. $result[]=array($user->username,$group->name);
  953. }
  954. $this->assertEquals(array(
  955. array('user1','group1'),
  956. array('user1','group2'),
  957. ),$result);
  958. // 'through' should not clear existing relations defined via short syntax
  959. $user=User::model()->with('groups.description')->findByPk(1);
  960. $result=array();
  961. foreach($user->groups as $group)
  962. $result[]=array($user->username,$group->name,$group->description->name);
  963. $this->assertEquals(array(
  964. array('user1','group1','room1'),
  965. array('user1','group2','room2'),
  966. ),$result);
  967. // 'through' should not clear existing with
  968. $user=User::model()->with(array('groups'=>array('with'=>'description')))->findByPk(1);
  969. $result=array();
  970. foreach($user->groups as $group)
  971. $result[]=array($user->username,$group->name,$group->description->name);
  972. $this->assertEquals(array(
  973. array('user1','group1','room1'),
  974. array('user1','group2','room2'),
  975. ),$result);
  976. // bridge fields handling
  977. $user=User::model()->with('roles','groups')->findByPk(1);
  978. $result=array();
  979. foreach($user->groups as $group)
  980. $result[]=array($user->username,$group->name);
  981. $this->assertEquals(array(
  982. array('user1','group1'),
  983. array('user1','group2'),
  984. ),$result);
  985. $result=array();
  986. foreach($user->roles as $role)
  987. $result[]=array($user->username,$role->name);
  988. $this->assertEquals(array(
  989. array('user1','dev'),
  990. array('user1','user'),
  991. ),$result);
  992. // bridge fields handling, another relations order
  993. $user=User::model()->with('groups','roles')->findByPk(1);
  994. $result=array();
  995. foreach($user->groups as $group)
  996. $result[]=array($user->username,$group->name);
  997. $this->assertEquals(array(
  998. array('user1','group1'),
  999. array('user1','group2'),
  1000. ),$result);
  1001. $result=array();
  1002. foreach($user->roles as $role)
  1003. $result[]=array($user->username,$role->name);
  1004. $this->assertEquals(array(
  1005. array('user1','dev'),
  1006. array('user1','user'),
  1007. ),$result);
  1008. // bridge fields handling, base limited
  1009. $users=User::model()->with('roles','groups')->findAll(array('limit'=>1));
  1010. $result=array();
  1011. foreach($users as $user)
  1012. {
  1013. foreach($user->groups as $group)
  1014. $result[]=array($user->username,$group->name);
  1015. }
  1016. $this->assertEquals(array(
  1017. array('user1','group1'),
  1018. array('user1','group2'),
  1019. ),$result);
  1020. $result=array();
  1021. foreach($users as $user)
  1022. {
  1023. foreach($user->roles as $role)
  1024. $result[]=array($user->username,$role->name);
  1025. }
  1026. $this->assertEquals(array(
  1027. array('user1','dev'),
  1028. array('user1','user'),
  1029. ),$result);
  1030. // nested through
  1031. $group=Group::model()->with('comments')->findByPk(1);
  1032. $result=array();
  1033. foreach($group->comments as $comment)
  1034. $result[]=array($group->name,$comment->content);
  1035. $this->assertEquals(array(
  1036. array('group1','comment 1'),
  1037. array('group1','comment 2'),
  1038. array('group1','comment 3'),
  1039. array('group1','comment 4'),
  1040. array('group1','comment 5'),
  1041. array('group1','comment 6'),
  1042. array('group1','comment 7'),
  1043. array('group1','comment 8'),
  1044. array('group1','comment 9'),
  1045. ),$result);
  1046. // nested through, base limited
  1047. $groups=Group::model()->with('comments')->findAll(array('limit'=>1));
  1048. $result=array();
  1049. foreach($groups as $group)
  1050. {
  1051. foreach($group->comments as $comment)
  1052. $result[]=array($group->name,$comment->content);
  1053. }
  1054. $this->assertEquals(array(
  1055. array('group1','comment 1'),
  1056. array('group1','comment 2'),
  1057. array('group1','comment 3'),
  1058. array('group1','comment 4'),
  1059. array('group1','comment 5'),
  1060. array('group1','comment 6'),
  1061. array('group1','comment 7'),
  1062. array('group1','comment 8'),
  1063. array('group1','comment 9'),
  1064. ),$result);
  1065. // self through
  1066. $teachers=User::model()->with('students')->findAll();
  1067. $result=array();
  1068. foreach($teachers as $teacher)
  1069. {
  1070. foreach($teacher->students as $student)
  1071. $result[]=array($teacher->username,$student->username);
  1072. }
  1073. $this->assertEquals(array(
  1074. array('user1','user3'),
  1075. array('user2','user4'),
  1076. ),$result);
  1077. // self through, bridge fields handling for right part
  1078. $teachers=User::model()->with('mentorships','students')->findAll();
  1079. $result=array();
  1080. foreach($teachers as $teacher)
  1081. {
  1082. foreach($teacher->students as $student)
  1083. $result[$student->primaryKey]=array('teacher'=>$teacher->username,'student'=>$student->username);
  1084. foreach($teacher->mentorships as $mentorship)
  1085. $result[$mentorship->student_id]['progress']=$mentorship->progress;
  1086. }
  1087. $this->assertEquals(array(
  1088. 3=>array('teacher'=>'user1','student'=>'user3','progress'=>'good'),
  1089. 4=>array('teacher'=>'user2','student'=>'user4','progress'=>'average'),
  1090. ),$result);
  1091. // self through, base limited
  1092. $teachers=User::model()->with('students')->findAll(array('limit'=>1));
  1093. $result=array();
  1094. foreach($teachers as $teacher)
  1095. {
  1096. foreach($teacher->students as $student)
  1097. $result[]=array($teacher->username,$student->username);
  1098. }
  1099. $this->assertEquals(array(
  1100. array('user1','user3'),
  1101. ),$result);
  1102. }
  1103. public function testHasManyThroughLazy()
  1104. {
  1105. $user=User::model()->findByPk(1);
  1106. $result=array();
  1107. foreach($user->groups as $group)
  1108. $result[]=array($user->username,$group->name);
  1109. $this->assertEquals(array(
  1110. array('user1','group1'),
  1111. array('user1','group2'),
  1112. ),$result);
  1113. $user=User::model()->findByPk(1);
  1114. $result=array();
  1115. foreach($user->groups(array('with'=>'description')) as $group)
  1116. $result[]=array($user->username,$group->name,$group->description->name);
  1117. $this->assertEquals(array(
  1118. array('user1','group1','room1'),
  1119. array('user1','group2','room2'),
  1120. ),$result);
  1121. // nested through
  1122. $group=Group::model()->findByPk(1);
  1123. $result=array();
  1124. foreach($group->comments as $comment)
  1125. $result[]=array($group->name,$comment->content);
  1126. $this->assertEquals(array(
  1127. array('group1','comment 1'),
  1128. array('group1','comment 2'),
  1129. array('group1','comment 3'),
  1130. array('group1','comment 4'),
  1131. array('group1','comment 5'),
  1132. array('group1','comment 6'),
  1133. array('group1','comment 7'),
  1134. array('group1','comment 8'),
  1135. array('group1','comment 9'),
  1136. ),$result);
  1137. // self through
  1138. $teacher=User::model()->findByPk(1);
  1139. $result=array();
  1140. foreach($teacher->students as $student)
  1141. $result[]=array($teacher->username,$student->username);
  1142. $this->assertEquals(array(
  1143. array('user1','user3'),
  1144. ),$result);
  1145. }
  1146. /**
  1147. * @see issue2274
  1148. */
  1149. function testMergingWith()
  1150. {
  1151. User::model()->nonEmptyPosts()->findAll(array(
  1152. 'with'=>array(
  1153. 'posts'=>array(
  1154. 'joinType'=>'INNER JOIN',
  1155. ),
  1156. )
  1157. ));
  1158. }
  1159. /**
  1160. * @see github issue 206
  1161. * Unable to pass CDbCriteria to relation while array works fine.
  1162. */
  1163. public function testIssue206()
  1164. {
  1165. $user = User::model()->findByPk(2);
  1166. $result1 = $user->posts(array('condition' => 'id IN (2,3)'));
  1167. $criteria = new CDbCriteria();
  1168. $criteria->addInCondition('id', array(2,3));
  1169. $user = User::model()->findByPk(2);
  1170. $result2 = $user->posts($criteria);
  1171. $this->assertEquals($result1, $result2);
  1172. }
  1173. /**
  1174. * https://github.com/yiisoft/yii/issues/1070
  1175. */
  1176. public function testIssue1070()
  1177. {
  1178. $dataProvider=new CActiveDataProvider('UserWithDefaultScope');
  1179. foreach($dataProvider->getData() as $item)
  1180. {
  1181. try
  1182. {
  1183. $item->links[0]->from_user;
  1184. $result=true;
  1185. }
  1186. catch ( CDbException $e )
  1187. {
  1188. $result=false;
  1189. }
  1190. $this->assertTrue($result);
  1191. }
  1192. }
  1193. /**
  1194. * https://github.com/yiisoft/yii/issues/507
  1195. */
  1196. public function testIssue507()
  1197. {
  1198. $this->assertEquals(2, count(UserWithDefaultScope::model()->findAll()));
  1199. }
  1200. }