From 6f02e4b1276c291fec0198332b7809b858ff7d53 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 22 Jan 2017 12:42:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E8=81=94=E9=A2=84=E8=BD=BD=E5=85=A5?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=B0=83=E6=95=B4=20=E5=8F=96=E6=B6=88class?= =?UTF-8?q?=E5=8F=82=E6=95=B0=20=E5=85=B3=E8=81=94=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86=E6=9F=A5=E8=AF=A2=E6=A0=B9=E6=8D=AE=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E6=95=B0=E6=8D=AE=E9=9B=86=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=AE=BE=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lang/zh-cn.php | 1 + library/think/Model.php | 13 +++++---- library/think/db/Connection.php | 4 +-- library/think/db/Query.php | 22 ++++++++------- library/think/model/Relation.php | 7 ++--- library/think/model/relation/BelongsTo.php | 28 ++++++++++--------- .../think/model/relation/BelongsToMany.php | 10 +++---- library/think/model/relation/HasMany.php | 15 ++++------ library/think/model/relation/HasOne.php | 25 +++++++++-------- library/think/model/relation/MorphMany.php | 14 ++++------ library/think/model/relation/MorphTo.php | 13 +++++---- library/think/model/relation/OneToOne.php | 10 +++---- 12 files changed, 79 insertions(+), 83 deletions(-) diff --git a/lang/zh-cn.php b/lang/zh-cn.php index 6d33b0ce..b837bc41 100644 --- a/lang/zh-cn.php +++ b/lang/zh-cn.php @@ -63,4 +63,5 @@ return [ 'route name not exists' => '路由标识不存在(或参数不够)', 'invalid request' => '非法请求', 'bind attr has exists' => '模型的属性已经存在', + 'relation data not exists' => '关联数据不存在', ]; diff --git a/library/think/Model.php b/library/think/Model.php index eb01a3f6..8cf92f73 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -155,6 +155,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->dateFormat = $this->db(false)->getConfig('datetime_format'); } + if (is_null($this->resultSetType)) { + $this->resultSetType = $this->db(false)->getConfig('resultset_type'); + } // 执行初始化操作 $this->initialize(); } @@ -1404,10 +1407,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param array $resultSet 数据集 * @param string $relation 关联名 - * @param string $class 数据集对象名 为空表示数组 * @return array */ - public function eagerlyResultSet(&$resultSet, $relation, $class = '') + public function eagerlyResultSet(&$resultSet, $relation) { $relations = is_string($relation) ? explode(',', $relation) : $relation; foreach ($relations as $key => $relation) { @@ -1421,7 +1423,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure, $class); + $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure); } } @@ -1430,10 +1432,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param Model $result 数据对象 * @param string $relation 关联名 - * @param string $class 数据集对象名 为空表示数组 * @return Model */ - public function eagerlyResult(&$result, $relation, $class = '') + public function eagerlyResult(&$result, $relation) { $relations = is_string($relation) ? explode(',', $relation) : $relation; @@ -1448,7 +1449,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess list($relation, $subRelation) = explode('.', $relation, 2); } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure, $class); + $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure); } } diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 9c66ddd1..6198f924 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -528,11 +528,11 @@ abstract class Connection } /** - * 获得数据集 + * 获得数据集数组 * @access protected * @param bool $pdo 是否返回PDOStatement * @param bool $procedure 是否存储过程 - * @return mixed + * @return array */ protected function getResult($pdo = false, $procedure = false) { diff --git a/library/think/db/Query.php b/library/think/db/Query.php index dc05ddaf..b7288c93 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2289,29 +2289,31 @@ class Query // 数据列表读取后的处理 if (!empty($this->model)) { // 生成模型对象 - $model = $this->model; + $modelName = $this->model; if (count($resultSet) > 0) { foreach ($resultSet as $key => $result) { /** @var Model $result */ - $result = new $model($result); - $result->isUpdate(true); + $model = new $modelName($result); + $model->isUpdate(true); // 关联查询 if (!empty($options['relation'])) { - $result->relationQuery($options['relation']); + $model->relationQuery($options['relation']); } // 关联统计 if (!empty($options['with_count'])) { - $result->relationCount($result, $options['with_count']); + $model->relationCount($model, $options['with_count']); } - $resultSet[$key] = $result; + $resultSet[$key] = $model; } if (!empty($options['with'])) { // 预载入 - $result->eagerlyResultSet($resultSet, $options['with'], is_object($resultSet) ? get_class($resultSet) : ''); + $model->eagerlyResultSet($resultSet, $options['with']); } + // 模型数据集转换 + $resultSet = $model->toCollection($resultSet); + } else { + $resultSet = (new $modelName)->toCollection($resultSet); } - // 模型数据集转换 - $resultSet = (new $model)->toCollection($resultSet); } elseif ('collection' == $this->connection->getConfig('resultset_type')) { // 返回Collection对象 $resultSet = new Collection($resultSet); @@ -2406,7 +2408,7 @@ class Query } // 预载入查询 if (!empty($options['with'])) { - $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : ''); + $data->eagerlyResult($data, $options['with']); } // 关联统计 if (!empty($options['with_count'])) { diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index b16d08e4..5c2db75e 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -27,8 +27,6 @@ abstract class Relation protected $foreignKey; // 关联表主键 protected $localKey; - // 关联查询条件 - protected $where; // 关联查询参数 protected $option; // 基础查询 @@ -68,12 +66,11 @@ abstract class Relation * 封装关联数据集 * @access public * @param array $resultSet 数据集 - * @param string $class 数据集类名 * @return mixed */ - protected function resultSetBuild($resultSet, $class = '') + protected function resultSetBuild($resultSet) { - return $class ? new $class($resultSet) : $resultSet; + return (new $this->model)->toCollection($resultSet); } /** diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 85b04b7a..1a3cad6f 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Exception; use think\Loader; use think\Model; @@ -57,10 +58,9 @@ class BelongsTo extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure, $class) + protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -74,8 +74,7 @@ class BelongsTo extends OneToOne } if (!empty($range)) { - $this->where[$localKey] = ['in', $range]; - $data = $this->eagerlyWhere($this, [ + $data = $this->eagerlyWhere($this, [ $localKey => [ 'in', $range, @@ -85,10 +84,13 @@ class BelongsTo extends OneToOne $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { - if (!isset($data[$result->$foreignKey])) { - $data[$result->$foreignKey] = []; + // 关联模型 + if (!isset($data[$result->$localKey])) { + throw new Exception('relation data not exists :' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class); + if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); @@ -106,19 +108,19 @@ class BelongsTo extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlyOne(&$result, $relation, $subRelation, $closure, $class) + protected function eagerlyOne(&$result, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; $data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure); - // 关联数据封装 - if (!isset($data[$result->$foreignKey])) { - $data[$result->$foreignKey] = []; + // 关联模型 + if (!isset($data[$result->$localKey])) { + throw new Exception('relation data not exists :' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$foreignKey], $class); if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 03992470..9c6337dc 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -84,10 +84,9 @@ class BelongsToMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -117,7 +116,7 @@ class BelongsToMany extends Relation $data[$result->$pk] = []; } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk])); } } } @@ -129,10 +128,9 @@ class BelongsToMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $pk = $result->getPk(); if (isset($result->$pk)) { @@ -144,7 +142,7 @@ class BelongsToMany extends Relation if (!isset($data[$pk])) { $data[$pk] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$pk])); } } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 49ea2e78..32cf1b76 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -60,7 +60,7 @@ class HasMany extends Relation * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $range = []; @@ -72,8 +72,7 @@ class HasMany extends Relation } if (!empty($range)) { - $this->where[$this->foreignKey] = ['in', $range]; - $data = $this->eagerlyOneToMany($this, [ + $data = $this->eagerlyOneToMany($this, [ $this->foreignKey => [ 'in', $range, @@ -86,7 +85,7 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$localKey])); } } } @@ -101,7 +100,7 @@ class HasMany extends Relation * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $localKey = $this->localKey; @@ -111,7 +110,7 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey])); } } @@ -259,9 +258,7 @@ class HasMany extends Relation protected function baseQuery() { if (empty($this->baseQuery)) { - if (isset($this->where)) { - $this->query->where($this->where); - } elseif (isset($this->parent->{$this->localKey})) { + if (isset($this->parent->{$this->localKey})) { // 关联查询带入关联条件 $this->query->where($this->foreignKey, $this->parent->{$this->localKey}); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 5c7ecd23..751f53d5 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Exception; use think\Loader; use think\Model; @@ -84,10 +85,9 @@ class HasOne extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure, $class) + protected function eagerlySet(&$resultSet, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -101,8 +101,7 @@ class HasOne extends OneToOne } if (!empty($range)) { - $this->where[$foreignKey] = ['in', $range]; - $data = $this->eagerlyWhere($this, [ + $data = $this->eagerlyWhere($this, [ $foreignKey => [ 'in', $range, @@ -112,10 +111,12 @@ class HasOne extends OneToOne $attr = Loader::parseName($relation); // 关联数据封装 foreach ($resultSet as $result) { + // 关联模型 if (!isset($data[$result->$localKey])) { - $data[$result->$localKey] = []; + throw new Exception('relation data not exists : ' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$localKey], $class); if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); @@ -133,19 +134,21 @@ class HasOne extends OneToOne * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - protected function eagerlyOne(&$result, $relation, $subRelation, $closure, $class) + protected function eagerlyOne(&$result, $relation, $subRelation, $closure) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; $data = $this->eagerlyWhere($this, [$foreignKey => $result->$localKey], $foreignKey, $relation, $subRelation, $closure); - // 关联数据封装 + + // 关联模型 if (!isset($data[$result->$localKey])) { - $data[$result->$localKey] = []; + throw new Exception('relation data not exists :' . $this->model); + } else { + $relationModel = $data[$result->$localKey]; } - $relationModel = $this->resultSetBuild($data[$result->$localKey], $class); + if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 71719c01..e5ed1701 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -65,10 +65,9 @@ class MorphMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $morphType = $this->morphType; $morphKey = $this->morphKey; @@ -83,9 +82,7 @@ class MorphMany extends Relation } if (!empty($range)) { - $this->where[$morphKey] = ['in', $range]; - $this->where[$morphType] = $type; - $data = $this->eagerlyMorphToMany([ + $data = $this->eagerlyMorphToMany([ $morphKey => ['in', $range], $morphType => $type, ], $relation, $subRelation, $closure); @@ -96,7 +93,7 @@ class MorphMany extends Relation if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr($attr, $this->resultSetBuild($data[$result->$pk])); } } } @@ -108,15 +105,14 @@ class MorphMany extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $pk = $result->getPk(); if (isset($result->$pk)) { $data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure); - $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk], $class)); + $result->setAttr(Loader::parseName($relation), $this->resultSetBuild($data[$result->$pk])); } } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 8051862d..ffad707b 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -11,6 +11,7 @@ namespace think\model\relation; +use think\Exception; use think\Loader; use think\Model; use think\model\Relation; @@ -95,10 +96,9 @@ class MorphTo extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { $morphKey = $this->morphKey; $morphType = $this->morphType; @@ -125,10 +125,12 @@ class MorphTo extends Relation } foreach ($resultSet as $result) { if ($key == $result->$morphType) { + // 关联模型 if (!isset($data[$result->$morphKey])) { - $data[$result->$morphKey] = []; + throw new Exception('relation data not exists :' . $this->model); + } else { + $result->setAttr($attr, $data[$result->$morphKey]); } - $result->setAttr($attr, $this->resultSetBuild($data[$result->$morphKey], $class)); } } } @@ -142,10 +144,9 @@ class MorphTo extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { $morphKey = $this->morphKey; $morphType = $this->morphType; diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index fd11733d..0ec28987 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -100,14 +100,13 @@ abstract class OneToOne extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure) { if (1 == $this->eagerlyType) { // IN查询 - $this->eagerlySet($resultSet, $relation, $subRelation, $closure, $class); + $this->eagerlySet($resultSet, $relation, $subRelation, $closure); } else { // 模型关联组装 foreach ($resultSet as $result) { @@ -123,14 +122,13 @@ abstract class OneToOne extends Relation * @param string $relation 当前关联名 * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 - * @param string $class 数据集对象名 为空表示数组 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure) { if (1 == $this->eagerlyType) { // IN查询 - $this->eagerlyOne($result, $relation, $subRelation, $closure, $class); + $this->eagerlyOne($result, $relation, $subRelation, $closure); } else { // 模型关联组装 $this->match($this->model, $relation, $result);