Model类增加appendRelationAttr方法用于追加关联模型的属性到当前模型

This commit is contained in:
thinkphp
2016-12-14 15:03:00 +08:00
parent 5ba052d94f
commit c0b8580738

View File

@@ -500,6 +500,28 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return $this;
}
/**
* 设置附加关联对象的属性
* @access public
* @param string $relation 关联方法
* @param string|array $append 追加属性名
* @return $this
*/
public function appendRelationAttr($relation, $append)
{
if (is_string($append)) {
$append = explode(',', $append);
}
$model = $this->getAttr($relation);
if ($model instanceof Model) {
foreach ($append as $key => $attr) {
$key = is_numeric($key) ? $attr : $key;
$this->setAttr($key, $model->$attr);
}
}
return $this;
}
/**
* 设置需要隐藏的输出属性
* @access public