From d3be47e269be080c0f30ac74d322e46fabef653b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 1 Apr 2017 16:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 8 +++++++- library/think/model/relation/HasMany.php | 9 ++++++++- library/think/model/relation/HasManyThrough.php | 10 ++++------ library/think/model/relation/HasOne.php | 5 ++++- library/think/model/relation/MorphMany.php | 9 ++++++++- library/think/model/relation/MorphOne.php | 5 ++++- library/think/model/relation/MorphTo.php | 6 ++++-- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index cd5c04c0..9e443d54 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -50,7 +50,13 @@ class BelongsTo extends OneToOne if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find(); + $relationModel = $this->query + ->where($this->localKey, $this->parent->$foreignKey) + ->relation($subRelation) + ->find(); + $relationModel->setParent(clone $this->parent); + + return $relationModel; } /** diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 2f0d30b8..2c574869 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -47,7 +47,14 @@ class HasMany extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->relation($subRelation)->select(); + $list = $this->relation($subRelation)->select(); + $parent = clone $this->parent; + + foreach ($list as &$model) { + $model->setParent($parent); + } + + return $list; } /** diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index dd6f6a55..395e7f55 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -57,6 +57,7 @@ class HasManyThrough extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } + return $this->relation($subRelation)->select(); } @@ -96,8 +97,7 @@ class HasManyThrough extends Relation * @return void */ public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class) - { - } + {} /** * 预载入关联查询 返回模型对象 @@ -110,8 +110,7 @@ class HasManyThrough extends Relation * @return void */ public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class) - { - } + {} /** * 关联统计 @@ -121,8 +120,7 @@ class HasManyThrough extends Relation * @return integer */ public function relationCount($result, $closure) - { - } + {} /** * 执行基础查询(进执行一次) diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 27627627..991a594f 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -50,7 +50,10 @@ class HasOne extends OneToOne call_user_func_array($closure, [ & $this->query]); } // 判断关联类型执行查询 - return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); + $relationModel = $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); + $relationModel->setParent(clone $this->parent); + + return $relationModel; } /** diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 6b1ca7b3..0e4bf6f4 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -56,7 +56,14 @@ class MorphMany extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->relation($subRelation)->select(); + $list = $this->relation($subRelation)->select(); + $parent = clone $this->parent; + + foreach ($list as &$model) { + $model->setParent($parent); + } + + return $list; } /** diff --git a/library/think/model/relation/MorphOne.php b/library/think/model/relation/MorphOne.php index 9e231d68..b564fbc8 100644 --- a/library/think/model/relation/MorphOne.php +++ b/library/think/model/relation/MorphOne.php @@ -56,7 +56,10 @@ class MorphOne extends Relation if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->relation($subRelation)->find(); + $relationModel = $this->relation($subRelation)->find(); + $relationModel->setParent(clone $this->parent); + + return $relationModel; } /** diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index c6a2211b..70f1dc03 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -56,8 +56,10 @@ class MorphTo extends Relation // 多态模型 $model = $this->parseModel($this->parent->$morphType); // 主键数据 - $pk = $this->parent->$morphKey; - return (new $model)->relation($subRelation)->find($pk); + $pk = $this->parent->$morphKey; + $relationModel = (new $model)->relation($subRelation)->find($pk); + $relationModel->setParent(clone $this->parent); + return $relationModel; } /**