Query类的hidden和visible方法取消 改进Model类的hidden和visible方法支持操作关联属性

This commit is contained in:
thinkphp
2017-01-22 23:21:46 +08:00
parent 44654c76ef
commit 7348729b40
4 changed files with 78 additions and 47 deletions

View File

@@ -20,6 +20,10 @@ use JsonSerializable;
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
{
protected $items = [];
// 显示属性
protected $visible = [];
// 隐藏属性
protected $hidden = [];
public function __construct($items = [])
{
@@ -40,11 +44,47 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
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()
{
return array_map(function ($value) {
return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value;
}, $this->items);
$result = [];
foreach ($this->items as $key => $item) {
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()
@@ -225,7 +265,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
$result = [];
foreach ($this->items as $row) {
$key = $value = null;
$key = $value = null;
$keySet = $valueSet = false;
if (null !== $index_key && array_key_exists($index_key, $row)) {
$keySet = true;

View File

@@ -596,13 +596,31 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
public function toArray()
{
$item = [];
//过滤属性
$item = [];
$visible = [];
$hidden = [];
// 过滤属性
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)) {
$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 {
$data = $this->data;
}
@@ -610,11 +628,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
foreach ($data as $key => $val) {
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();
} elseif (is_array($val) && reset($val) instanceof Model) {
// 关联模型数据集
$arr = [];
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();
}
$item[$key] = $arr;

View File

@@ -1503,30 +1503,6 @@ class Query
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
@@ -2342,14 +2318,6 @@ class Query
if (!empty($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;
}
if (!empty($options['with'])) {
@@ -2461,12 +2429,6 @@ class Query
if (!empty($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'])) {
$this->throwNotFound($options);

View File

@@ -27,4 +27,5 @@ class Collection extends BaseCollection
$item->eagerlyResultSet($this->items, $relation);
return $this;
}
}