模型事件

事件列表

事件名 常量 描述
BeforeFind ModelEvents::BEFORE_FIND 查找前,Model::find()触发
AfterFind ModelEvents::AFTER_FIND 查找后,Model::find()触发
BeforeSelect ModelEvents::BEFORE_SELECT 查询前,Model::select()触发
AfterSelect ModelEvents::AFTER_SELECT 查询后,Model::select()触发
BeforeInit ModelEvents::BEFORE_INIT 初始化值前,newInstance()触发
AfterInit ModelEvents::AFTER_INIT 初始化值后,newInstance()触发
BeforeInsert ModelEvents::BEFORE_INSERT 插入前,insert()/save()触发
AfterInsert ModelEvents::AFTER_INSERT 插入前,insert()/save()触发
BeforeUpdate ModelEvents::BEFORE_UPDATE 更新前,update()/save()触发
AfterUpdate ModelEvents::AFTER_UPDATE 更新前,update()/save()触发
BeforeDelete ModelEvents::BEFORE_DELETE 删除前,delete()触发
AfterDelete ModelEvents::AFTER_DELETE 删除前,delete()触发
BeforeSave ModelEvents::BEFORE_SAVE 保存前,先于插入前和更新前触发
AfterSave ModelEvents::AFTER_SAVE 保存后,后于插入后和更新后触发
BeforeBatchUpdate ModelEvents::BEFORE_BATCH_UPDATE 批量更新前
AfterBatchUpdate ModelEvents::AFTER_BATCH_UPDATE 批量更新后
BeforeBatchDelete ModelEvents::BEFORE_BATCH_DELETE 批量删除前
AfterBatchDelete ModelEvents::AFTER_BATCH_DELETE 批量删除后

事件传递过来的参数类型为Imi\Model\Event\Param\事件名EventParam

BeforeInitAfterInit是例外,共用Imi\Model\Event\Param\InitEventParam

事件监听

事件监听分两种,一种是仅限于对象的事件,另一种是静态方法触发的事件(批量操作)。

监听方式分两种:1、在类里写监听代码2、定义监听类

批量操作的事件监听,一般建议用监听类方式。对象的事件监听根据习惯选择即可。

对象事件监听方法1-监听代码

  1. /**
  2. * Test
  3. * @Entity
  4. * @Table(name="tb_test", id={"id", "a"})
  5. */
  6. class Test extends Model
  7. {
  8. public function __init($data = [])
  9. {
  10. $this->on(ModelEvents::AFTER_INIT, [$this, 'onAfterInit']);
  11. parent::__init($data);
  12. }
  13. public function onAfterInit(\Imi\Model\Event\Param\InitEventParam $data)
  14. {
  15. var_dump($data->data);
  16. }
  17. }

对象事件监听方法2-定义监听类

  1. <?php
  2. namespace XXX\ModelEvent\Logs;
  3. use Imi\Bean\Annotation\ClassEventListener;
  4. use Imi\Model\Event\Param\BeforeInsertEventParam;
  5. use Imi\Model\Event\Listener\IBeforeInsertEventListener;
  6. /**
  7. * 插入前处理
  8. * @ClassEventListener(className="\XXX\Model\Test",eventName=\Imi\Model\Event\ModelEvents::BEFORE_INSERT)
  9. */
  10. class BeforeInsert implements IBeforeInsertEventListener
  11. {
  12. /**
  13. * 事件处理方法
  14. * @param BeforeInsertEventParam $e
  15. * @return void
  16. */
  17. public function handle(BeforeInsertEventParam $e)
  18. {
  19. $e->data->data = json_encode($e->data->data);
  20. $e->data->ip = inet_pton($e->data->ip);
  21. }
  22. }

批量操作事件监听

事件名称为模型类名+事件名,如:XXX\Model\TestBefore+BatchUpdate=XXX\Model\TestBeforeBatchUpdate

  1. <?php
  2. namespace XXX\Listener;
  3. use Imi\Model\Event\Param\BeforeBatchUpdateEventParam;
  4. use Imi\Model\Event\Listener\IBeforeBatchUpdateEventListener;
  5. /**
  6. * @Listener("XXX\Model\TestBeforeBatchUpdate")
  7. */
  8. class BeforeBatchUpdate implements IBeforeBatchUpdateEventListener
  9. {
  10. /**
  11. * 事件处理方法
  12. * @param BeforeBatchUpdateEventParam $e
  13. * @return void
  14. */
  15. public function handle(BeforeBatchUpdateEventParam $e)
  16. {
  17. // $e->data->name = '123'; // 在更新前可以对数据赋值
  18. }
  19. }