From b69e64a5213dd901f1d1dbd36b7d97c54960edd1 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 24 Nov 2017 15:26:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E8=81=94=E8=87=AA=E5=8A=A8=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=A2=9E=E5=8A=A0=E4=B8=80=E5=AF=B9=E5=A4=9A=E5=85=B3?= =?UTF-8?q?=E8=81=94=E5=88=A0=E9=99=A4=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/traits/model/SoftDelete.php | 33 +++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/library/traits/model/SoftDelete.php b/library/traits/model/SoftDelete.php index 753b2c8b..e33402df 100644 --- a/library/traits/model/SoftDelete.php +++ b/library/traits/model/SoftDelete.php @@ -29,7 +29,7 @@ trait SoftDelete */ public static function withTrashed() { - return (new static)->getQuery(); + return (new static )->getQuery(); } /** @@ -53,7 +53,9 @@ trait SoftDelete */ public function delete($force = false) { - if (false === $this->trigger('before_delete', $this)) return false; + if (false === $this->trigger('before_delete', $this)) { + return false; + } $name = $this->getDeleteTimeField(); if (!$force) { @@ -68,10 +70,15 @@ trait SoftDelete // 关联删除 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(); + $name = is_numeric($key) ? $name : $key; + $result = $this->getRelation($name); + if ($result instanceof Model) { + $result->delete(); + } elseif ($result instanceof Collection || is_array($result)) { + foreach ($result as $model) { + $model->delete(); + } + } } } @@ -92,7 +99,9 @@ trait SoftDelete */ public static function destroy($data, $force = false) { - if (is_null($data)) return 0; + if (is_null($data)) { + return 0; + } // 包含软删除数据 $query = self::withTrashed(); @@ -100,7 +109,7 @@ trait SoftDelete $query->where($data); $data = null; } elseif ($data instanceof \Closure) { - call_user_func_array($data, [&$query]); + call_user_func_array($data, [ & $query]); $data = null; } @@ -157,10 +166,12 @@ trait SoftDelete protected function getDeleteTimeField($read = false) { $field = property_exists($this, 'deleteTime') && isset($this->deleteTime) ? - $this->deleteTime : - 'delete_time'; + $this->deleteTime : + 'delete_time'; - if (!strpos($field, '.')) $field = '__TABLE__.' . $field; + if (!strpos($field, '.')) { + $field = '__TABLE__.' . $field; + } if (!$read && strpos($field, '.')) { $array = explode('.', $field);