Changeset 2180

Show
Ignore:
Timestamp:
02/27/2008 03:04:49 PM (11 months ago)
Author:
Shadowhand
Message:

Better handling of transparent one<>one relationships.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/libraries/ORM.php

    r2179 r2180  
    130130                        return $this->object->$key; 
    131131                } 
    132                 elseif ( ! empty($this->object->id) AND (in_array($key, $this->has_one) OR in_array($key, $this->belongs_to))) 
     132                elseif (in_array($key, $this->has_one) OR in_array($key, $this->belongs_to)) 
    133133                { 
    134134                        // Set the model name 
    135135                        $model = ucfirst($key).'_Model'; 
    136136 
    137                         // Set the child id name 
    138                         $child_id = $key.'_id'; 
    139  
    140                         $this->object->$key = new $model(isset($this->object->$child_id) 
    141                                 // Get the foreign object using the key defined in this object 
    142                                 ? $this->object->$child_id 
    143                                 // Get the foreign object using the primary key of this object 
    144                                 : array($this->class.'_id', $this->object->id)); 
     137                        if (empty($this->object->id)) 
     138                        { 
     139                                // Load an empty model 
     140                                $this->object->$key = new $model; 
     141                        } 
     142                        else 
     143                        { 
     144                                // Set the child id name 
     145                                $child_id = $key.'_id'; 
     146 
     147                                $this->object->$key = new $model(isset($this->object->$child_id) 
     148                                        // Get the foreign object using the key defined in this object 
     149                                        ? $this->object->$child_id 
     150                                        // Get the foreign object using the primary key of this object 
     151                                        : array($this->class.'_id', $this->object->id)); 
     152                        } 
    145153 
    146154                        // Return the model