From e105bef4601c57f9a20a6174793bd23116cc7e2e Mon Sep 17 00:00:00 2001 From: JaguarJack <82664165@qq.com> Date: Mon, 24 May 2021 21:08:01 +0800 Subject: [PATCH 1/3] update cms category event --- catch/cms/model/events/CategoryEvent.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/catch/cms/model/events/CategoryEvent.php b/catch/cms/model/events/CategoryEvent.php index dd4e591..0bd931b 100644 --- a/catch/cms/model/events/CategoryEvent.php +++ b/catch/cms/model/events/CategoryEvent.php @@ -13,6 +13,7 @@ namespace catchAdmin\cms\model\events; +use catcher\exceptions\CatchException; use catcher\exceptions\FailedException; use catcher\Utils; @@ -24,10 +25,15 @@ trait CategoryEvent * @time 2021年03月03日 * @param \think\Model $category * @return void + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\db\exception\DataNotFoundException */ public static function onBeforeInsert(\think\Model $category): void { - + if (self::where('name', $category->getData('name'))->find()) { + throw new FailedException('分类名称重复,请重新填写'); + } } /** @@ -36,10 +42,17 @@ trait CategoryEvent * @time 2021年03月03日 * @param \think\Model $category * @return mixed|void + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @throws \think\db\exception\DataNotFoundException */ public static function onBeforeUpdate(\think\Model $category) { - + $where = $category->getWhere(); + + if (self::where('name', $category->getData('name'))->where('id', '<>', $where['id'])->find()) { + throw new FailedException('分类名称重复,请重新填写'); + } } /** From bcfb184618f17a3929de38b415d4bea9e1a67b05 Mon Sep 17 00:00:00 2001 From: JaguarJack <82664165@qq.com> Date: Mon, 24 May 2021 21:16:35 +0800 Subject: [PATCH 2/3] update BaseOptions --- catch/cms/model/Category.php | 2 ++ extend/catcher/traits/db/BaseOptionsTrait.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/catch/cms/model/Category.php b/catch/cms/model/Category.php index 8773744..139b04a 100644 --- a/catch/cms/model/Category.php +++ b/catch/cms/model/Category.php @@ -61,6 +61,8 @@ class Category extends BaseModel 'deleted_at', ); + protected $updateChildrenFields = 'status'; + const LIST_TYPE = 1; // 列表 const PAGE_TYPE = 2; // 单页 const COVER_TYPE = 3; // 封面 diff --git a/extend/catcher/traits/db/BaseOptionsTrait.php b/extend/catcher/traits/db/BaseOptionsTrait.php index 0e14c6a..c2caf07 100644 --- a/extend/catcher/traits/db/BaseOptionsTrait.php +++ b/extend/catcher/traits/db/BaseOptionsTrait.php @@ -184,7 +184,7 @@ trait BaseOptionsTrait if (!empty($this->updateChildrenFields)) { if (is_array($this->updateChildrenFields)) { foreach ($data as $field => $value) { - if (in_array($field, $this->updateChildrenFields)) { + if (! in_array($field, $this->updateChildrenFields)) { unset($data[$field]); } } From d6e5fa091adcfb6b6b9ce61f5987064080f5df77 Mon Sep 17 00:00:00 2001 From: JaguarJack <82664165@qq.com> Date: Mon, 24 May 2021 21:17:27 +0800 Subject: [PATCH 3/3] update cms category --- catch/cms/model/Category.php | 2 +- catch/cms/model/events/CategoryEvent.php | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/catch/cms/model/Category.php b/catch/cms/model/Category.php index 139b04a..e351295 100644 --- a/catch/cms/model/Category.php +++ b/catch/cms/model/Category.php @@ -61,7 +61,7 @@ class Category extends BaseModel 'deleted_at', ); - protected $updateChildrenFields = 'status'; + protected $updateChildrenFields = ['status']; const LIST_TYPE = 1; // 列表 const PAGE_TYPE = 2; // 单页 diff --git a/catch/cms/model/events/CategoryEvent.php b/catch/cms/model/events/CategoryEvent.php index 0bd931b..96f52de 100644 --- a/catch/cms/model/events/CategoryEvent.php +++ b/catch/cms/model/events/CategoryEvent.php @@ -48,10 +48,14 @@ trait CategoryEvent */ public static function onBeforeUpdate(\think\Model $category) { - $where = $category->getWhere(); + $data = $category->getData(); - if (self::where('name', $category->getData('name'))->where('id', '<>', $where['id'])->find()) { - throw new FailedException('分类名称重复,请重新填写'); + if (isset($data['name'])) { + $where = $category->getWhere(); + + if (self::where('name', $category->getData('name'))->where('id', '<>', $where['id'])->find()) { + throw new FailedException('分类名称重复,请重新填写'); + } } }