mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 15:42:48 +08:00
Model类append方法支持追加关联对象属性
This commit is contained in:
@@ -24,6 +24,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
|||||||
protected $visible = [];
|
protected $visible = [];
|
||||||
// 隐藏属性
|
// 隐藏属性
|
||||||
protected $hidden = [];
|
protected $hidden = [];
|
||||||
|
// 追加属性
|
||||||
|
protected $append = [];
|
||||||
|
|
||||||
public function __construct($items = [])
|
public function __construct($items = [])
|
||||||
{
|
{
|
||||||
@@ -69,6 +71,19 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置需要追加的输出属性
|
||||||
|
* @access public
|
||||||
|
* @param array $append 属性列表
|
||||||
|
* @param bool $override 是否覆盖
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function append($append = [], $override = false)
|
||||||
|
{
|
||||||
|
$this->append = [$append, $override];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
$result = [];
|
$result = [];
|
||||||
@@ -79,6 +94,9 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
|||||||
} elseif (!empty($this->hidden)) {
|
} elseif (!empty($this->hidden)) {
|
||||||
$item->hidden($this->hidden[0], $this->hidden[1]);
|
$item->hidden($this->hidden[0], $this->hidden[1]);
|
||||||
}
|
}
|
||||||
|
if (!empty($this->append)) {
|
||||||
|
$item->append($this->append[0], $this->append[1]);
|
||||||
|
}
|
||||||
$result[$key] = $item->toArray();
|
$result[$key] = $item->toArray();
|
||||||
} else {
|
} else {
|
||||||
$result[$key] = $item;
|
$result[$key] = $item;
|
||||||
|
|||||||
@@ -596,12 +596,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
$item = [];
|
$item = [];
|
||||||
$visible = [];
|
|
||||||
$hidden = [];
|
|
||||||
// 过滤属性
|
// 过滤属性
|
||||||
if (!empty($this->visible)) {
|
if (!empty($this->visible)) {
|
||||||
$array = [];
|
$visible = [];
|
||||||
|
$array = [];
|
||||||
foreach ($this->visible as $key => $val) {
|
foreach ($this->visible as $key => $val) {
|
||||||
if (is_array($val)) {
|
if (is_array($val)) {
|
||||||
$array[] = $key;
|
$array[] = $key;
|
||||||
@@ -612,7 +611,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
$data = array_intersect_key($this->data, array_flip($array));
|
$data = array_intersect_key($this->data, array_flip($array));
|
||||||
} elseif (!empty($this->hidden)) {
|
} elseif (!empty($this->hidden)) {
|
||||||
$array = [];
|
$hidden = [];
|
||||||
|
$array = [];
|
||||||
foreach ($this->hidden as $key => $val) {
|
foreach ($this->hidden as $key => $val) {
|
||||||
if (is_array($val)) {
|
if (is_array($val)) {
|
||||||
$hidden[$key] = $val;
|
$hidden[$key] = $val;
|
||||||
@@ -653,8 +653,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
// 追加属性(必须定义获取器)
|
// 追加属性(必须定义获取器)
|
||||||
if (!empty($this->append)) {
|
if (!empty($this->append)) {
|
||||||
foreach ($this->append as $name) {
|
foreach ($this->append as $key => $name) {
|
||||||
$item[$name] = $this->getAttr($name);
|
if (is_array($name)) {
|
||||||
|
// 追加关联对象属性
|
||||||
|
$relation = $this->getAttr($key);
|
||||||
|
$item[$key] = $relation->append($name)->toArray();
|
||||||
|
} else {
|
||||||
|
$item[$name] = $this->getAttr($name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return !empty($item) ? $item : [];
|
return !empty($item) ? $item : [];
|
||||||
|
|||||||
Reference in New Issue
Block a user