Ticket #40: find_and_include_should_return_unrelated_items.patch
| File find_and_include_should_return_unrelated_items.patch, 9.2 kB (added by bermi, 1 year ago) |
|---|
-
test/unit/lib/AkActiveRecord/AkHasMany.php
old new 8 8 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php'); 9 9 10 10 11 class test_AkActiveRecord_hasMany_Associations extends UnitTestCase11 class test_AkActiveRecord_hasMany_Associations extends AkUnitTest 12 12 { 13 13 14 14 function test_start() … … 56 56 57 57 $this->assertNull($Property->pictures[0]->get('property_id')); 58 58 59 //$Property->dbug();60 59 $MountainViews =& new Picture(array('title'=>'Mountain views')); 61 60 $this->assertTrue($MountainViews->isNewRecord()); 62 61 $Property->picture->add($MountainViews); … … 132 131 $Property =& $Property->find('first'); 133 132 $this->assertIdentical($Property->picture->count(), 0); 134 133 135 $this->assertFalse($Property->find('first', array('include'=>'pictures'))); 136 134 //$this->assertTrue($Property =& $Property->find('first', array('include'=>'pictures'))); 135 //$this->assertIdentical($Property->picture->count(), 0); 136 137 137 $Picture =& new Picture(); 138 138 $Alicia =& $Picture->create(array('title'=>'Alicia')); 139 139 $Bermi =& $Picture->create(array('title'=>'Bermi')); … … 212 212 213 213 $this->assertEqual($VillaAltea->pictures[0]->get('title'), 'Garden'); 214 214 } 215 215 216 216 /**/ 217 217 218 218 function test_clean_up_dependencies() 219 219 { 220 220 $Property =& new Property(array('description'=>'Ruins in Matamon')); 221 221 $this->assertTrue($Property->save()); 222 222 223 223 $South =& $Property->picture->create(array('title'=>'South views')); 224 224 $this->assertReference($South, $Property->pictures[0]); 225 225 $this->assertFalse($South->isNewRecord()); 226 226 227 227 $pic_id = $South->getId(); 228 228 229 229 $Property =& new Property($Property->getId()); 230 230 $this->assertTrue($Property->destroy()); 231 231 232 232 $Picture =& new Picture(); 233 233 234 234 $this->assertFalse($Picture->find($pic_id)); 235 236 } 237 238 function _test_should_not_die_on_unincluded_model() 239 { 240 $this->installAndIncludeModels(array('Post')); 241 $Post =& new Post(); 242 $Post->dbug(); 243 $Post->find('all', array('include' => array('comments'))); 244 } 245 246 function test_should_find_owner_even_if_it_has_no_relations() 247 { 248 $this->installAndIncludeModels(array('Post', 'Comment')); 235 249 250 $Post =& new Post(array('title' => 'Post for unit testing', 'body' => 'This is a post for testing the model')); 251 252 $Post->save(); 253 $Post->reload(); 254 255 $expected_id = $Post->getId(); 256 257 $this->assertTrue($Result =& $Post->find($expected_id, array('include' => array('comments')))); 258 $this->assertEqual($Result->getId(), $expected_id); 236 259 } 237 260 261 function test_should_find_owner_using_related_conditions() 262 { 263 $this->installAndIncludeModels(array('Post', 'Comment')); 264 265 $Post =& new Post(array('title' => 'Post for unit testing', 'body' => 'This is a post for testing the model')); 266 $Post->comment->create(array('body' => 'hello', 'name' => 'Aditya')); 267 $Post->save(); 268 $Post->reload(); 269 270 $expected_id = $Post->getId(); 271 272 $this->assertTrue($Result =& $Post->find($expected_id, array('include' => array('comments'), 'conditions' => "name = 'Aditya'"))); 273 274 $this->assertEqual($Result->comments[0]->get('name'), 'Aditya'); 275 } 238 276 } 239 277 240 278 ak_test('test_AkActiveRecord_hasMany_Associations', true); -
test/fixtures/app/installers/comment_installer.php
old new 1 <?php 2 class CommentInstaller extends AkInstaller 3 { 4 function up_1() 5 { 6 $this->createTable('comments', "id,name,body,post_id,created_at"); 7 } 8 9 function up_2() 10 { 11 $this->addColumn('comments', 'name'); 12 } 13 14 function down_1() 15 { 16 $this->dropTable('comments'); 17 } 18 19 function down_2() 20 { 21 $this->removeColumn('comments', 'name'); 22 } 23 } 24 25 ?> -
test/fixtures/app/models/comment.php
old new 1 <?php 2 3 class Comment extends ActiveRecord 4 { 5 var $belongs_to = 'post'; 6 } 7 8 ?> -
test/fixtures/app/models/post.php
old new 2 2 3 3 class Post extends ActiveRecord 4 4 { 5 var $has_many = 'comments'; 5 6 } 6 7 7 8 ?> -
lib/AkActiveRecord/AkHasAndBelongsToMany.php
old new 761 761 static $ModelInstance; 762 762 if(empty($ModelInstance)){ 763 763 $class_name = $this->getOption($this->association_id, 'class_name'); 764 Ak::import($class_name); 764 765 $ModelInstance =& new $class_name(); 765 766 } 766 767 return $ModelInstance; -
lib/AkActiveRecord/AkHasMany.php
old new 565 565 566 566 $finder_options['selection'] = trim($finder_options['selection'], ', '); 567 567 568 /** 569 * @todo Refactorize me. This is too confusing 570 */ 571 $finder_options['conditions'] = 572 // If owner is not available we build the searcher without an specific id 573 (empty($owner_id) ? '' : 568 $finder_options['conditions'] = empty($options['conditions']) ? '' : 574 569 575 // We have an Id so we add it to the conditions 576 ' '.$Associated->_addTableAliasesToAssociatedSql('_'.$this->association_id, $options['foreign_key']).' = '.$owner_id.' '. 577 // After adding the Id we need to add AND in case we have a previous contidion available 578 (!empty($options['conditions']) ? ' AND ' : ' ')). 579 580 // We add previous conditions 581 (!empty($options['conditions']) ? 582 $Associated->_addTableAliasesToAssociatedSql('_'.$this->association_id, $options['conditions']).' ' : ''); 583 570 $Associated->_addTableAliasesToAssociatedSql('_'.$this->association_id, $options['conditions']).' '; 571 584 572 return $finder_options; 585 573 } 586 574 … … 614 602 static $ModelInstance; 615 603 if(empty($ModelInstance)){ 616 604 $class_name = $this->getOption($this->association_id, 'class_name'); 605 Ak::import($class_name); 617 606 $ModelInstance =& new $class_name(); 618 607 } 619 608 return $ModelInstance; -
lib/AkActiveRecord/AkAssociatedActiveRecord.php
old new 418 418 $objects[$i]->$association_id->build($attributes, false); 419 419 $objects[$i]->$association_id->_newRecord = false; 420 420 } 421 422 /**423 * @todo FIXME This is a dirty hack for sqlite table joins which are not exclusive as they are on MySql424 * this makes table joins behave the same way as they do on MySql425 */426 }elseif (in_array($association_id, $included_associations) && $this->_getDatabaseType() == 'sqlite'){427 $false = false;428 return $false;429 421 } 430 422 } 431 423 -
lib/AkActiveRecord.php
old new 2673 2673 $this->_columns = $this->getColumnSettings(); 2674 2674 } 2675 2675 2676 return $this->_columns;2676 return (array)$this->_columns; 2677 2677 } 2678 2678 2679 2679 function getColumnSettings() … … 2704 2704 !$this->_runCurrentModelInstallerIfExists($column_objects)){ 2705 2705 trigger_error(Ak::t('Ooops! Could not fetch details for the table %table_name.', array('%table_name'=>$this->getTableName())), E_USER_ERROR); 2706 2706 return false; 2707 }elseif(is_array($column_objects)){ 2707 }elseif (empty($column_objects)){ 2708 $this->_runCurrentModelInstallerIfExists($column_objects); 2709 } 2710 if(is_array($column_objects)){ 2708 2711 foreach (array_keys($column_objects) as $k){ 2709 2712 $this->setColumnSettings($column_objects[$k]->name, $column_objects[$k]); 2710 2713 }
