From ea1b88af50829897c2f0fe5616f80e0ca3233e61 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 9 Jul 2016 22:16:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=AF=95Model=E7=B1=BBappend=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E7=9A=84=E8=BF=BD=E5=8A=A0=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 7e7fdcb9..b3fc26ad 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -478,11 +478,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function toArray($allow = []) { $item = []; - if (!empty($this->append)) { - foreach ($this->append as $name) { - $item[$name] = $this->getAttr($name); - } - } if (empty($allow)) { $allow = array_keys($this->data); } @@ -508,6 +503,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $item[$key] = $this->getAttr($key); } } + // 追加属性(必须定义获取器) + if (!empty($this->append)) { + foreach ($this->append as $name) { + $item[$name] = $this->getAttr($name); + } + } return !empty($item) ? $item : []; }