catchAdmin/catch/cms/model/events/CategoryEvent.php

75 lines
2.4 KiB
PHP
Raw Normal View History

2021-05-22 11:02:45 +08:00
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model\events;
2021-05-24 21:08:01 +08:00
use catcher\exceptions\CatchException;
2021-05-22 11:02:45 +08:00
use catcher\exceptions\FailedException;
use catcher\Utils;
trait CategoryEvent
{
/**
* 插入前
*
* @time 2021年03月03日
* @param \think\Model $category
* @return void
2021-05-24 21:08:01 +08:00
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
2021-05-22 11:02:45 +08:00
*/
2021-05-23 20:35:21 +08:00
public static function onBeforeInsert(\think\Model $category): void
2021-05-22 11:02:45 +08:00
{
2021-05-24 21:08:01 +08:00
if (self::where('name', $category->getData('name'))->find()) {
throw new FailedException('分类名称重复,请重新填写');
}
2021-05-22 11:02:45 +08:00
}
/**
* 更新前
*
* @time 2021年03月03日
* @param \think\Model $category
* @return mixed|void
2021-05-24 21:08:01 +08:00
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
2021-05-22 11:02:45 +08:00
*/
2021-05-23 20:35:21 +08:00
public static function onBeforeUpdate(\think\Model $category)
2021-05-22 11:02:45 +08:00
{
2021-05-24 21:17:27 +08:00
$data = $category->getData();
2021-05-24 21:08:01 +08:00
2021-05-24 21:17:27 +08:00
if (isset($data['name'])) {
$where = $category->getWhere();
if (self::where('name', $category->getData('name'))->where('id', '<>', $where['id'])->find()) {
throw new FailedException('分类名称重复,请重新填写');
}
2021-05-24 21:08:01 +08:00
}
2021-05-22 11:02:45 +08:00
}
/**
* 删除前
*
* @time 2021年03月03日
* @param $category
* @return void
*/
public static function onBeforeDelete($category)
{
if ($category->hasNextLevel()) {
throw new FailedException('存在下级栏目, 无法删除');
}
}
}