Show
Ignore:
Timestamp:
03/10/2008 09:11:45 AM (10 months ago)
Author:
Shadowhand
Message:

Fixing broken ORM, thanks alexsancho and zeelot.

Files:
1 modified

Legend:

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

    r2262 r2268  
    219219                } 
    220220 
    221                 if (substr($method, 0, 8) === 'find_by_' OR ($all = substr($method, 0, 12)) === 'find_all_by_') 
    222                 { 
    223                         $method = isset($all) ? substr($method, 12) : substr($method, 8); 
    224  
     221                if (substr($method, 0, 8) === 'find_by_' OR substr($method, 0, 12) === 'find_all_by_') 
     222                { 
    225223                        // Make a find_by call 
    226224                        return $this->call_find_by($method, $args); 
     
    230228                { 
    231229                        // Make a find_related call 
    232                         return $this->call_find_related(substr($method, 13)); 
     230                        return $this->call_find_related($method, $args); 
    233231                } 
    234232 
    235233                if (preg_match('/^(has|add|remove)_(.+)/', $method, $matches)) 
    236234                { 
    237                         $action = $matches[1]; 
    238                         $model  = is_object(current($args)) ? current($args) : $this->load_model($matches[2]); 
    239  
    240235                        // Make a has/add/remove call 
    241                         return $this->call_has_add_remove($action, $model); 
     236                        return $this->call_has_add_remove($method, $args, $matches); 
    242237                } 
    243238 
     
    278273         * __call: find_by_*, find_all_by_* 
    279274         * 
    280          * @param   string  column names to find by 
     275         * @param   string  method 
    281276         * @param   array   arguments 
    282277         * @return  object 
     
    284279        protected function call_find_by($method, $args) 
    285280        { 
     281                // Use ALL 
     282                $ALL = (substr($method, 0, 12) === 'find_all_by_'); 
     283 
     284                // Method args 
     285                $method = $ALL ? substr($method, 12) : substr($method, 8); 
     286 
    286287                // WHERE is manually set 
    287288                $this->where = TRUE; 
     
    319320                } 
    320321 
    321                 if (isset($all)) 
     322                if ($ALL) 
    322323                { 
    323324                        // Array of results 
     
    334335         * __call: find_related_* 
    335336         * 
    336          * @param   string  table name 
     337         * @param   string   method name 
     338         * @param   array    arguments 
    337339         * @return  object 
    338340         */ 
    339         protected function call_find_related($table) 
    340         { 
     341        protected function call_find_related($method, $args) 
     342        { 
     343                // Extract table name 
     344                $table = substr($method, 13) 
     345 
    341346                // Construct a new model 
    342347                $model = $this->load_model($table); 
     
    388393         * __call: has_*, add_*, remove_* 
    389394         * 
    390          * @param   string   action: has, add, remove 
    391          * @param   object   model 
     395         * @param   string   method 
     396         * @param   array    arguments 
     397         * @param   array    action matches 
    392398         * @return  boolean 
    393399         */ 
    394         protected function call_has_add_remove($action, $model) 
    395         { 
     400        protected function call_has_add_remove($method, $args, $matches) 
     401        { 
     402                $action = $matches[1]; 
     403                $model  = is_object(current($args)) ? current($args) : $this->load_model($matches[2]); 
     404 
    396405                // Real foreign table name 
    397406                $table = $model->table_name; 
     
    907916        public function __construct($class, $result) 
    908917        { 
     918                // Class name 
    909919                $this->class = $class; 
     920 
     921                // Database result 
    910922                $this->result = $result; 
    911923        } 
     
    936948        public function range($start, $end) 
    937949        { 
     950                // Array of objects 
    938951                $array = array(); 
    939952 
     
    948961                        for ($i = $start; $i < $end; $i++) 
    949962                        { 
     963                                // Insert each object in the range 
    950964                                $array[] = new $class($this->result->offsetGet($i)); 
    951965                        } 
     
    971985                $class = $this->class; 
    972986 
    973                 return $this->result->offsetExists(0) ? new $class($this->result->offsetGet(0)) : FALSE; 
     987                return ($row = $this->result->current()) ? new $class($row) : FALSE; 
    974988        } 
    975989