From 92c961bb9e7427a51804c6bd67758614e5a70697 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 15 Jan 2018 16:09:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=BD=AF=E5=88=A0=E9=99=A4?= =?UTF-8?q?=20=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AEdeleteTime=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/traits/model/SoftDelete.php | 34 +++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/library/traits/model/SoftDelete.php b/library/traits/model/SoftDelete.php index e33402df..4c8a6b82 100644 --- a/library/traits/model/SoftDelete.php +++ b/library/traits/model/SoftDelete.php @@ -19,7 +19,10 @@ trait SoftDelete { $field = $this->getDeleteTimeField(); - return !empty($this->data[$field]); + if ($field && !empty($this->data[$field])) { + return true; + } + return false; } /** @@ -42,7 +45,11 @@ trait SoftDelete $model = new static(); $field = $model->getDeleteTimeField(true); - return $model->getQuery()->useSoftDelete($field, ['not null', '']); + if ($field) { + return $model->getQuery()->useSoftDelete($field, ['not null', '']); + } else { + return $model->getQuery(); + } } /** @@ -58,7 +65,7 @@ trait SoftDelete } $name = $this->getDeleteTimeField(); - if (!$force) { + if ($name && !$force) { // 软删除 $this->data[$name] = $this->autoWriteTimestamp($name); $result = $this->isUpdate()->save(); @@ -139,11 +146,15 @@ trait SoftDelete $name = $this->getDeleteTimeField(); - // 恢复删除 - return $this->getQuery() - ->useSoftDelete($name, ['not null', '']) - ->where($where) - ->update([$name => null]); + if ($name) { + // 恢复删除 + return $this->getQuery() + ->useSoftDelete($name, ['not null', '']) + ->where($where) + ->update([$name => null]); + } else { + return 0; + } } /** @@ -154,7 +165,8 @@ trait SoftDelete */ protected function base($query) { - return $query->useSoftDelete($this->getDeleteTimeField(true)); + $field = $this->getDeleteTimeField(true); + return $field ? $query->useSoftDelete($field) : $query; } /** @@ -169,6 +181,10 @@ trait SoftDelete $this->deleteTime : 'delete_time'; + if (false === $field) { + return false; + } + if (!strpos($field, '.')) { $field = '__TABLE__.' . $field; }