示例#1
0
 /**
  * 获得静态对象实例
  *
  * @return self
  */
 public static function instance($offset = null)
 {
     $dkey = empty($offset) ? 'default' : $offset;
     $key = 'database:' . $dkey;
     $oo = '__objects';
     if (!empty($GLOBALS[$oo][$key]) && is_object($GLOBALS[$oo][$key])) {
         return $GLOBALS[$oo][$key];
     }
     $policy = Kohana::$config->load('zpdo')->{$dkey};
     foreach (array('dsn', 'username', 'password', 'boost', 'options') as $k) {
         $policy[$k] = array_key_exists($k, $policy) ? $policy[$k] : null;
     }
     try {
         $object = new Zpdo_Natural($policy);
     } catch (Exception $e) {
         if (is_null($object) && $dkey != 'default') {
             return Zpdo::instance();
         }
         throw $e;
     }
     //循环执行boost中的SQL语句
     if (0 < count($policy['boost'])) {
         foreach ($policy['boost'] as $v) {
             $object->exec($v);
         }
     }
     //END boost
     $GLOBALS[$oo][$key] = $object;
     return $GLOBALS[$oo][$key];
 }
示例#2
0
 /**
  * 获得 数据库 对象
  * @return object
  */
 public function getDatabase($dbname = null)
 {
     if (!is_object($this->_db)) {
         $this->_db = Zpdo::instance($dbname);
     }
     return $this->_db;
 }