Changeset 1351

Show
Ignore:
Timestamp:
07/31/09 02:05:18 (1 year ago)
Author:
arnoschn
Message:

Fixing data type casting when retrieving associated records via find(array('include'=>'model')).
values were not type casted before nor was the var $serialize taken into account.

Files:

Legend:

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

    r1345 r1351  
    870870            $available['load_associations'] = false; 
    871871            $available['load_acts'] = $load_acts; 
    872  
     872            /** 
     873             * unserializing data 
     874             */ 
     875            /**$serialized_attributes = !empty($instance->serialize)? Ak::toArray($instance->serialize):array(); 
     876            $serialized_attributes = array_intersect(array_keys($available),$serialized_attributes); 
     877            foreach($serialized_attributes as $serialized_attribute) { 
     878                if($instance->_shouldSerializeColumn($serialized_attribute)) { 
     879                    $available[$serialized_attribute] = @unserialize($available[$serialized_attribute]); 
     880                } 
     881            }*/ 
     882            $available = $this->_castAttributesFromDatabase($available,$instance); 
    873883            $obj=&$parent->$assoc_name->build($available,false); 
    874884 
     
    884894        } 
    885895    } 
    886  
     896    function _castAttributesFromDatabase($attributes = array(),$record = null) 
     897    { 
     898        foreach($attributes as $key => $value) { 
     899            $attributes[$key] = $this->_castAttributeFromDatabase($key,$value,$record); 
     900        } 
     901        return $attributes; 
     902    } 
     903    function _castAttributeFromDatabase($column_name,$value, $record) 
     904    { 
     905        $column_type = $record->getColumnType($column_name); 
     906 
     907        if($column_type){ 
     908            if('integer' == $column_type){ 
     909                return is_null($value) ? null : (integer)$value; 
     910                //return is_null($value) ? null : $value;    // maybe for bigint we can do this 
     911            }elseif('boolean' == $column_type){ 
     912                if (is_null($value)) { 
     913                    return null; 
     914                } 
     915                if ($this->_getDatabaseType()=='postgre'){ 
     916                    return $value=='t' ? true : false; 
     917                } 
     918                return (integer)$value === 1 ? true : false; 
     919            }elseif(!empty($value) && 'date' == $column_type && strstr(trim($value),' ')){ 
     920                return substr($value,0,10) == '0000-00-00' ? null : str_replace(substr($value,strpos($value,' ')), '', $value); 
     921            }elseif (!empty($value) && 'datetime' == $column_type && substr($value,0,10) == '0000-00-00'){ 
     922                return null; 
     923            }elseif ('binary' == $column_type && $this->_getDatabaseType() == 'postgre'){ 
     924                $value = $this->_db->unescape_blob($value); 
     925                $value = empty($value) || trim($value) == 'null' ? null : $value; 
     926            }elseif($record->_shouldSerializeColumn($column_name)){ 
     927                $this->_ensureClassExistsForSerializedColumnBeforeUnserializing($column_name); 
     928                $value = @unserialize($value); 
     929            } 
     930        } 
     931        return $value; 
     932    } 
    887933    function getCollectionHandlerName($association_id) 
    888934    { 
  • trunk/test/fixtures/app/installers/bb_installer.php

    r1286 r1351  
    33class BbInstaller extends AkInstaller 
    44{ 
     5    function up_2() 
     6    { 
     7        $this->addColumn('bbs','languages string(200)'); 
     8        $this->addColumn('bbs','other string(200)'); 
     9    } 
     10    function down_2() 
     11    { 
     12        $this->removeColumn('bbs','languages'); 
     13        $this->removeColumn('bbs','other'); 
     14    } 
    515    function up_1() 
    616    { 
  • trunk/test/fixtures/app/models/bb.php

    r1286 r1351  
    55    var $belongsTo = array('aa'); 
    66    var $habtm = array('ccs'); 
     7    var $serialize = array('languages','other'); 
    78} 
    89 
  • trunk/test/unit/lib/AkActiveRecord/_AkActiveRecord_nested_finders.php

    r1286 r1351  
    2929        $aa = &$this->Aa->create(array('name'=>'first aa')); 
    3030        $this->assertTrue($aa); 
    31         $bb1 = &$this->Bb->create(array('name'=>'first bb')); 
    32         $bb2 = &$this->Bb->create(array('name'=>'second bb')); 
     31        $bb1 = &$this->Bb->create(array('name'=>'first bb','languages'=>array('es','de'),'other'=>array(1,2,3))); 
     32        $bb2 = &$this->Bb->create(array('name'=>'second bb','languages'=>array('en','fr'),'other'=>array(4,5,6))); 
    3333        $babies = array($bb1,$bb2); 
    3434        //Ak::debug($aa->babies); 
     35        //die; 
    3536        //die; 
    3637        $aa->babies->set($babies); 
     
    6162        $this->assertEqual('second bb',$found_first_aa->bbs[0]->name); 
    6263         
     64        /** 
     65         * now find them back and test the serialized bb values 
     66         */ 
     67         
     68        $found_first_aa = $this->Aa->findFirstBy('name','first aa',array('include'=>array('bbs'=>array('order' => 'id ASC')))); 
     69        $this->assertTrue($found_first_aa); 
     70        $this->assertTrue($found_first_aa->bbs); 
     71        $this->assertEqual(2,$found_first_aa->babies->count()); 
     72        $this->assertEqual(array('en','fr'),$found_first_aa->bbs[1]->languages); 
     73        $this->assertEqual(array(4,5,6),$found_first_aa->bbs[1]->other); 
     74        $this->assertEqual(array('es','de'),$found_first_aa->bbs[0]->languages); 
     75        $this->assertEqual(array(1,2,3),$found_first_aa->bbs[0]->other); 
    6376    } 
    6477     
     
    422435         
    423436    } 
     437     
     438     
    424439} 
    425440