执行顺序:
beforeDel(控制器) → onDelBefore(模型) → onDelAfter(模型) → onDelAfter(控制器)
方式一:控制器
<?phpnamespace app\controller;use app\BaseController;/*** 人员控制器*/class User extends BaseController{use \tpScriptVueCurd\base\controller\Controller;public function init(): void{$this->title='人员信息';$this->md=\app\model\User::make($this);}/*** 删除前执行* @param array $ids 根据请求id,返回要删除的数据id* @return array*/protected function beforeDel(array $ids): array{throw new \think\Exception('此处抛出异常将会终止删除,并提示此处抛出的异常');return $ids;}/*** 删除后执行* @param \think\Collection $delInfos 已删除的数据信息* @return void*/protected function afterDel(\think\Collection $delInfos): void{throw new \think\Exception('此处抛出异常将会终止删除,并提示此处抛出的异常');}}
方式二:模型
<?php
namespace app\model;
use think\model\Collection;
use tpScriptVueCurd\base\model\BaseModel;
use tpScriptVueCurd\field\RadioField;
use tpScriptVueCurd\field\StringField;
use tpScriptVueCurd\FieldCollection;
class User extends BaseModel
{
/**
* 表字段配置
* @return FieldCollection
*/
public function fields(): FieldCollection
{
return new FieldCollection([
StringField::init('name','姓名'),
RadioField::init('sex', '性别'),
]);
}
/**
* 删除数据前
* @param Collection $delList 要删除的数据
* @return void
*/
protected function onDelBefore(Collection $delList): void
{
throw new \think\Exception('此处抛出异常将会终止删除,并提示此处抛出的异常');
}
/**
* 删除数据后
* @param Collection $delList 已删除的数据
* @return void
*/
protected function onDelAfter(Collection $delList): void
{
throw new \think\Exception('此处抛出异常将会终止删除,并提示此处抛出的异常');
}
}
