Show
Ignore:
Timestamp:
02/28/2008 10:50:07 AM (11 months ago)
Author:
gregmac
Message:

Add ORM->primary_column property to allow overriding primary field name

Files:
1 modified

Legend:

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

    r2180 r2183  
    2828        protected $class; 
    2929        protected $table; 
     30         
     31        // Primary column name 
     32        protected $primary_column = 'id'; 
    3033 
    3134        // SQL building status 
     
    134137                        // Set the model name 
    135138                        $model = ucfirst($key).'_Model'; 
    136  
    137                         if (empty($this->object->id)) 
     139                         
     140                        $idcolumn = $this->primary_column; 
     141                         
     142                        if (empty($this->object->$idcolumn)) 
    138143                        { 
    139144                                // Load an empty model 
     
    149154                                        ? $this->object->$child_id 
    150155                                        // Get the foreign object using the primary key of this object 
    151                                         : array($this->class.'_id', $this->object->id)); 
     156                                        : array($this->class.'_id', $this->object->$idcolumn)); 
    152157                        } 
    153158 
     
    171176                } 
    172177        } 
     178 
    173179 
    174180        /** 
     
    470476        protected function where_key($id = NULL) 
    471477        { 
    472                 return $this->table.'.id'; 
     478                return $this->table.'.'.$this->primary_column; 
    473479        } 
    474480 
     
    518524                if (empty($this->changed)) 
    519525                        return TRUE; 
     526                 
     527                $idcolumn = $this->primary_column; 
    520528 
    521529                $data = array(); 
     
    526534                } 
    527535 
    528                 if ($this->object->id == '') 
     536                if ($this->object->$idcolumn == '') 
    529537                { 
    530538                        // Perform an insert 
     
    534542                        { 
    535543                                // Set current object id by the insert id 
    536                                 $this->object->id = $query->insert_id(); 
     544                                $this->object->$idcolumn = $query->insert_id(); 
    537545                        } 
    538546                } 
     
    540548                { 
    541549                        // Perform an update 
    542                         $query = self::$db->update($this->table, $data, array('id' => $this->object->id)); 
     550                        $query = self::$db->update($this->table, $data, array($idcolumn => $this->object->$idcolumn)); 
    543551                } 
    544552 
     
    790798                self::$db->where("$join.$primary", $this->object->id)->join($join, "$join.$foreign", "$table.id"); 
    791799        } 
    792  
    793800} // End ORM 
    794801