mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
Query类的hidden和visible方法取消 改进Model类的hidden和visible方法支持操作关联属性
This commit is contained in:
@@ -20,6 +20,10 @@ use JsonSerializable;
|
|||||||
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||||
{
|
{
|
||||||
protected $items = [];
|
protected $items = [];
|
||||||
|
// 显示属性
|
||||||
|
protected $visible = [];
|
||||||
|
// 隐藏属性
|
||||||
|
protected $hidden = [];
|
||||||
|
|
||||||
public function __construct($items = [])
|
public function __construct($items = [])
|
||||||
{
|
{
|
||||||
@@ -40,11 +44,47 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
|||||||
return empty($this->items);
|
return empty($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置需要隐藏的输出属性
|
||||||
|
* @access public
|
||||||
|
* @param array $hidden 属性列表
|
||||||
|
* @param bool $override 是否覆盖
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function hidden($hidden = [], $override = false)
|
||||||
|
{
|
||||||
|
$this->hidden = [$hidden, $override];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置需要输出的属性
|
||||||
|
* @param array $visible
|
||||||
|
* @param bool $override 是否覆盖
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function visible($visible = [], $override = false)
|
||||||
|
{
|
||||||
|
$this->visible = [$visible, $override];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
return array_map(function ($value) {
|
$result = [];
|
||||||
return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value;
|
foreach ($this->items as $key => $item) {
|
||||||
}, $this->items);
|
if ($item instanceof Model || $item instanceof self) {
|
||||||
|
if (!empty($this->visible)) {
|
||||||
|
$item->visible($this->visible[0], $this->visible[1]);
|
||||||
|
} elseif (!empty($this->hidden)) {
|
||||||
|
$item->hidden($this->hidden[0], $this->hidden[1]);
|
||||||
|
}
|
||||||
|
$result[$key] = $item->toArray();
|
||||||
|
} else {
|
||||||
|
$result[$key] = $item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function all()
|
public function all()
|
||||||
|
|||||||
@@ -597,12 +597,30 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
$item = [];
|
$item = [];
|
||||||
|
$visible = [];
|
||||||
|
$hidden = [];
|
||||||
// 过滤属性
|
// 过滤属性
|
||||||
if (!empty($this->visible)) {
|
if (!empty($this->visible)) {
|
||||||
$data = array_intersect_key($this->data, array_flip($this->visible));
|
$array = [];
|
||||||
|
foreach ($this->visible as $key => $val) {
|
||||||
|
if (is_array($val)) {
|
||||||
|
$array[] = $key;
|
||||||
|
$visible[$key] = $val;
|
||||||
|
} else {
|
||||||
|
$array[] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$data = array_intersect_key($this->data, array_flip($array));
|
||||||
} elseif (!empty($this->hidden)) {
|
} elseif (!empty($this->hidden)) {
|
||||||
$data = array_diff_key($this->data, array_flip($this->hidden));
|
$array = [];
|
||||||
|
foreach ($this->hidden as $key => $val) {
|
||||||
|
if (is_array($val)) {
|
||||||
|
$hidden[$key] = $val;
|
||||||
|
} else {
|
||||||
|
$array[] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$data = array_diff_key($this->data, array_flip($array));
|
||||||
} else {
|
} else {
|
||||||
$data = $this->data;
|
$data = $this->data;
|
||||||
}
|
}
|
||||||
@@ -610,11 +628,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
if ($val instanceof Model || $val instanceof Collection) {
|
if ($val instanceof Model || $val instanceof Collection) {
|
||||||
// 关联模型对象
|
// 关联模型对象
|
||||||
|
if (isset($visible[$key])) {
|
||||||
|
$val->visible($visible[$key]);
|
||||||
|
} elseif (isset($hidden[$key])) {
|
||||||
|
$val->hidden($hidden[$key]);
|
||||||
|
}
|
||||||
$item[$key] = $val->toArray();
|
$item[$key] = $val->toArray();
|
||||||
} elseif (is_array($val) && reset($val) instanceof Model) {
|
} elseif (is_array($val) && reset($val) instanceof Model) {
|
||||||
// 关联模型数据集
|
// 关联模型数据集
|
||||||
$arr = [];
|
$arr = [];
|
||||||
foreach ($val as $k => $value) {
|
foreach ($val as $k => $value) {
|
||||||
|
if (isset($visible[$k])) {
|
||||||
|
$value->visible($visible[$k]);
|
||||||
|
} elseif (isset($hidden[$k])) {
|
||||||
|
$value->hidden($hidden[$k]);
|
||||||
|
}
|
||||||
$arr[$k] = $value->toArray();
|
$arr[$k] = $value->toArray();
|
||||||
}
|
}
|
||||||
$item[$key] = $arr;
|
$item[$key] = $arr;
|
||||||
|
|||||||
@@ -1503,30 +1503,6 @@ class Query
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置模型输出显示字段
|
|
||||||
* @access public
|
|
||||||
* @param mixed $visible
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function visible($visible)
|
|
||||||
{
|
|
||||||
$this->options['visible'] = $visible;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置模型输出隐藏字段
|
|
||||||
* @access public
|
|
||||||
* @param mixed $hidden
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function hidden($hidden)
|
|
||||||
{
|
|
||||||
$this->options['hidden'] = $hidden;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定数据表别名
|
* 指定数据表别名
|
||||||
* @access public
|
* @access public
|
||||||
@@ -2342,14 +2318,6 @@ class Query
|
|||||||
if (!empty($options['with_count'])) {
|
if (!empty($options['with_count'])) {
|
||||||
$model->relationCount($model, $options['with_count']);
|
$model->relationCount($model, $options['with_count']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 显式或隐藏属性
|
|
||||||
if (!empty($options['visible'])) {
|
|
||||||
$model->visible($options['visible']);
|
|
||||||
} elseif (!empty($options['hidden'])) {
|
|
||||||
$model->hidden($options['hidden']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$resultSet[$key] = $model;
|
$resultSet[$key] = $model;
|
||||||
}
|
}
|
||||||
if (!empty($options['with'])) {
|
if (!empty($options['with'])) {
|
||||||
@@ -2461,12 +2429,6 @@ class Query
|
|||||||
if (!empty($options['with_count'])) {
|
if (!empty($options['with_count'])) {
|
||||||
$data->relationCount($data, $options['with_count']);
|
$data->relationCount($data, $options['with_count']);
|
||||||
}
|
}
|
||||||
// 显式或隐藏属性
|
|
||||||
if (!empty($options['visible'])) {
|
|
||||||
$data->visible($options['visible']);
|
|
||||||
} elseif (!empty($options['hidden'])) {
|
|
||||||
$data->hidden($options['hidden']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} elseif (!empty($options['fail'])) {
|
} elseif (!empty($options['fail'])) {
|
||||||
$this->throwNotFound($options);
|
$this->throwNotFound($options);
|
||||||
|
|||||||
@@ -27,4 +27,5 @@ class Collection extends BaseCollection
|
|||||||
$item->eagerlyResultSet($this->items, $relation);
|
$item->eagerlyResultSet($this->items, $relation);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user