From 82e8da05e2c2d911ec902a4e51069663e38cc024 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 27 Mar 2017 19:23:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=A4=9A=E6=80=81=E5=85=B3?= =?UTF-8?q?=E8=81=94=E7=9A=84=E5=85=B3=E8=81=94=E6=95=B0=E6=8D=AE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/relation/MorphTo.php | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/library/think/model/relation/MorphTo.php b/library/think/model/relation/MorphTo.php index 5fafd47a..d406e8f8 100644 --- a/library/think/model/relation/MorphTo.php +++ b/library/think/model/relation/MorphTo.php @@ -222,6 +222,41 @@ class MorphTo extends Relation $result->setAttr(Loader::parseName($relation), $data ?: null); } + /** + * 添加关联数据 + * @access public + * @param Model $model 关联模型对象 + * @return Model + */ + public function associate($model) + { + $morphKey = $this->morphKey; + $morphType = $this->morphType; + $pk = $model->getPk(); + + $this->parent->setAttr($morphKey, $model->$pk); + $this->parent->setAttr($morphType, get_class($model)); + $this->parent->save(); + return $this; + } + + /** + * 添加关联数据 + * @access public + * @param Model $model 关联模型对象 + * @return Model + */ + public function dissociate() + { + $morphKey = $this->morphKey; + $morphType = $this->morphType; + + $this->parent->setAttr($morphKey, null); + $this->parent->setAttr($morphType, null); + $this->parent->save(); + return $this; + } + /** * 执行基础查询(进执行一次) * @access protected