From cd57d97f36767019902e9ce525b649c78080db4d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 22 Dec 2016 12:22:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=B3=E8=81=94=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 23 +++++++++++++++++++ library/think/db/Query.php | 22 +++++++++++++++++- .../think/model/relation/BelongsToMany.php | 20 ++++++++++++++++ library/think/model/relation/HasMany.php | 21 +++++++++++++++++ library/think/model/relation/MorphMany.php | 23 +++++++++++++++++++ 5 files changed, 108 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 52f46516..a1523914 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1308,6 +1308,29 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param string|array $relation 关联名 + * @return void + */ + public function relationCount(&$result, $relation) + { + $relations = is_string($relation) ? explode(',', $relation) : $relation; + + foreach ($relations as $key => $relation) { + $closure = false; + if ($relation instanceof \Closure) { + $closure = $relation; + $relation = $key; + } + $relation = Loader::parseName($relation, 1, false); + $count = $this->$relation()->relationCount($result, $closure); + $result->setAttr(Loader::parseName($relation) . '_count', $count); + } + } + /** * HAS ONE 关联定义 * @access public diff --git a/library/think/db/Query.php b/library/think/db/Query.php index ace9a90a..116b7070 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1666,6 +1666,18 @@ class Query return $this; } + /** + * 关联统计 + * @access public + * @param string|array $relation 关联方法名 + * @return $this + */ + public function withCount($relation) + { + $this->options['with_count'] = $relation; + return $this; + } + /** * 关联预加载中 获取关联指定字段值 * example: @@ -1997,6 +2009,10 @@ class Query if (!empty($options['relation'])) { $result->relationQuery($options['relation']); } + // 关联统计 + if (!empty($options['with_count'])) { + $result->relationCount($result, $options['with_count']); + } $resultSet[$key] = $result; } if (!empty($options['with'])) { @@ -2095,10 +2111,14 @@ class Query if (!empty($options['relation'])) { $data->relationQuery($options['relation']); } + // 预载入查询 if (!empty($options['with'])) { - // 预载入 $data->eagerlyResult($data, $options['with'], is_object($result) ? get_class($result) : ''); } + // 关联统计 + if (!empty($options['with_count'])) { + $data->relationCount($data, $options['with_count']); + } } } elseif (!empty($options['fail'])) { $this->throwNotFound($options); diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index b5d21680..cf831f1b 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -145,6 +145,26 @@ class BelongsToMany extends Relation } } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @return integer + */ + public function relationCount($result, $closure) + { + $localKey = $this->localKey; + $foreignKey = $this->foreignKey; + $pk = $result->getPk(); + $count = 0; + if (isset($result->$pk)) { + $pk = $result->$pk; + $count = $this->belongsToManyQuery($this->middle, $foreignKey, $localKey, ['pivot.' . $localKey => $pk])->count(); + } + return $count; + } + /** * 多对多 关联模型预查询 * @access public diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 416c5a5b..08e1e81e 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -113,6 +113,27 @@ class HasMany extends Relation } } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @return integer + */ + public function relationCount($result, $closure) + { + $localKey = $this->localKey; + $foreignKey = $this->foreignKey; + $count = 0; + if (isset($result->$localKey)) { + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } + $count = $this->query->where([$foreignKey => $result->$localKey])->count(); + } + return $count; + } + /** * 一对多 关联模型预查询 * @access public diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 5a2e2e9b..b2e21013 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -116,6 +116,29 @@ class MorphMany extends Relation } } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @return integer + */ + public function relationCount($result, $closure) + { + $morphType = $this->morphType; + $morphKey = $this->morphKey; + $type = $this->type; + $pk = $result->getPk(); + $count = 0; + if (isset($result->$pk)) { + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } + $count = $this->query->where([$morphKey => $result->$pk, $morphType => $type])->count(); + } + return $count; + } + /** * 多态一对多 关联模型预查询 * @access public