Changeset 194

Show
Ignore:
Timestamp:
04/11/07 20:29:59 (2 years ago)
Author:
bermiferrer
Message:

Adding 'unique' support for habtm associations

Files:

Legend:

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

    r139 r194  
    483483                    $record_id = $records[$k]; 
    484484                } 
    485                  
     485 
    486486                foreach (array_keys($this->Owner->{$this->association_id}) as $kk){ 
    487487                    if( 
     
    864864            $class_name = strtolower($CollectionHandler->getOption($association_id, 'class_name')); 
    865865            if(!empty($object->$association_id) && is_array($object->$association_id)){ 
     866                $this->_removeDuplicates($object, $association_id); 
    866867                foreach (array_keys($object->$association_id) as $k){ 
    867868                    if(!empty($object->{$association_id}[$k]) && strtolower(get_class($object->{$association_id}[$k])) == $class_name){ 
     
    888889 
    889890    } 
     891 
     892    function _removeDuplicates(&$object, $association_id) 
     893    { 
     894        if(!empty($object->{$association_id})){ 
     895            $CollectionHandler =& $object->hasAndBelongsToMany->models[$association_id]; 
     896            $options = $CollectionHandler->getOptions($association_id); 
     897            if(empty($options['unique'])){ 
     898                return ; 
     899            } 
     900            if($object->isNewRecord()){ 
     901                $ids = array(); 
     902            }else{ 
     903                if($existing = $CollectionHandler->find()){ 
     904                    $ids = $existing[0]->collect($existing,'id','id'); 
     905                }else{ 
     906                    $ids = array(); 
     907                } 
     908            } 
     909            $class_name = strtolower($CollectionHandler->getOption($association_id, 'class_name')); 
     910            foreach (array_keys($object->$association_id) as $k){ 
     911                if(!empty($object->{$association_id}[$k]) && strtolower(get_class($object->{$association_id}[$k])) == $class_name && !$object->{$association_id}[$k]->isNewRecord()){ 
     912                    $AssociatedItem =& $object->{$association_id}[$k]; 
     913                    if(isset($ids[$AssociatedItem->getId()])){ 
     914                        unset($object->{$association_id}[$k]); 
     915                        continue; 
     916                    } 
     917                    $ids[$AssociatedItem->getId()] = true; 
     918                } 
     919            } 
     920        } 
     921    } 
    890922} 
    891923 
  • trunk/test/unit/lib/AkActiveRecord/AkHasAndBelongsToMany.php

    r139 r194  
    228228    } 
    229229 
    230      
     230 
    231231    function test_find_on_unsaved_models_including_associations() 
    232232    { 
     
    252252    } 
    253253 
    254      
     254 
    255255    function test_clean_up_dependencies() 
    256256    { 
     
    276276    } 
    277277 
    278      
     278 
    279279    function test_double_assignation() 
    280280    { 
     
    311311        $PisoBermi->property_type->add($Atico); 
    312312        $PisoBermi->property_type->add($Apartamento); 
    313          
     313 
    314314 
    315315        $this->assertTrue($PisoJose =& $PisoJose->findFirstBy('description','Piso Jose')); 
     
    321321 
    322322        $this->assertTrue($PisoBermi =& $PisoBermi->findFirstBy('description','Piso Bermi')); 
    323          
     323 
    324324        $this->assertTrue($PisoJose =& $PisoJose->findFirstBy('description','Piso Jose')); 
    325325        $PisoJose->property_type->load(); 
    326          
     326 
    327327        $this->assertTrue($Atico =& $Atico->findFirstBy('description','Ático')); 
    328328        $this->assertTrue($Apartamento =& $Apartamento->findFirstBy('description','Apartamento')); 
    329          
     329 
    330330        $this->assertEqual($PisoJose->property_types[0]->getId(), $Apartamento->getId()); 
    331331        $this->assertEqual($PisoBermi->property_type->count(), 2); 
    332          
    333          
    334     } 
    335  
    336     /** 
    337      * @todo Implement support for unique elements 
    338      */ 
     332 
     333 
     334    } 
     335 
     336 
    339337    function test_associated_uniqueness() 
    340338    { 
     
    353351        $this->assertTrue($RanchoMaria =& $Property->findFirstBy('description','Rancho Maria')); 
    354352        $this->assertTrue($Rancho =&  $PropertyType->findFirstBy('description','Rancho', array('include'=>'properties'))); 
    355          
     353 
    356354        $Rancho->property->add($RanchoMaria); 
    357355        $this->assertEqual($Rancho->property->count(), 1); 
    358356 
     357        $Rancho->set('description', 'Rancho Type'); 
     358        $this->assertTrue($Rancho->save()); 
     359        $this->assertTrue($Rancho =&  $PropertyType->findFirstBy('description','Rancho Type', array('include'=>'properties'))); 
     360        $this->assertEqual($Rancho->property->count(), 1); 
    359361    } 
    360362