Changeset 315

Show
Ignore:
Timestamp:
08/20/07 19:39:43 (1 year ago)
Author:
bermiferrer
Message:

Including associated models on hasMany and hasAndBelongsToMany associations, fixes #39.

Fixing model finding including an association without members, fixes #40.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/AkActiveRecord.php

    r312 r315  
    26742674        } 
    26752675 
    2676         return $this->_columns; 
     2676        return (array)$this->_columns; 
    26772677    } 
    26782678 
     
    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]); 
  • trunk/lib/AkActiveRecord/AkAssociatedActiveRecord.php

    r285 r315  
    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                } 
  • trunk/lib/AkActiveRecord/AkHasAndBelongsToMany.php

    r285 r315  
    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        } 
  • trunk/lib/AkActiveRecord/AkHasMany.php

    r285 r315  
    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) ? '' : 
    574  
    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  
     568        $finder_options['conditions'] = empty($options['conditions']) ? '' :  
     569 
     570        $Associated->_addTableAliasesToAssociatedSql('_'.$this->association_id, $options['conditions']).' '; 
     571         
    584572        return $finder_options; 
    585573    } 
     
    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        } 
  • trunk/test/fixtures/app/models/post.php

    r313 r315  
    33class Post extends ActiveRecord 
    44{ 
     5    var $has_many = 'comments'; 
    56} 
    67 
  • trunk/test/unit/lib/AkActiveRecord/AkHasMany.php

    r217 r315  
    99 
    1010 
    11 class test_AkActiveRecord_hasMany_Associations extends  UnitTestCase 
     11class test_AkActiveRecord_hasMany_Associations extends  AkUnitTest  
    1212{ 
    1313 
     
    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()); 
     
    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')); 
     
    213213        $this->assertEqual($VillaAltea->pictures[0]->get('title'), 'Garden'); 
    214214    } 
    215      
     215 
    216216    /**/ 
    217217 
     
    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  
     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')); 
     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); 
     259    } 
     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    } 
    238276} 
    239277