diff --git a/library/think/Model.php b/library/think/Model.php index 00191954..d4003a2d 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -1282,14 +1282,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } foreach ($relations as $key => $relation) { - $closure = null; + $subRelation = ''; + $closure = null; if ($relation instanceof \Closure) { // 支持闭包查询过滤关联条件 $closure = $relation; $relation = $key; } + if (strpos($relation, '.')) { + list($relation, $subRelation) = explode('.', $relation); + } $method = Loader::parseName($relation, 1, false); - $this->data[$relation] = $this->$method()->getRelation($closure); + $this->data[$relation] = $this->$method()->getRelation($subRelation, $closure); } return $this; } diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index a96d9028..1f7454b0 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -38,17 +38,17 @@ class BelongsTo extends OneToOne /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { $foreignKey = $this->foreignKey; - $localKey = $this->localKey; if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->query->where($localKey, $this->parent->$foreignKey)->find(); + return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find(); } /** diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 25e1a078..9b63b011 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -46,10 +46,11 @@ class BelongsToMany extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { $foreignKey = $this->foreignKey; $localKey = $this->localKey; @@ -60,7 +61,7 @@ class BelongsToMany extends Relation // 关联查询 $pk = $this->parent->getPk(); $condition['pivot.' . $localKey] = $this->parent->$pk; - $result = $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition)->select(); + $result = $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition)->relation($subRelation)->select(); foreach ($result as $set) { $pivot = []; foreach ($set->getData() as $key => $val) { diff --git a/library/think/model/relation/HasMany.php b/library/think/model/relation/HasMany.php index 044cc88d..60bd1677 100644 --- a/library/think/model/relation/HasMany.php +++ b/library/think/model/relation/HasMany.php @@ -39,15 +39,16 @@ class HasMany extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->select(); + return $this->relation($subRelation)->select(); } /** diff --git a/library/think/model/relation/HasManyThrough.php b/library/think/model/relation/HasManyThrough.php index 983daf22..fee83b00 100644 --- a/library/think/model/relation/HasManyThrough.php +++ b/library/think/model/relation/HasManyThrough.php @@ -49,15 +49,16 @@ class HasManyThrough extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->select(); + return $this->relation($subRelation)->select(); } /** diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index 05230922..d2e1f9c4 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -38,10 +38,11 @@ class HasOne extends OneToOne /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($colsure = null) + public function getRelation($subRelation = '', $colsure = null) { // 执行关联定义方法 $localKey = $this->localKey; @@ -49,7 +50,7 @@ class HasOne extends OneToOne call_user_func_array($closure, [ & $this->query]); } // 判断关联类型执行查询 - return $this->query->where($this->foreignKey, $this->parent->$localKey)->find(); + return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); } /** diff --git a/library/think/model/relation/MorphMany.php b/library/think/model/relation/MorphMany.php index 40c1f791..c4c23df7 100644 --- a/library/think/model/relation/MorphMany.php +++ b/library/think/model/relation/MorphMany.php @@ -45,15 +45,16 @@ class MorphMany extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { if ($closure) { call_user_func_array($closure, [ & $this->query]); } - return $this->select(); + return $this->relation($subRelation)->select(); } /** diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 05017e8a..4014d7bd 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -39,10 +39,11 @@ class MorphTo extends Relation /** * 延迟获取关联数据 + * @param string $subRelation 子关联名 * @param \Closure $closure 闭包查询条件 * @access public */ - public function getRelation($closure = null) + public function getRelation($subRelation = '', $closure = null) { $morphKey = $this->morphKey; $morphType = $this->morphType; @@ -50,7 +51,7 @@ class MorphTo extends Relation $model = $this->parseModel($this->parent->$morphType); // 主键数据 $pk = $this->parent->$morphKey; - return (new $model)->find($pk); + return (new $model)->relation($subRelation)->find($pk); } /**