update cms category event

This commit is contained in:
JaguarJack 2021-05-24 21:08:01 +08:00
parent 3bad548d10
commit e105bef460

View File

@ -13,6 +13,7 @@
namespace catchAdmin\cms\model\events; namespace catchAdmin\cms\model\events;
use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException; use catcher\exceptions\FailedException;
use catcher\Utils; use catcher\Utils;
@ -24,10 +25,15 @@ trait CategoryEvent
* @time 2021年03月03日 * @time 2021年03月03日
* @param \think\Model $category * @param \think\Model $category
* @return void * @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 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日 * @time 2021年03月03日
* @param \think\Model $category * @param \think\Model $category
* @return mixed|void * @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) 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('分类名称重复,请重新填写');
}
} }
/** /**