改进多态关联的关联数据添加

This commit is contained in:
thinkphp
2017-03-27 19:23:21 +08:00
parent af350238f1
commit 82e8da05e2

View File

@@ -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