Show
Ignore:
Timestamp:
11/24/2007 07:11:57 PM (12 months ago)
Author:
Shadowhand
Message:

Changes to core:

  • Added system/controllers/template.php, for auto-rendering templates
  • Updated ORM with some new syntax, adding select() and where(), and optimizing find()

These changes may break things in ORM that currently work, more testing is required.

Files:
1 modified

Legend:

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

    r1230 r1255  
    2020        protected $table; 
    2121 
    22         // This object 
     22        // SQL building status 
     23        protected $select = FALSE; 
     24        protected $where = FALSE; 
     25 
     26        // Currently loaded object 
    2327        protected $object; 
    2428 
     
    2630        protected $changed = array(); 
    2731 
    28         // Relationships 
     32        // Object Relationships 
    2933        protected $has_one = array(); 
    3034        protected $has_many = array(); 
     
    4650                        // Insert db into this object 
    4751                        self::$db = Kohana::instance()->db; 
    48                 } 
    49  
    50                 // Define ALL 
    51                 defined('ALL') or define('ALL', 100); 
     52 
     53                        // Define ALL 
     54                        defined('ALL') or define('ALL', 100); 
     55                } 
    5256 
    5357                // Fetch table name 
     
    7175                else 
    7276                { 
    73                         // Load the object 
    74                         $this->find($id); 
     77                        if (empty($id)) 
     78                        { 
     79                                // Load an empty object 
     80                                $this->load_result(array()); 
     81                        } 
     82                        else 
     83                        { 
     84                                // Query and load object 
     85                                $this->where($id); 
     86                                $this->find(); 
     87                        } 
    7588                } 
    7689        } 
     
    159172                        $model = $this->load_model($table); 
    160173 
    161                         if (in_array($table, $this->has_and_belongs_to_many)) 
    162                         { 
    163                                 // Execute joins for many<>many 
     174                        if (in_array($table, $this->has_one)) 
     175                        { 
     176                                // Find one<>one relationships 
     177                                $model->find(array($this->class.'_id' => $this->object->id)); 
     178                                return $model; 
     179                        } 
     180                        elseif (in_array($table, $this->has_many)) 
     181                        { 
     182                                // Find one<>many relationships 
     183                                self::$db->where($this->class.'_id', $this->object->id); 
     184                        } 
     185                        elseif (in_array($table, $this->has_and_belongs_to_many)) 
     186                        { 
     187                                // Find many<>many relationships, via a JOIN 
    164188                                $this->related_join($table); 
    165                         } 
    166                         else 
    167                         { 
    168                                 // Add this object id to WHERE 
    169                                 self::$db->where($this->class.'_id', $this->object->id); 
    170189                        } 
    171190 
     
    276295 
    277296        /** 
     297         * Select  
     298         */ 
     299        public function select() 
     300        { 
     301                // Return all the objects in the table 
     302                if (func_num_args() === 0) 
     303                { 
     304                        self::$db->select($this->table.'.*'); 
     305                } 
     306                else 
     307                { 
     308                        $args = func_get_args(); 
     309 
     310                        if (count($args) === 1) 
     311                        { 
     312                                self::$db->select(current($args)); 
     313                        } 
     314                        else 
     315                        { 
     316                                self::$db->select($args); 
     317                        } 
     318                } 
     319 
     320                // SELECT has been set 
     321                $this->select = TRUE; 
     322 
     323                return $this; 
     324        } 
     325 
     326        /** 
     327         * Method: where 
     328         *  Generate a WHERE array. 
     329         */ 
     330        public function where() 
     331        { 
     332                switch(func_num_args()) 
     333                { 
     334                        case 1: 
     335                                $id = func_get_arg(0); 
     336                                if ( ! empty($id)) 
     337                                { 
     338                                        self::$db->where(is_array($id) ? $id : array('id' => $id)); 
     339 
     340                                        // WHERE has been set 
     341                                        $this->where = TRUE; 
     342                                } 
     343                        break; 
     344                        case 2: 
     345                                $key = func_get_arg(0); 
     346                                $val = func_get_arg(1); 
     347 
     348                                if (is_array($key)) 
     349                                { 
     350                                        // Choose the OR method to use 
     351                                        $or = (strpos($val, '%') === FALSE) ? 'orwhere' : 'orlike'; 
     352 
     353                                        foreach ($key as $k) 
     354                                        { 
     355                                                // Use OR WHERE/LIKE 
     356                                                self::$db->$or($k, $val); 
     357                                        } 
     358                                } 
     359                                else 
     360                                { 
     361                                        self::$db->where(array($key => $val)); 
     362                                } 
     363 
     364                                // WHERE has been set 
     365                                $this->where = TRUE; 
     366                        break; 
     367                } 
     368 
     369                return $this; 
     370        } 
     371 
     372        /** 
    278373         * Method: find 
    279374         *  Find and load this object data. 
     
    287382         *  Array of objects if where is an array 
    288383         */ 
    289         public function find($where = FALSE, $limit = 1) 
    290         { 
    291                 if ($limit === ALL OR is_array($where) OR $where = $this->where($where)) 
    292                 { 
    293                         // Use limit 
    294                         ($limit === ALL) or self::$db->limit($limit); 
    295  
    296                         // Use where 
    297                         empty($where) or self::$db->where($where); 
    298  
    299                         $query = self::$db 
    300                                 ->select($this->table.'.*') 
    301                                 ->from($this->table) 
    302                                 ->get(); 
    303  
    304                         if ($limit > 1) 
    305                         { 
    306                                 $model = get_class($this); 
    307  
    308                                 // Construct an array of objects 
    309                                 $objects = array(); 
    310                                 foreach($query as $result) 
    311                                 { 
    312                                         $objects[] = new $model($result); 
    313                                 } 
    314                                 return $objects; 
    315                         } 
    316  
    317                         if (count($query) === 1) 
    318                         { 
    319                                 // Fetch the first result 
    320                                 $this->object = $query->current(); 
    321                         } 
    322                 } 
    323  
    324                 if (empty($this->object)) 
    325                 { 
    326                         // Create a new object 
    327                         $this->object = new StdClass(); 
    328  
    329                         // Fill the fields 
    330                         foreach(self::$fields[$this->table] as $field) 
    331                         { 
    332                                 $this->object->$field = ''; 
    333                         } 
    334                 } 
    335  
    336                 // Reset changed 
    337                 $this->changed = array(); 
    338  
    339                 // Return true if something was actually loaded 
    340                 return ($this->object->id != 0); 
     384        public function find($limit = 1, $offset = FALSE) 
     385        { 
     386                // SELECT 
     387                ($this->select == FALSE) and $this->select(); 
     388                // WHERE 
     389                ($this->where == FALSE) and $this->where(); 
     390                // LIMIT 
     391                ($limit !== ALL) and self::$db->limit($limit, $offset); 
     392 
     393                // Perform the query 
     394                $query = self::$db 
     395                        ->from($this->table) 
     396                        ->get(); 
     397 
     398                // Load the query result 
     399                return $this->load_result($query); 
    341400        } 
    342401 
     
    421480 
    422481        /** 
    423          * Method: where 
    424          *  Generate a WHERE array. 
     482         * Loads a database object result. 
    425483         * 
    426484         * Parameters: 
    427          *  id - id or array of id's 
    428          * 
    429          * Returns: 
    430          *  FALSE or Array 
    431          */ 
    432         protected function where($id) 
    433         { 
    434                 if (empty($id)) 
    435                         return FALSE; 
    436  
    437                 if (is_array($id)) 
    438                         return $id; 
    439  
    440                 if (ctype_digit((string) $id)) 
    441                         return array('id' => $id); 
     485         *  result - database result object 
     486         * 
     487         * Return: 
     488         *  boolean - TRUE for single result, FALSE for an empty result 
     489         *  array   - Multiple row result set 
     490         */ 
     491        protected function load_result($result) 
     492        { 
     493                if (count($result) > 0) 
     494                { 
     495                        if (count($result) > 1) 
     496                        { 
     497                                // Model class name 
     498                                $class = get_class($this); 
     499 
     500                                $array = array(); 
     501                                foreach($result as $row) 
     502                                { 
     503                                        // Add object to the array 
     504                                        $array[] = new $class($row); 
     505                                } 
     506 
     507                                // Return an array of all the objects 
     508                                return $array; 
     509                        } 
     510                        else 
     511                        { 
     512                                // Clear the changed keys, a new object has been loaded 
     513                                $this->changed = array(); 
     514 
     515                                // Fetch the first result 
     516                                $this->object = $result->current(); 
     517                        } 
     518                } 
     519                else 
     520                { 
     521                        // Create an empty object 
     522                        $this->object = new StdClass(); 
     523 
     524                        // Empty the object 
     525                        foreach(self::$fields[$this->table] as $field) 
     526                        { 
     527                                $this->object->$field = ''; 
     528                        } 
     529                } 
     530 
     531                // Return this object 
     532                return $this; 
    442533        } 
    443534