Changeset 2255

Show
Ignore:
Timestamp:
03/09/2008 11:07:35 PM (10 months ago)
Author:
Shadowhand
Message:

Fixing #406, thanks sbeattie!

Files:
1 modified

Legend:

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

    r2218 r2255  
    7777                        // Preloaded object 
    7878                        $this->object = $id; 
     79 
     80                        // Convert the value to the correct type 
     81                        $this->load_object_types(); 
    7982                } 
    8083                else 
     
    618621                } 
    619622 
     623                // Convert the value to the correct type 
     624                $this->load_object_types(); 
     625 
    620626                // Reset object status 
    621627                $this->changed = array(); 
     
    673679                                // Load the first result, if there is only one result 
    674680                                $this->object = $result->current(); 
     681 
     682                                // Convert the value to the correct type 
     683                                $this->load_object_types(); 
    675684                        } 
    676685                        else 
     
    700709                // Create and return the object 
    701710                return ORM::factory(inflector::singular($table)); 
     711        } 
     712 
     713        /** 
     714         * Converts the loaded object values to correct types. 
     715         * 
     716         * @return void 
     717         */ 
     718        protected function load_object_types() 
     719        { 
     720                foreach (self::$fields[$this->table] as $field => $data) 
     721                { 
     722                        if (isset($this->object->$field)) 
     723                        { 
     724                                if ( ! empty($data['binary']) AND ! empty($data['exact']) AND $data['length'] == 1) 
     725                                { 
     726                                        // Use boolean for an binary(1) field 
     727                                        $data['type'] = 'boolean'; 
     728                                } 
     729 
     730                                switch ($data['type']) 
     731                                { 
     732                                        case 'int': 
     733                                                $this->object->$field = (int) $this->object->$field; 
     734                                        break; 
     735                                        case 'float': 
     736                                                $this->object->$field = (float) $this->object->$field; 
     737                                        break; 
     738                                        case 'boolean': 
     739                                                $this->object->$field = (bool) $this->object->$field; 
     740                                        break; 
     741                                } 
     742                        } 
     743                } 
    702744        } 
    703745