改进一对一关联

This commit is contained in:
thinkphp
2018-03-02 15:46:44 +08:00
parent 748c060f01
commit 95e9d848f7
2 changed files with 6 additions and 1 deletions

View File

@@ -52,6 +52,7 @@ class BelongsTo extends OneToOne
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [ & $this->query]);
} }
$relationModel = $this->query $relationModel = $this->query
->removeWhereField($this->localKey)
->where($this->localKey, $this->parent->$foreignKey) ->where($this->localKey, $this->parent->$foreignKey)
->relation($subRelation) ->relation($subRelation)
->find(); ->find();

View File

@@ -50,7 +50,11 @@ class HasOne extends OneToOne
call_user_func_array($closure, [ & $this->query]); call_user_func_array($closure, [ & $this->query]);
} }
// 判断关联类型执行查询 // 判断关联类型执行查询
$relationModel = $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find(); $relationModel = $this->query
->removeWhereField($this->foreignKey)
->where($this->foreignKey, $this->parent->$localKey)
->relation($subRelation)
->find();
if ($relationModel) { if ($relationModel) {
$relationModel->setParent(clone $this->parent); $relationModel->setParent(clone $this->parent);