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  
    88require_once(dirname(__FILE__).'/../../../fixtures/config/config.php'); 
    99 
    1010 
    11 class test_AkActiveRecord_hasMany_Associations extends  UnitTestCase 
     11class test_AkActiveRecord_hasMany_Associations extends  AkUnitTest  
    1212{ 
    1313 
    1414    function test_start() 
     
    5656 
    5757        $this->assertNull($Property->pictures[0]->get('property_id')); 
    5858 
    59         //$Property->dbug(); 
    6059        $MountainViews =& new Picture(array('title'=>'Mountain views')); 
    6160        $this->assertTrue($MountainViews->isNewRecord()); 
    6261        $Property->picture->add($MountainViews); 
     
    132131        $Property =& $Property->find('first'); 
    133132        $this->assertIdentical($Property->picture->count(), 0); 
    134133 
    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         
    137137        $Picture =& new Picture(); 
    138138        $Alicia =& $Picture->create(array('title'=>'Alicia')); 
    139139        $Bermi =& $Picture->create(array('title'=>'Bermi')); 
     
    212212 
    213213        $this->assertEqual($VillaAltea->pictures[0]->get('title'), 'Garden'); 
    214214    } 
    215      
     215 
    216216    /**/ 
    217217 
    218218    function test_clean_up_dependencies() 
    219219    { 
    220220        $Property =& new Property(array('description'=>'Ruins in Matamon')); 
    221221        $this->assertTrue($Property->save()); 
    222          
     222 
    223223        $South =& $Property->picture->create(array('title'=>'South views')); 
    224224        $this->assertReference($South, $Property->pictures[0]); 
    225225        $this->assertFalse($South->isNewRecord()); 
    226          
     226 
    227227        $pic_id = $South->getId(); 
    228228 
    229229        $Property =& new Property($Property->getId()); 
    230230        $this->assertTrue($Property->destroy()); 
    231          
     231 
    232232        $Picture =& new Picture(); 
    233233 
    234234        $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')); 
    235249         
     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); 
    236259    } 
    237260 
     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    } 
    238276} 
    239277 
    240278ak_test('test_AkActiveRecord_hasMany_Associations', true); 
  • test/fixtures/app/installers/comment_installer.php

    old new  
     1<?php 
     2class 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 
     3class Comment extends ActiveRecord  
     4{ 
     5    var $belongs_to = 'post'; 
     6} 
     7 
     8?> 
  • test/fixtures/app/models/post.php

    old new  
    22 
    33class Post extends ActiveRecord 
    44{ 
     5    var $has_many = 'comments'; 
    56} 
    67 
    78?> 
  • lib/AkActiveRecord/AkHasAndBelongsToMany.php

    old new  
    761761        static $ModelInstance; 
    762762        if(empty($ModelInstance)){ 
    763763            $class_name = $this->getOption($this->association_id, 'class_name'); 
     764            Ak::import($class_name); 
    764765            $ModelInstance =& new $class_name(); 
    765766        } 
    766767        return $ModelInstance; 
  • lib/AkActiveRecord/AkHasMany.php

    old new  
    565565 
    566566        $finder_options['selection'] = trim($finder_options['selection'], ', '); 
    567567 
    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']) ? '' :  
    574569 
    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         
    584572        return $finder_options; 
    585573    } 
    586574 
     
    614602        static $ModelInstance; 
    615603        if(empty($ModelInstance)){ 
    616604            $class_name = $this->getOption($this->association_id, 'class_name'); 
     605            Ak::import($class_name); 
    617606            $ModelInstance =& new $class_name(); 
    618607        } 
    619608        return $ModelInstance; 
  • lib/AkActiveRecord/AkAssociatedActiveRecord.php

    old new  
    418418                            $objects[$i]->$association_id->build($attributes, false); 
    419419                            $objects[$i]->$association_id->_newRecord = false; 
    420420                        } 
    421  
    422                         /** 
    423                      * @todo FIXME This is a dirty hack for sqlite table joins which are not exclusive as they are on MySql 
    424                      *       this makes table joins behave the same way as they do on MySql 
    425                      */ 
    426                     }elseif (in_array($association_id, $included_associations) && $this->_getDatabaseType() == 'sqlite'){ 
    427                         $false = false;  
    428                         return $false; 
    429421                    } 
    430422                } 
    431423 
  • lib/AkActiveRecord.php

    old new  
    26732673            $this->_columns = $this->getColumnSettings(); 
    26742674        } 
    26752675 
    2676         return $this->_columns; 
     2676        return (array)$this->_columns; 
    26772677    } 
    26782678 
    26792679    function getColumnSettings() 
     
    27042704            !$this->_runCurrentModelInstallerIfExists($column_objects)){ 
    27052705                trigger_error(Ak::t('Ooops! Could not fetch details for the table %table_name.', array('%table_name'=>$this->getTableName())), E_USER_ERROR); 
    27062706                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)){ 
    27082711                foreach (array_keys($column_objects) as $k){ 
    27092712                    $this->setColumnSettings($column_objects[$k]->name, $column_objects[$k]); 
    27102713                }