Model类增加hidden属性 支持输出数组和json的时候 隐藏个别字段

This commit is contained in:
thinkphp
2016-05-05 10:00:45 +08:00
parent 7a8bb3b54f
commit fd72a3006c

View File

@@ -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();