完善关联统计 避免一对一关联报错

This commit is contained in:
thinkphp
2016-12-22 13:52:41 +08:00
parent bde2607442
commit 91e089b7e7
6 changed files with 54 additions and 43 deletions

View File

@@ -58,10 +58,8 @@ class HasMany extends Relation
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$range = [];
$localKey = $this->localKey;
$range = [];
foreach ($resultSet as $result) {
// 获取关联外键列表
if (isset($result->$localKey)) {
@@ -70,9 +68,9 @@ class HasMany extends Relation
}
if (!empty($range)) {
$this->where[$foreignKey] = ['in', $range];
$data = $this->eagerlyOneToMany($this, [
$foreignKey => [
$this->where[$this->foreignKey] = ['in', $range];
$data = $this->eagerlyOneToMany($this, [
$this->foreignKey => [
'in',
$range,
],
@@ -100,11 +98,10 @@ class HasMany extends Relation
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$localKey = $this->localKey;
if (isset($result->$localKey)) {
$data = $this->eagerlyOneToMany($this, [$foreignKey => $result->$localKey], $relation, $subRelation, $closure);
$data = $this->eagerlyOneToMany($this, [$this->foreignKey => $result->$localKey], $relation, $subRelation, $closure);
// 关联数据封装
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
@@ -122,14 +119,13 @@ class HasMany extends Relation
*/
public function relationCount($result, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$count = 0;
$localKey = $this->localKey;
$count = 0;
if (isset($result->$localKey)) {
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
$count = $this->query->where([$foreignKey => $result->$localKey])->count();
$count = $this->query->where([$this->foreignKey => $result->$localKey])->count();
}
return $count;
}