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