From 323d3e94165fa0113bac2a4608f6a8f5acc03005 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 May 2016 11:36:25 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0append=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E5=92=8Cappend=E6=96=B9=E6=B3=95=20=E7=94=A8=E4=BA=8E?= =?UTF-8?q?toarray=E5=92=8Ctojson=E6=96=B9=E6=B3=95=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=20=E9=99=84=E5=8A=A0=E9=A2=9D?= =?UTF-8?q?=E5=A4=96=E7=9A=84=E5=B1=9E=E6=80=A7=EF=BC=8C=E8=AF=A5=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E7=9A=84=E5=80=BC=E5=BF=85=E9=A1=BB=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=99=A8=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/library/think/Model.php b/library/think/Model.php index 184178a1..f765c0c8 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -48,6 +48,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $field = []; // 隐藏属性 protected $hidden = []; + // 追加属性 + protected $append = []; // 数据信息 protected $data = []; // 记录改变字段 @@ -174,6 +176,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return array_key_exists($name, $this->data) ? $this->data[$name] : $this->data; } + /** + * 设置需要追加的输出属性 + * @access public + * @param array $append 属性列表 + * @return $this + */ + public function append($append = []) + { + $this->append = $append; + return $this; + } + /** * 设置需要隐藏的属性 * @access public @@ -194,6 +208,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function toArray() { $item = []; + if (!empty($this->append)) { + foreach ($this->append as $name) { + $item[$name] = $this->__get($name); + } + } foreach ($this->data as $key => $val) { // 如果是隐藏属性不输出 if (in_array($key, $this->hidden)) {