From 8ff20f98d05d790528c2de3ec78f27dfb6d080f8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 20 Jul 2017 17:52:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94=E7=9A=84?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/BelongsTo.php | 5 +++-- library/think/model/relation/HasOne.php | 16 ++++++++-------- library/think/model/relation/OneToOne.php | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index 905ea944..a32cc124 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -143,10 +143,11 @@ class BelongsTo extends OneToOne $relationModel->isUpdate(true); } - if ($relationModel && !empty($this->bindAttr)) { + if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); } + // 设置关联属性 $result->setRelation($attr, $relationModel); } @@ -175,7 +176,7 @@ class BelongsTo extends OneToOne $relationModel->setParent(clone $result); $relationModel->isUpdate(true); } - if ($relationModel && !empty($this->bindAttr)) { + if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); } diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index c500c02a..99327da2 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -140,10 +140,10 @@ class HasOne extends OneToOne $relationModel = $data[$result->$localKey]; $relationModel->setParent(clone $result); $relationModel->isUpdate(true); - if (!empty($this->bindAttr)) { - // 绑定关联属性 - $this->bindAttr($relationModel, $result, $this->bindAttr); - } + } + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); } // 设置关联属性 $result->setRelation($attr, $relationModel); @@ -173,10 +173,10 @@ class HasOne extends OneToOne $relationModel = $data[$result->$localKey]; $relationModel->setParent(clone $result); $relationModel->isUpdate(true); - if (!empty($this->bindAttr)) { - // 绑定关联属性 - $this->bindAttr($relationModel, $result, $this->bindAttr); - } + } + if (!empty($this->bindAttr)) { + // 绑定关联属性 + $this->bindAttr($relationModel, $result, $this->bindAttr); } $result->setRelation(Loader::parseName($relation), $relationModel); diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index ff066ed3..7877eaaa 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -276,7 +276,7 @@ abstract class OneToOne extends Relation if (isset($result->$key)) { throw new Exception('bind attr has exists:' . $key); } else { - $result->setAttr($key, $model->$attr); + $result->setAttr($key, $model ? $model->$attr : null); } } }