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 : []; }