Changeset 2672

Show
Ignore:
Timestamp:
05/14/08 12:10:35 (3 months ago)
Author:
Shadowhand
Message:

Changes to ORM:

  • Added many<>many relationship handling to get, set
  • Implemented many<>many relationship saving in save()

This means you can do: $user->roles = array(1, 2), etc. (I kick ass!)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/system/libraries/ORM.php

    r2625 r2672  
    4040        // Currently loaded object 
    4141        protected $object; 
     42        protected $object_related; 
    4243        protected $loaded = FALSE; 
    4344        protected $saved = FALSE; 
     
    4546        // Changed object keys 
    4647        protected $changed = array(); 
     48        protected $changed_related = array(); 
    4749 
    4850        // Object Relationships 
     
    162164                        // Return the model 
    163165                        return $this->object->$key; 
     166                } 
     167                elseif (in_array($key, $this->has_and_belongs_to_many)) 
     168                { 
     169                        if (empty($this->object_related[$key]['cur'])) 
     170                        { 
     171                                // Foreign key 
     172                                $fk = inflector::singular($key).'_id'; 
     173 
     174                                // Query to find relationships 
     175                                $query = self::$db 
     176                                        ->select($fk) 
     177                                        ->from($this->related_table($key)) 
     178                                        ->where($this->class.'_id', $this->id) 
     179                                        ->get(); 
     180 
     181                                foreach ($query as $row) 
     182                                { 
     183                                        // Set relationships 
     184                                        $this->object_related[$key]['cur'][] = $row->$fk; 
     185                                } 
     186                        } 
     187 
     188                        // Return the current relationships 
     189                        return $this->object_related[$key]['cur']; 
    164190                } 
    165191                else 
     
    206232                        } 
    207233                } 
     234                elseif (in_array($key, $this->has_and_belongs_to_many) AND is_array($value)) 
     235                { 
     236                        if (empty($this->object_related[$key])) 
     237                        { 
     238                                // Force a load of the relationship 
     239                                $this->$key; 
     240                        } 
     241 
     242                        // Set the new relationships 
     243                        $this->object_related[$key]['new'] = $value; 
     244 
     245                        // Relationships changed 
     246                        $this->changed_related[$key] = $key; 
     247                } 
    208248                else 
    209249                { 
     
    751791        { 
    752792                // No data was changed 
    753                 if (empty($this->changed)
     793                if (empty($this->changed) AND empty($this->changed_related)
    754794                        return TRUE; 
    755795 
    756                 $data = array(); 
    757                 foreach ($this->changed as $key) 
    758                 { 
    759                         // Get changed data 
    760                         $data[$key] = $this->object->$key; 
    761                 } 
    762  
    763                 if (empty($this->object->id)) 
    764                 { 
    765                         // Perform an insert 
    766                         $query = self::$db->insert($this->table, $data); 
     796                if ( ! empty($this->changed)) 
     797                { 
     798                        $data = array(); 
     799                        foreach ($this->changed as $key) 
     800                        { 
     801                                // Get changed data 
     802                                $data[$key] = $this->object->$key; 
     803                        } 
     804 
     805                        if (empty($this->object->id)) 
     806                        { 
     807                                // Perform an insert 
     808                                $query = self::$db->insert($this->table, $data); 
     809 
     810                                if ($query->count()) 
     811                                { 
     812                                        // Set current object id by the insert id 
     813                                        $this->object->id = $query->insert_id(); 
     814                                } 
     815                        } 
     816                        else 
     817                        { 
     818                                // Perform an update 
     819                                $query = self::$db->update($this->table, $data, array('id' => $this->object->id)); 
     820                        } 
    767821 
    768822                        if ($query->count()) 
    769823                        { 
    770                                 // Set current object id by the insert id 
    771                                 $this->object->id = $query->insert_id(); 
    772                         } 
    773                 } 
    774                 else 
    775                 { 
    776                         // Perform an update 
    777                         $query = self::$db->update($this->table, $data, array('id' => $this->object->id)); 
    778                 } 
    779  
    780                 if ($query->count()) 
    781                 { 
    782                         // Reset changed data 
    783                         $this->changed = array(); 
    784  
    785                         // Reset SELECT, WHERE, and FROM 
    786                         $this->select = $this->where = $this->from = FALSE; 
     824                                // Reset changed data 
     825                                $this->changed = array(); 
     826 
     827                                // Reset SELECT, WHERE, and FROM 
     828                                $this->select = $this->where = $this->from = FALSE; 
     829 
     830                                // Object has been loaded and saved 
     831                                $this->loaded = $this->saved = TRUE; 
     832                        } 
     833                } 
     834 
     835                if ( ! empty($this->changed_related) AND ($this->saved OR $this->loaded)) 
     836                { 
     837                        foreach ($this->changed_related as $table) 
     838                        { 
     839                                // Skip empty rows 
     840                                if (empty($this->object_related[$table])) 
     841                                        continue; 
     842 
     843                                // Current and new relationships 
     844                                $cur = $this->object_related[$table]['cur']; 
     845                                $new = $this->object_related[$table]['new']; 
     846 
     847                                // Foreign and primary keys 
     848                                $fk = inflector::singular($table).'_id'; 
     849                                $pk = $this->class.'_id'; 
     850 
     851                                // Relationship table 
     852                                $table = $this->related_table($table); 
     853 
     854                                if ($delete = array_diff($cur, $new)) 
     855                                { 
     856                                        // Delete relationships 
     857                                        self::$db->in($fk, $delete)->where($pk, $this->id)->delete($table); 
     858                                } 
     859 
     860                                foreach (array_diff($new, $cur) as $key) 
     861                                { 
     862                                        // Create new relationships 
     863                                        self::$db->insert($table, array($pk => $this->id, $fk => $key)); 
     864                                } 
     865                        } 
    787866                } 
    788867