From 405825ff8a1db02736df2e6b9dc8debae9b3840c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 5 Dec 2016 16:24:02 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0resultSetType?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=20=E7=94=A8=E4=BA=8E=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=9F=A5=E8=AF=A2=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=9B=86=E5=AF=B9=E8=B1=A1=EF=BC=88=E9=BB=98=E8=AE=A4=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E8=BF=94=E5=9B=9E=E6=95=B0=E7=BB=84=EF=BC=89=20Db?= =?UTF-8?q?=E7=B1=BB=E6=9F=A5=E8=AF=A2=E4=B8=8D=E5=86=8D=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E8=87=AA=E5=AE=9A=E4=B9=89=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=9B=86=E5=AF=B9=E8=B1=A1=EF=BC=88=E5=8F=AA=E8=83=BD=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=95=B0=E7=BB=84=E6=88=96=E8=80=85think\Collection?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 12 +++++++++++- library/think/db/Connection.php | 5 +---- library/think/db/Query.php | 31 +++++++++++-------------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 6a6841a6..68688759 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -111,6 +111,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $useGlobalScope = true; // 是否采用批量验证 protected $batchValidate = false; + // 查询数据集对象 + protected $resultSetType; /** * 初始化过的模型. @@ -574,7 +576,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function toCollection($collection) { - return new Collection($collection); + if ($this->resultSetType) { + if ('collection' == $this->resultSetType) { + $collection = new Collection($collection); + } else { + $class = $this->resultSetType; + $collection = new $class($collection); + } + } + return $collection; } /** diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index d9b7978f..f7d33161 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -489,10 +489,7 @@ abstract class Connection $result = $this->PDOStatement->fetchAll($this->fetchType); $this->numRows = count($result); - if (!empty($class)) { - // 返回指定数据集对象类 - $result = new $class($result); - } elseif ('collection' == $this->resultSetType) { + if ('collection' == $this->resultSetType) { // 返回数据集Collection对象 $result = new Collection($result); } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 4d63ba14..a2c0f211 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1318,18 +1318,6 @@ class Query return $this; } - /** - * 指定数据集返回对象 - * @access public - * @param string $class 指定返回的数据集对象类名 - * @return $this - */ - public function fetchClass($class) - { - $this->options['fetch_class'] = $class; - return $this; - } - /** * 设置从主服务器读取数据 * @access public @@ -1967,12 +1955,11 @@ class Query } } - // 返回结果处理 - if (count($resultSet) > 0) { - // 数据列表读取后的处理 - if (!empty($this->model)) { - // 生成模型对象 - $model = $this->model; + // 数据列表读取后的处理 + if (!empty($this->model)) { + // 生成模型对象 + $model = $this->model; + if (count($resultSet) > 0) { foreach ($resultSet as $key => $result) { /** @var Model $result */ $result = new $model($result); @@ -1983,12 +1970,16 @@ class Query } $resultSet[$key] = $result; } - if (!empty($options['with']) && $result instanceof Model) { + if (!empty($options['with'])) { // 预载入 $result->eagerlyResultSet($resultSet, $options['with'], is_object($resultSet) ? get_class($resultSet) : ''); } } - } elseif (!empty($options['fail'])) { + // 模型数据集转换 + $resultSet = (new $model)->toCollection($resultSet); + } + // 返回结果处理 + if (!empty($options['fail']) && count($resultSet) == 0) { $this->throwNotFound($options); } return $resultSet;