/** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options = []) { if (!empty($options)) { $this->options = array_merge($this->options, $options); } $this->handler = \think\Db::connect(!empty($this->options['hostname']) || !empty($this->options['dsn']) ? $this->options : []); }
/** * 架构函数 * @param array $options 缓存参数 * @access public */ public function __construct($options = []) { if (!empty($options)) { $this->options = array_merge($this->options, $options); } $this->handler = \Think\Db::connect(); }
/** * 获取当前模型的数据库查询对象 * @access public * @param bool $baseQuery 是否调用全局查询范围 * @return Query */ public function db($baseQuery = true) { $model = $this->class; if (!isset(self::$links[$model])) { // 设置当前模型 确保查询返回模型对象 $query = Db::connect($this->connection)->model($model, $this->query); // 设置当前数据表和模型名 if (!empty($this->table)) { $query->setTable($this->table); } else { $query->name($this->name); } if (!empty($this->pk)) { $query->pk($this->pk); } self::$links[$model] = $query; } // 全局作用域 if ($baseQuery && method_exists($this, 'base')) { call_user_func_array([$this, 'base'], [&self::$links[$model]]); } // 返回当前模型的数据库查询对象 return self::$links[$model]; }
/** * 实例化数据库类 * @param array $config 数据库配置参数 * @return object */ function db($config = []) { return \think\Db::connect($config); }
/** * 实例化数据库类 * @param string $name 操作的数据表名称(不含前缀) * @param array|string $config 数据库配置参数 * @param bool $force 是否强制重新连接 * @return \think\db\Query */ function db($name = '', $config = [], $force = true) { return Db::connect($config, $force)->name($name); }
/** * 切换当前的数据库连接 * @access public * @param mixed $config * @return $this */ public function connect($config) { $this->connection = Db::connect($config); return $this; }
/** * 实例化数据库类 * @param string $name 操作的数据表名称(不含前缀) * @param array|string $config 数据库配置参数 * @return \think\db\Connection */ function db($name = '', $config = []) { return Db::connect($config)->name($name); }
/** * 初始化数据库对象 * @access public * @param bool $new 是否采用新的数据库连接 * @return \think\db\Driver */ public static function db($new = false) { $model = get_called_class(); if ($new || !isset(self::$links[$model])) { $class = new static([], false); self::$links[$model] = Db::connect($class->connection, $new); self::$instance[$model] = $class; } else { $class = self::$instance[$model]; } // 设置当前数据表和模型名 if (!empty($class->table)) { self::$links[$model]->table($class->table); } else { $name = !empty($class->name) ? $class->name : basename(str_replace('\\', '/', $model)); self::$links[$model]->name($name); } // 设置当前模型 确保查询返回模型对象 self::$links[$model]->model($model); // 返回当前数据库对象 return self::$links[$model]; }
/** * 数据库初始化 并取得数据库类实例 * @param mixed $config 数据库配置 * @param bool|string $name 连接标识 true 强制重新连接 * @return \think\db\Connection */ public static function db($config = [], $name = false) { return Db::connect($config, $name); }
public function testConnect() { Db::connect('mysql://root@127.0.0.1/test#utf8')->execute('show databases'); }
/** * 获取当前模型的数据库查询对象 * @access public * @return Query */ public function db() { $model = $this->class; if (!isset(self::$links[$model])) { // 设置当前模型 确保查询返回模型对象 $query = Db::connect($this->connection)->model($model, $this->query); // 设置当前数据表和模型名 if (!empty($this->table)) { $query->setTable($this->table); } else { $query->name($this->name); } if (!empty($this->pk)) { $query->pk($this->pk); } self::$links[$model] = $query; } // 返回当前模型的数据库查询对象 return self::$links[$model]; }
/** * 实例化数据库 * @param mixed $config 数据库配置 * @return object */ public static function db($config = []) { return Db::connect($config); }
/** * 获取当前模型的数据库查询对象 * @access public * @return Query */ public function db() { $model = $this->class; if (!isset(self::$links[$model])) { // 设置当前模型 确保查询返回模型对象 $query = Db::connect($this->connection)->model($model); // 设置当前数据表和模型名 if (!empty($this->table)) { $query->setTable($this->table); } else { $query->name($this->name); } if (!empty($this->field)) { if (true === $this->field) { $type = $query->getTableInfo('', 'type'); } else { $type = []; foreach ((array) $this->field as $key => $val) { if (is_int($key)) { $key = $val; $val = 'varchar'; } $type[$key] = $val; } } $query->setFieldType($type); $this->field = array_keys($type); $query->allowField($this->field); } if (!empty($this->pk)) { $query->pk($this->pk); } self::$links[$model] = $query; } // 返回当前模型的数据库查询对象 return self::$links[$model]; }
/** * 架构函数 * @access public * @param object|string $connection 数据库对象实例 * @throws Exception */ public function __construct($connection = '') { $this->connection = $connection ?: Db::connect([], true); $this->driver = $this->connection->getDriverName(); }