From 9657ee01b1dcb89984e1e5e5cf0ebe20b0bcfd9c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 22 Dec 2016 12:52:13 +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=9F=A5=E8=AF=A2=E6=94=AF=E6=8C=81=E5=85=B3=E8=81=94=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 10 ++++++---- library/think/db/Query.php | 12 +++++++----- library/think/model/relation/BelongsToMany.php | 16 +++++++++++++--- library/think/model/relation/HasMany.php | 15 +++++++++++++-- library/think/model/relation/MorphMany.php | 15 +++++++++++++-- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index a1523914..0848a108 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1261,9 +1261,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param array $resultSet 数据集 * @param string $relation 关联名 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return array */ - public function eagerlyResultSet(&$resultSet, $relation, $class = '') + public function eagerlyResultSet(&$resultSet, $relation, $class = '', $count = false) { $relations = is_string($relation) ? explode(',', $relation) : $relation; foreach ($relations as $key => $relation) { @@ -1277,7 +1278,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess list($relation, $subRelation) = explode('.', $relation); } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure, $class); + $this->$relation()->eagerlyResultSet($resultSet, $relation, $subRelation, $closure, $class, $count); } } @@ -1287,9 +1288,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @param Model $result 数据对象 * @param string $relation 关联名 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return Model */ - public function eagerlyResult(&$result, $relation, $class = '') + public function eagerlyResult(&$result, $relation, $class = '', $count = false) { $relations = is_string($relation) ? explode(',', $relation) : $relation; @@ -1304,7 +1306,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess list($relation, $subRelation) = explode('.', $relation); } $relation = Loader::parseName($relation, 1, false); - $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure, $class); + $this->$relation()->eagerlyResult($result, $relation, $subRelation, $closure, $class, $count); } } diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 116b7070..79a30eb7 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1620,10 +1620,11 @@ class Query /** * 设置关联查询JOIN预查询 * @access public - * @param string|array $with 关联方法名称 + * @param string|array $with 关联方法名称 + * @param bool $count 是否统计 * @return $this */ - public function with($with) + public function with($with, $count = false) { if (empty($with)) { return $this; @@ -1662,7 +1663,8 @@ class Query } } $this->via(); - $this->options['with'] = $with; + $this->options['with'] = $with; + $this->options['count'] = $count; return $this; } @@ -2017,7 +2019,7 @@ class Query } if (!empty($options['with'])) { // 预载入 - $result->eagerlyResultSet($resultSet, $options['with'], is_object($resultSet) ? get_class($resultSet) : ''); + $result->eagerlyResultSet($resultSet, $options['with'], is_object($resultSet) ? get_class($resultSet) : '', !empty($options['count']) ? true : false); } } // 模型数据集转换 @@ -2113,7 +2115,7 @@ class Query } // 预载入查询 if (!empty($options['with'])) { - $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : ''); + $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : '', !empty($options['count']) ? true : false); } // 关联统计 if (!empty($options['with_count'])) { diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index cf831f1b..a83ac53b 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -13,6 +13,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; +use think\Loader; use think\Model; use think\model\Pivot; use think\model\Relation; @@ -80,9 +81,10 @@ class BelongsToMany extends Relation * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class, $count) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -110,7 +112,10 @@ class BelongsToMany extends Relation if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } - + if ($count) { + // 关联统计 + $result->setAttr(Loader::parseName($relation) . '_count', count($data[$result->$pk])); + } $result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class)); } } @@ -124,9 +129,10 @@ class BelongsToMany extends Relation * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class, $count) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -141,6 +147,10 @@ class BelongsToMany extends Relation if (!isset($data[$pk])) { $data[$pk] = []; } + if ($count) { + // 关联统计 + $result->setAttr(Loader::parseName($relation) . '_count', count($data[$pk])); + } $result->setAttr($relation, $this->resultSetBuild($data[$pk], $class)); } } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 08e1e81e..ed5fb0be 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -13,6 +13,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; +use think\Loader; use think\Model; use think\model\Relation; @@ -54,9 +55,10 @@ class HasMany extends Relation * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class, $count) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -83,6 +85,10 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } + if ($count) { + // 关联统计 + $result->setAttr(Loader::parseName($relation) . '_count', count($data[$result->$localKey])); + } $result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class)); } } @@ -96,9 +102,10 @@ class HasMany extends Relation * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class, $count) { $localKey = $this->localKey; $foreignKey = $this->foreignKey; @@ -109,6 +116,10 @@ class HasMany extends Relation if (!isset($data[$result->$localKey])) { $data[$result->$localKey] = []; } + if ($count) { + // 关联统计 + $result->setAttr(Loader::parseName($relation) . '_count', count($data[$result->$localKey])); + } $result->setAttr($relation, $this->resultSetBuild($data[$result->$localKey], $class)); } } diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index b2e21013..9340bc42 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -13,6 +13,7 @@ namespace think\model\relation; use think\Db; use think\db\Query; +use think\Loader; use think\Model; use think\model\Relation; @@ -60,9 +61,10 @@ class MorphMany extends Relation * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return void */ - public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) + public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class, $count) { $morphType = $this->morphType; $morphKey = $this->morphKey; @@ -89,6 +91,10 @@ class MorphMany extends Relation if (!isset($data[$result->$pk])) { $data[$result->$pk] = []; } + if ($count) { + // 关联统计 + $result->setAttr(Loader::parseName($relation) . '_count', count($data[$result->$pk])); + } $result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class)); } } @@ -102,9 +108,10 @@ class MorphMany extends Relation * @param string $subRelation 子关联名 * @param \Closure $closure 闭包 * @param string $class 数据集对象名 为空表示数组 + * @param bool $count 是否统计 * @return void */ - public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) + public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class, $count) { $morphType = $this->morphType; $morphKey = $this->morphKey; @@ -112,6 +119,10 @@ class MorphMany extends Relation $pk = $result->getPk(); if (isset($result->$pk)) { $data = $this->eagerlyMorphToMany([$morphKey => $result->$pk, $morphType => $type], $relation, $subRelation, $closure); + if ($count) { + // 关联统计 + $result->setAttr(Loader::parseName($relation) . '_count', count($data[$result->$pk])); + } $result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class)); } }