mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
Model类增加hidden属性 支持输出数组和json的时候 隐藏个别字段
This commit is contained in:
@@ -40,6 +40,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
|
|
||||||
// 字段属性
|
// 字段属性
|
||||||
protected $field = [];
|
protected $field = [];
|
||||||
|
// 隐藏属性
|
||||||
|
protected $hidden = [];
|
||||||
// 数据信息
|
// 数据信息
|
||||||
protected $data = [];
|
protected $data = [];
|
||||||
// 缓存数据
|
// 缓存数据
|
||||||
@@ -64,8 +66,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $isUpdate = false;
|
protected $isUpdate = false;
|
||||||
// 当前执行的关联对象
|
// 当前执行的关联对象
|
||||||
protected $relation;
|
protected $relation;
|
||||||
// 当前模型的父模型
|
|
||||||
protected $parent;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化过的模型.
|
* 初始化过的模型.
|
||||||
@@ -144,6 +144,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return isset($this->data[$name]) ? $this->data[$name] : $this->data;
|
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
|
* @access public
|
||||||
@@ -153,6 +165,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
{
|
{
|
||||||
$item = [];
|
$item = [];
|
||||||
foreach ($this->data as $key => $val) {
|
foreach ($this->data as $key => $val) {
|
||||||
|
// 如果是隐藏属性不输出
|
||||||
|
if (in_array($key, $this->hidden)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($val instanceof Model) {
|
if ($val instanceof Model) {
|
||||||
// 关联模型对象
|
// 关联模型对象
|
||||||
$item[$key] = $val->toArray();
|
$item[$key] = $val->toArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user