diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index cf831f1b..93df01cf 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -128,14 +128,11 @@ class BelongsToMany extends Relation */ public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) { - $localKey = $this->localKey; - $foreignKey = $this->foreignKey; - $pk = $result->getPk(); if (isset($result->$pk)) { $pk = $result->$pk; // 查询管理数据 - $data = $this->eagerlyManyToMany(['pivot.' . $localKey => $pk], $relation, $subRelation); + $data = $this->eagerlyManyToMany(['pivot.' . $this->localKey => $pk], $relation, $subRelation); // 关联数据封装 if (!isset($data[$pk])) { @@ -154,13 +151,11 @@ class BelongsToMany extends Relation */ public function relationCount($result, $closure) { - $localKey = $this->localKey; - $foreignKey = $this->foreignKey; - $pk = $result->getPk(); - $count = 0; + $pk = $result->getPk(); + $count = 0; if (isset($result->$pk)) { $pk = $result->$pk; - $count = $this->belongsToManyQuery($this->middle, $foreignKey, $localKey, ['pivot.' . $localKey => $pk])->count(); + $count = $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, ['pivot.' . $this->localKey => $pk])->count(); } return $count; } @@ -175,10 +170,8 @@ class BelongsToMany extends Relation */ protected function eagerlyManyToMany($where, $relation, $subRelation = '') { - $foreignKey = $this->foreignKey; - $localKey = $this->localKey; // 预载入关联查询 支持嵌套预载入 - $list = $this->belongsToManyQuery($this->middle, $foreignKey, $localKey, $where)->with($subRelation)->select(); + $list = $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, $where)->with($subRelation)->select(); // 组装模型数据 $data = []; @@ -193,8 +186,8 @@ class BelongsToMany extends Relation } } } - $set->pivot = new Pivot($pivot, $this->middle); - $data[$pivot[$localKey]][] = $set; + $set->pivot = new Pivot($pivot, $this->middle); + $data[$pivot[$this->localKey]][] = $set; } return $data; } diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 08e1e81e..f6455d6b 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -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; } diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index 56b90f4c..79a822f3 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -67,8 +67,7 @@ class HasManyThrough extends Relation * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) - { - } + {} /** * 预载入关联查询 返回模型对象 @@ -81,8 +80,17 @@ class HasManyThrough extends Relation * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) - { - } + {} + + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @return integer + */ + public function relationCount($result, $closure) + {} /** * 执行基础查询(进执行一次) diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index b2e21013..44c344cd 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -106,12 +106,9 @@ class MorphMany extends Relation */ public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) { - $morphType = $this->morphType; - $morphKey = $this->morphKey; - $type = $this->type; - $pk = $result->getPk(); + $pk = $result->getPk(); if (isset($result->$pk)) { - $data = $this->eagerlyMorphToMany([$morphKey => $result->$pk, $morphType => $type], $relation, $subRelation, $closure); + $data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure); $result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class)); } } @@ -125,16 +122,13 @@ class MorphMany extends Relation */ public function relationCount($result, $closure) { - $morphType = $this->morphType; - $morphKey = $this->morphKey; - $type = $this->type; - $pk = $result->getPk(); - $count = 0; + $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(); + $count = $this->query->where([$this->morphKey => $result->$pk, $this->morphType => $this->type])->count(); } return $count; } diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 6de1d4f4..fa691489 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -136,6 +136,16 @@ class MorphTo extends Relation $this->eagerlyMorphToOne($model, $relation, $result, $subRelation); } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @return integer + */ + public function relationCount($result, $closure) + {} + /** * 多态MorphTo 关联模型预查询 * @access public diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 0165314f..a5a5de9d 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -179,6 +179,16 @@ abstract class OneToOne extends Relation return $this; } + /** + * 关联统计 + * @access public + * @param Model $result 数据对象 + * @param \Closure $closure 闭包 + * @return integer + */ + public function relationCount($result, $closure) + {} + /** * 一对一 关联模型预查询拼装 * @access public