From fd72a3006c412f941f78afd6f67970ec4010fb73 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 5 May 2016 10:00:45 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0hidden=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=20=E6=94=AF=E6=8C=81=E8=BE=93=E5=87=BA=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=92=8Cjson=E7=9A=84=E6=97=B6=E5=80=99=20=E9=9A=90?= =?UTF-8?q?=E8=97=8F=E4=B8=AA=E5=88=AB=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 46df0a68..a7547429 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -40,6 +40,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 字段属性 protected $field = []; + // 隐藏属性 + protected $hidden = []; // 数据信息 protected $data = []; // 缓存数据 @@ -64,8 +66,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $isUpdate = false; // 当前执行的关联对象 protected $relation; - // 当前模型的父模型 - protected $parent; /** * 初始化过的模型. @@ -144,6 +144,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return isset($this->data[$name]) ? $this->data[$name] : $this->data; } + /** + * 设置需要隐藏的属性 + * @access public + * @param array $hidden 属性列表 + * @return $this + */ + public function hidden($hidden = []) + { + $this->hidden = $hidden; + return $this; + } + /** * 转换当前模型对象为数组 * @access public @@ -153,6 +165,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess { $item = []; foreach ($this->data as $key => $val) { + // 如果是隐藏属性不输出 + if (in_array($key, $this->hidden)) { + continue; + } + if ($val instanceof Model) { // 关联模型对象 $item[$key] = $val->toArray();