增加关联统计功能

This commit is contained in:
thinkphp
2016-12-22 12:22:44 +08:00
parent 83c13a741c
commit cd57d97f36
5 changed files with 108 additions and 1 deletions

View File

@@ -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);