From 96f45258cd865f48bcb0aa95cd233e4fbc64f7d7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 14 May 2017 11:27:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=85=B3=E8=81=94=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E7=9A=84=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 5e8a8a36..28d37cfe 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -658,14 +658,22 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (is_string($append)) { $append = explode(',', $append); } - $model = $this->getAttr($relation); + + $relation = Loader::parseName($relation, 1, false); + + if (isset($this->relation[$relation])) { + $model = $this->relation[$relation]; + } else { + $model = $this->getAttr($relation); + } + if ($model instanceof Model) { foreach ($append as $key => $attr) { $key = is_numeric($key) ? $attr : $key; - if ($this->__isset($key)) { + if (isset($this->data[$key])) { throw new Exception('bind attr has exists:' . $key); } else { - $this->setAttr($key, $model->$attr); + $this->data[$key] = $model->$attr; } } } @@ -778,7 +786,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 关联模型数据集 $arr = []; foreach ($val as $k => $value) { - $arr[$k] = $this->subToArray($value, $visible, $hidden, $k); + $arr[$k] = $this->subToArray($value, $visible, $hidden, $key); } $item[$key] = $arr; } else {