diff --git a/library/think/Model.php b/library/think/Model.php index 5d112e44..a42eafaf 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -612,7 +612,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ protected function getRelationData(Relation $modelRelation) { - if ($this->parent && $modelRelation->getModel() == $this->parent) { + if ($this->parent && !$modelRelation->isSelfRelation() && get_class($modelRelation->getModel()) == get_class($this->parent)) { $value = $this->parent; } else { // 首先获取关联数据 diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index f4c5781e..b67bbf58 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -35,6 +35,8 @@ abstract class Relation protected $localKey; // 基础查询 protected $baseQuery; + // 是否为自关联 + protected $selfRelation; /** * 获取关联的所属模型 @@ -66,6 +68,28 @@ abstract class Relation return $this->query; } + /** + * 设置当前关联为自关联 + * @access public + * @param bool $self 是否自关联 + * @return $this + */ + public function selfRelation($self = true) + { + $this->selfRelation = $self; + return $this; + } + + /** + * 当前关联是否为自关联 + * @access public + * @return bool + */ + public function isSelfRelation() + { + return $this->selfRelation; + } + /** * 封装关联数据集 * @access public