Changeset 1276

Show
Ignore:
Timestamp:
11/26/2007 12:29:22 PM (12 months ago)
Author:
Shadowhand
Message:

Adding auto_save option to ORM, as per #215. Thanks dlib!

Files:
1 modified

Legend:

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

    r1271 r1276  
    1616        protected static $db; 
    1717 
     18        // Automatic saving on model destruction 
     19        protected $auto_save = FALSE; 
     20 
    1821        // This table 
    1922        protected $class; 
     
    8992 
    9093        /** 
    91          * Magic method for getting object keys. 
     94         * Enables automatic saving of the object when the model is destroyed. 
     95         */ 
     96        public function __destruct() 
     97        { 
     98                if ($this->auto_save == TRUE) 
     99                { 
     100                        // Automatically save the model 
     101                        $this->save(); 
     102                } 
     103        } 
     104 
     105        /** 
     106         * Magic method for getting object and model keys. 
    92107         */ 
    93108        public function __get($key) 
     
    97112                        return $this->object->$key; 
    98113                } 
    99                 elseif ($key === 'table_name') 
    100                 { 
    101                         return $this->table; 
    102                 } 
    103                 elseif ($key === 'class_name') 
    104                 { 
    105                         return $this->class; 
    106                 } 
    107         } 
    108  
    109         /** 
    110          * Magic method for setting object keys. 
     114                else 
     115                { 
     116                        switch($key) 
     117                        { 
     118                                case 'table_name': 
     119                                        return $this->table; 
     120                                break; 
     121                                case 'class_name': 
     122                                        return $this->class; 
     123                                break; 
     124                                case 'auto_save': 
     125                                        return $this->auto_save; 
     126                                break; 
     127                        } 
     128                } 
     129        } 
     130 
     131        /** 
     132         * Magic method for setting object and model keys. 
    111133         */ 
    112134        public function __set($key, $value) 
     
    121143                                // Data has changed 
    122144                                $this->changed[$key] = $key; 
     145                        } 
     146                } 
     147                else 
     148                { 
     149                        switch($key) 
     150                        { 
     151                                case 'auto_save': 
     152                                        $this->auto_save = (bool) $value; 
     153                                break; 
    123154                        } 
    124155                }