From 35890dad5c2533da7d7d4995963bdba03df613c4 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 20 Sep 2016 12:04:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=A2=9E=E5=8A=A0query=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E9=85=8D=E7=BD=AE=E6=9F=A5=E8=AF=A2=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=90=8D=E7=A7=B0=20=E6=A8=A1=E5=9E=8B=E7=B1=BB?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0query=E5=B1=9E=E6=80=A7=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=A8=A1=E5=9E=8B=E9=9C=80=E8=A6=81=E7=9A=84?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=AF=B9=E8=B1=A1=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 5 +++-- library/think/db/Connection.php | 11 ++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 30b71d52..1a9696b6 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -39,11 +39,12 @@ use think\paginator\Collection as PaginatorCollection; */ abstract class Model implements \JsonSerializable, \ArrayAccess { - // 数据库对象池 protected static $links = []; // 数据库配置 protected $connection = []; + // 数据库查询对象 + protected $query; // 当前模型名称 protected $name; // 数据表名称 @@ -147,7 +148,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = $this->class; if (!isset(self::$links[$model])) { // 设置当前模型 确保查询返回模型对象 - $query = Db::connect($this->connection)->model($model); + $query = Db::connect($this->connection)->model($model, $this->query); // 设置当前数据表和模型名 if (!empty($this->table)) { diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index a69f1dd6..f405f9b9 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -104,6 +104,8 @@ abstract class Connection 'sql_explain' => false, // Builder类 'builder' => '', + // Query类 + 'query' => '\\think\\db\\Query', ]; // PDO连接参数 @@ -131,12 +133,14 @@ abstract class Connection * 创建指定模型的查询对象 * @access public * @param string $model 模型类名称 + * @param string $queryClass 查询对象类名 * @return Query */ - public function model($model) + public function model($model, $queryClass = '') { if (!isset($this->query[$model])) { - $this->query[$model] = new Query($this, $model); + $class = $queryClass ?: $this->config['query']; + $this->query[$model] = new $class($this, $model); } return $this->query[$model]; } @@ -151,7 +155,8 @@ abstract class Connection public function __call($method, $args) { if (!isset($this->query['database'])) { - $this->query['database'] = new Query($this); + $class = $this->config['query']; + $this->query['database'] = new $class($this); } return call_user_func_array([$this->query['database'], $method], $args); }