Changeset 4580

Show
Ignore:
Timestamp:
06/28/08 07:28:01 (6 months ago)
Author:
jwage
Message:

fixes #1078 fixes #1117

Location:
branches/0.11
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • branches/0.11/lib/Doctrine/Collection.php

    r4520 r4580  
    721721        // create new records for each new row in the array 
    722722        foreach ($array as $rowKey => $row) { 
    723             $this[$rowKey]->fromArray($row); 
     723            $this[$rowKey]->synchronizeWithArray($row); 
    724724        } 
    725725    } 
  • branches/0.11/lib/Doctrine/Record.php

    r4573 r4580  
    914914    { 
    915915        if (isset($this->_data[$fieldName])) { 
     916            $type = $this->_table->getTypeOf($fieldName); 
    916917            if ($value instanceof Doctrine_Record) { 
    917                 $type = $this->_table->getTypeOf($fieldName); 
    918  
    919918                $id = $value->getIncremented(); 
    920919 
     
    930929            } 
    931930 
    932             if ($old !== $value) { 
     931            if ($this->_isValueModified($type, $old, $value)) { 
    933932                if ($value === null) { 
    934933                    $value = self::$_null; 
     
    959958 
    960959        return $this; 
     960    } 
     961 
     962    /** 
     963     * Check if a value has changed according to Doctrine 
     964     * Doctrine is loose with type checking in the same ways PHP is for consistancy of behavior 
     965     * 
     966     * This function basically says if what is being set is of Doctrine type boolean and something 
     967     * like current_value == 1 && new_value = true would not be considered modified 
     968     * 
     969     * Simply doing $old !== $new will return false for boolean columns would mark the field as modified 
     970     * and change it in the database when it is not necessary 
     971     * 
     972     * @param string $type  Doctrine type of the column 
     973     * @param string $old   Old value 
     974     * @param string $new   New value 
     975     * @return boolean $modified  Whether or not Doctrine considers the value modified 
     976     */ 
     977    protected function _isValueModified($type, $old, $new) 
     978    { 
     979        if ($type == 'boolean' && (is_bool($old) || is_numeric($old)) && (is_bool($new) || is_numeric($new)) && $old == $new) { 
     980            return false; 
     981        } else { 
     982            return $old !== $new; 
     983        } 
    961984    } 
    962985 
     
    13391362    public function fromArray($array, $deep = true) 
    13401363    { 
    1341         if (is_array($array)) { 
    1342             foreach ($array as $key => $value) { 
    1343                 if ($deep && $this->getTable()->hasRelation($key)) { 
    1344                     $this->$key->fromArray($value, $deep); 
    1345                 } else if ($this->getTable()->hasField($key)) { 
    1346                     $this->set($key, $value); 
    1347                 } 
    1348             } 
    1349         } 
     1364        return $this->synchronizeWithArray($array, $deep); 
    13501365    } 
    13511366 
     
    13611376     * @param array $array representation of a Doctrine_Record 
    13621377     */ 
    1363     public function synchronizeWithArray(array $array) 
    1364     { 
     1378    public function synchronizeWithArray(array $array, $deep = true) 
     1379    { 
     1380        $refresh = false; 
    13651381        foreach ($array as $key => $value) { 
    1366             if ($this->getTable()->hasRelation($key)) { 
     1382            if ($key == 'identifier') { 
     1383                $refresh = true; 
     1384                $this->assignIdentifier((array) $value); 
     1385                continue; 
     1386            } 
     1387            if ($deep && $this->getTable()->hasRelation($key)) { 
    13671388                $this->get($key)->synchronizeWithArray($value); 
    13681389            } else if ($this->getTable()->hasField($key)) { 
     
    13761397                unset($this->$name); 
    13771398            } 
     1399        } 
     1400 
     1401        if ($refresh) { 
     1402            $this->refresh(); 
    13781403        } 
    13791404    } 
  • branches/0.11/tests/Record/FromArrayTestCase.php

    r4482 r4580  
    5959         
    6060        # add group 
    61         $userArray['Group'][]['name'] = 'New Group'; # This is a n-m relationship 
     61        $userArray['Group'][0]['name'] = 'New Group'; # This is a n-m relationship 
    6262        # add a group which exists 
    63         $userArray['Group'][1]['id'] = $this->previous_group; # This is a n-m relationship where the group was made in prepareData 
     63        $userArray['Group'][1]['identifier'] = $this->previous_group; # This is a n-m relationship where the group was made in prepareData 
    6464           
    6565        $user->fromArray($userArray); 
  • branches/0.11/tests/Record/SynchronizeTestCase.php

    r4482 r4580  
    7070        $userArray['Group'][]['name'] = 'New Group'; # This is a n-m relationship 
    7171        // add a group which exists 
    72         $userArray['Group'][1]['id'] = $this->previous_group; # This is a n-m relationship where the group was made in prepareData 
     72        $userArray['Group'][1]['identifier'] = $this->previous_group; # This is a n-m relationship where the group was made in prepareData 
    7373 
    7474        $user->synchronizeWithArray($userArray); 
  • branches/0.11/tests/run.php

    r4566 r4580  
    292292$record->addTestCase(new Doctrine_Record_Inheritance_TestCase()); 
    293293$record->addTestCase(new Doctrine_Record_Synchronize_TestCase()); 
    294 $record->addTestCase(new Doctrine_Record_Generator_TestCase()); 
    295294$record->addTestCase(new Doctrine_Record_FromArray_TestCase()); 
    296295$test->addTestCase($record);