diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 6daa6f67..cd7a093b 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1675,11 +1675,29 @@ class Query * 关联统计 * @access public * @param string|array $relation 关联方法名 + * @param bool $subQuery 是否使用子查询 * @return $this */ - public function withCount($relation) + public function withCount($relation, $subQuery = false) { - $this->options['with_count'] = $relation; + if (!$subQuery) { + $this->options['with_count'] = $relation; + } else { + $relations = is_string($relation) ? explode(',', $relation) : $relation; + if (!isset($this->options['field'])) { + $this->field('*'); + } + foreach ($relations as $key => $relation) { + $closure = false; + if ($relation instanceof \Closure) { + $closure = $relation; + $relation = $key; + } + $relation = Loader::parseName($relation, 1, false); + $count = '(' . (new $this->model)->$relation()->getRelationCountQuery($closure) . ')'; + $this->field([$count => Loader::parseName($relation) . '_count']); + } + } return $this; } diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 37d07eec..e1849d89 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -161,6 +161,17 @@ class BelongsToMany extends Relation return $count; } + /** + * 获取关联统计子查询 + * @access public + * @param \Closure $closure 闭包 + * @return string + */ + public function getRelationCountQuery($closure) + { + return $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, ['pivot.' . $this->localKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()]])->fetchSql()->count(); + } + /** * 多对多 关联模型预查询 * @access public diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index f6455d6b..e5690137 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -130,6 +130,21 @@ class HasMany extends Relation return $count; } + /** + * 创建关联统计子查询 + * @access public + * @param \Closure $closure 闭包 + * @return string + */ + public function getRelationCountQuery($closure) + { + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } + + return $this->query->where([$this->foreignKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()]])->fetchSql()->count(); + } + /** * 一对多 关联模型预查询 * @access public diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 44c344cd..d76052ed 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -133,6 +133,21 @@ class MorphMany extends Relation return $count; } + /** + * 获取关联统计子查询 + * @access public + * @param \Closure $closure 闭包 + * @return string + */ + public function getRelationCountQuery($closure) + { + if ($closure) { + call_user_func_array($closure, [ & $this->query]); + } + + return $this->query->where([$this->morphKey => ['exp', '=' . $this->parent->getTable() . '.' . $this->parent->getPk()], $this->morphType => $this->type])->fetchSql()->count(); + } + /** * 多态一对多 关联模型预查询 * @access public