增加关联统计功能

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

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