Changeset 2033

Show
Ignore:
Timestamp:
02/11/2008 11:13:50 PM (11 months ago)
Author:
Shadowhand
Message:

Better Database instance caching for Model.

Files:
1 modified

Legend:

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

    r1911 r2033  
    11<?php defined('SYSPATH') or die('No direct script access.'); 
    22/** 
    3  * Model class. 
     3 * Model base class. 
    44 * 
    55 * $Id$ 
     
    1515 
    1616        /** 
    17          * Loads database to $this->db. 
     17         * Loads or sets the database instance. 
     18         * 
     19         * @param   object   Database instance 
     20         * @return  void 
    1821         */ 
    19         public function __construct() 
     22        public function __construct($database = NULL) 
    2023        { 
    21                 // Load the database into the model 
    22                 if (Event::has_run('system.pre_controller')) 
     24                static $db; 
     25 
     26                if (is_object($database) AND ($database instanceof Database)) 
    2327                { 
    24                         $this->db = isset(Kohana::instance()->db) ? Kohana::instance()->db : new Database('default'); 
     28                        // Use the passed database instance 
     29                        $this->db = $database; 
    2530                } 
    2631                else 
    2732                { 
    28                         $this->db = new Database('default'); 
     33                        // Load the default database if necessary 
     34                        ($db === NULL) and $db = new Database('default'); 
     35 
     36                        // Use the static database 
     37                        $this->db = $db; 
    2938                } 
    3039        } 
    3140 
    32 } // End Model class 
     41} // End Model