From 1e1d2ab951d91d65671e82eb5eaabbaebcd0333a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 19 Apr 2016 17:51:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Query=E7=B1=BB=E7=9A=84builde?= =?UTF-8?q?r=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 7 ++++--- library/think/db/Builder.php | 18 ++++++++++++++---- library/think/db/Query.php | 4 +++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index a5df413a..4aa4c4e5 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -853,10 +853,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (!isset(self::$links[$model])) { self::$links[$model] = Db::connect(static::$connection); + self::$links[$model]->setTable(static::$tableName); + $name = basename(str_replace('\\', '/', $model)); + self::$links[$model]->name($name); } - self::$links[$model]->setTable(static::$tableName); - $name = basename(str_replace('\\', '/', $model)); - self::$links[$model]->name($name); + // 设置当前模型 确保查询返回模型对象 self::$links[$model]->model($model); // 返回当前数据库对象 diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 08dbbb7e..9872c815 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -38,10 +38,20 @@ abstract class Builder * @access public * @param object $db 数据库对象实例 */ - public function __construct($connection, $query) + public function __construct($connection) { $this->connection = $connection; - $this->query = $query; + } + + /** + * 设置当前的Query对象实例 + * @access protected + * @param \think\db\Query $query 当前查询对象实例 + * @return void + */ + public function setQuery($query) + { + $this->query = $query; } /** @@ -299,13 +309,13 @@ abstract class Builder if ($value instanceof \Closure) { $whereStr .= $key . ' ' . $exp . ' ' . $this->parseClosure($value); } else { - $value = is_array($value) ? $value : explode(',', $value) ; + $value = is_array($value) ? $value : explode(',', $value); $zone = implode(',', $this->parseValue($value)); $whereStr .= $key . ' ' . $exp . ' (' . $zone . ')'; } } elseif (in_array($exp, ['NOT BETWEEN', 'BETWEEN'])) { // BETWEEN 查询 - $data = is_array($value) ? $value : explode(',', $value) ; + $data = is_array($value) ? $value : explode(',', $value); $whereStr .= $key . ' ' . $exp . ' ' . $this->parseValue($data[0]) . ' AND ' . $this->parseValue($data[1]); } elseif (in_array($exp, ['NOT EXISTS', 'EXISTS'])) { // EXISTS 查询 diff --git a/library/think/db/Query.php b/library/think/db/Query.php index dd33037c..1c3e5fe4 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -77,8 +77,10 @@ class Query $driver = $this->driver; if (!isset($builder[$driver])) { $class = '\\think\\db\\builder\\' . ucfirst($driver); - $builder[$driver] = new $class($this->connection, $this); + $builder[$driver] = new $class($this->connection); } + // 设置当前查询对象 + $builder[$driver]->setQuery($this); return $builder[$driver]; }