From 2a2b3882af6fb8c7a107cf4444529196869422ac Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 15 Sep 2017 17:32:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bappend=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=80=E5=AF=B9=E4=B8=80=E5=85=B3=E8=81=94?= =?UTF-8?q?=E7=9A=84bind=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 22 +++++++++++++++++++++- library/think/model/relation/BelongsTo.php | 11 ++++++----- library/think/model/relation/HasOne.php | 9 +++++---- library/think/model/relation/OneToOne.php | 10 ++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 4c3fb205..62f43f90 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -826,7 +826,27 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $relation = $this->getAttr($key); $item[$key] = $relation->append([$attr])->toArray(); } else { - $item[$name] = $this->getAttr($name); + $relation = Loader::parseName($name, 1, false); + if (method_exists($this, $relation)) { + $modelRelation = $this->$relation(); + $value = $this->getRelationData($modelRelation); + + if (method_exists($modelRelation, 'getBindAttr')) { + $bindAttr = $modelRelation->getBindAttr(); + foreach ($bindAttr as $key => $attr) { + $key = is_numeric($key) ? $attr : $key; + if (isset($this->data[$key])) { + throw new Exception('bind attr has exists:' . $key); + } else { + $item[$key] = $value ? $value->$attr : null; + } + } + } else { + $item[$name] = $value; + } + } else { + $item[$name] = $this->getAttr($name); + } } } } diff --git a/library/think/model/relation/BelongsTo.php b/library/think/model/relation/BelongsTo.php index dfd74a59..9b66e0c3 100644 --- a/library/think/model/relation/BelongsTo.php +++ b/library/think/model/relation/BelongsTo.php @@ -146,10 +146,10 @@ class BelongsTo extends OneToOne if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); + } else { + // 设置关联属性 + $result->setRelation($attr, $relationModel); } - - // 设置关联属性 - $result->setRelation($attr, $relationModel); } } } @@ -179,9 +179,10 @@ class BelongsTo extends OneToOne if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); + } else { + // 设置关联属性 + $result->setRelation(Loader::parseName($relation), $relationModel); } - // 设置关联属性 - $result->setRelation(Loader::parseName($relation), $relationModel); } /** diff --git a/library/think/model/relation/HasOne.php b/library/think/model/relation/HasOne.php index bb9f3286..3d4a3630 100644 --- a/library/think/model/relation/HasOne.php +++ b/library/think/model/relation/HasOne.php @@ -147,9 +147,10 @@ class HasOne extends OneToOne if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); + } else { + // 设置关联属性 + $result->setRelation($attr, $relationModel); } - // 设置关联属性 - $result->setRelation($attr, $relationModel); } } } @@ -180,9 +181,9 @@ class HasOne extends OneToOne if (!empty($this->bindAttr)) { // 绑定关联属性 $this->bindAttr($relationModel, $result, $this->bindAttr); + } else { + $result->setRelation(Loader::parseName($relation), $relationModel); } - - $result->setRelation(Loader::parseName($relation), $relationModel); } } diff --git a/library/think/model/relation/OneToOne.php b/library/think/model/relation/OneToOne.php index 9cf307a5..66d86a93 100644 --- a/library/think/model/relation/OneToOne.php +++ b/library/think/model/relation/OneToOne.php @@ -214,6 +214,16 @@ abstract class OneToOne extends Relation return $this; } + /** + * 获取绑定属性 + * @access public + * @return array + */ + public function getBindAttr() + { + return $this->bindAttr; + } + /** * 关联统计 * @access public