From 9d1b13b66672c7d275196bf50e10a07739a16b3d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 Jan 2017 08:42:20 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BBappend=E6=96=B9=E6=B3=95=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=BF=BD=E5=8A=A0=E5=85=B3=E8=81=94=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 18 ++++++++++++++++++ library/think/Model.php | 20 +++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index b450c48c..20bbe6a2 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -24,6 +24,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria protected $visible = []; // 隐藏属性 protected $hidden = []; + // 追加属性 + protected $append = []; public function __construct($items = []) { @@ -69,6 +71,19 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return $this; } + /** + * 设置需要追加的输出属性 + * @access public + * @param array $append 属性列表 + * @param bool $override 是否覆盖 + * @return $this + */ + public function append($append = [], $override = false) + { + $this->append = [$append, $override]; + return $this; + } + public function toArray() { $result = []; @@ -79,6 +94,9 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria } elseif (!empty($this->hidden)) { $item->hidden($this->hidden[0], $this->hidden[1]); } + if (!empty($this->append)) { + $item->append($this->append[0], $this->append[1]); + } $result[$key] = $item->toArray(); } else { $result[$key] = $item; diff --git a/library/think/Model.php b/library/think/Model.php index 772e6f12..3945269f 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -596,12 +596,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function toArray() { - $item = []; - $visible = []; - $hidden = []; + $item = []; // 过滤属性 if (!empty($this->visible)) { - $array = []; + $visible = []; + $array = []; foreach ($this->visible as $key => $val) { if (is_array($val)) { $array[] = $key; @@ -612,7 +611,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } $data = array_intersect_key($this->data, array_flip($array)); } elseif (!empty($this->hidden)) { - $array = []; + $hidden = []; + $array = []; foreach ($this->hidden as $key => $val) { if (is_array($val)) { $hidden[$key] = $val; @@ -653,8 +653,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 追加属性(必须定义获取器) if (!empty($this->append)) { - foreach ($this->append as $name) { - $item[$name] = $this->getAttr($name); + foreach ($this->append as $key => $name) { + if (is_array($name)) { + // 追加关联对象属性 + $relation = $this->getAttr($key); + $item[$key] = $relation->append($name)->toArray(); + } else { + $item[$name] = $this->getAttr($name); + } } } return !empty($item) ? $item : [];