From 0b22d6a970f8c1735b1041116940a07d25522c90 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 5 May 2016 11:58:34 +0800 Subject: [PATCH] =?UTF-8?q?Relation=E7=B1=BB=E5=A2=9E=E5=8A=A0delete?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=85=B3=E8=81=94=E6=A8=A1=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/model/Relation.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/library/think/model/Relation.php b/library/think/model/Relation.php index e7e15252..b5fde757 100644 --- a/library/think/model/Relation.php +++ b/library/think/model/Relation.php @@ -445,7 +445,30 @@ class Relation } /** - * 保存当前关联数据对象 + * 删除当前关联数据对象 + * @access public + * @param mixed $where 删除条件 一对多删除可以指定条件 多对多可以使用关联对象的主键删除 + * @return integer + */ + public function delete($where = []) + { + // 判断关联类型 + switch ($this->type) { + case self::HAS_ONE: + case self::BELONGS_TO: + case self::HAS_MANY: + // 保存关联表数据 + $where[$this->foreignKey] = $this->parent->{$this->localKey}; + $model = $this->model; + return $model::where($where)->delete(); + case self::BELONGS_TO_MANY: + // 删除关联表/中间表数据 + return $this->detach($where); + } + } + + /** + * 保存(新增)当前关联数据对象 * @access public * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键 * @param array $pivot 中间表额外数据