From 55bcfca1bd86e54360fa5163ba792603bacfb4d8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 19 Jan 2017 13:50:57 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0=E4=B8=80?= =?UTF-8?q?=E5=AF=B9=E4=B8=80=E5=85=B3=E8=81=94=E8=87=AA=E5=8A=A8=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/library/think/Model.php b/library/think/Model.php index 8af809c0..21fc10f0 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -831,7 +831,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($val instanceof Model) { $val->save(); } else { - $this->getAttr($name)->save($val); + $model = $this->getAttr($name); + if ($model instanceof Model) { + $model->save($val); + } } } } @@ -987,8 +990,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } + // 删除当前模型数据 $result = $this->db()->delete($this->data); + // 关联删除 + if (!empty($this->relationWrite)) { + foreach ($this->relationWrite as $key => $name) { + $name = is_numeric($key) ? $name : $key; + $model = $this->getAttr($name); + if ($model instanceof Model) { + $model->delete(); + } + } + } + $this->trigger('after_delete', $this); return $result; }