From c0b858073824797100c317fc068b29b56ec0e9ee Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 14 Dec 2016 15:03:00 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0appendRelationA?= =?UTF-8?q?ttr=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?=E5=85=B3=E8=81=94=E6=A8=A1=E5=9E=8B=E7=9A=84=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E5=88=B0=E5=BD=93=E5=89=8D=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/library/think/Model.php b/library/think/Model.php index 1a9677d4..7e00d60a 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -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