关联定义增加selfRelation方法用于设置是否自关联

This commit is contained in:
thinkphp
2018-01-05 17:55:29 +08:00
parent ea2ce8f8de
commit df1c05a988
2 changed files with 25 additions and 1 deletions

View File

@@ -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 {
// 首先获取关联数据

View File

@@ -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