limingxinleo / swoft-entity-event
Swoft实体事件组件
1.1.2
2018-11-26 08:07 UTC
Requires
- php: >=7.1
- swoft/db: ^1.0
- swoft/framework: ^1.0.24
Requires (Dev)
- phpstan/phpstan: ^0.10.5
- phpunit/phpunit: ^5.7
- swoft/swoole-ide-helper: dev-master
README
安装
composer require limingxinleo/swoft-entity-event
使用
定义事件监听器
<?php namespace SwoftTest\Testing\Event; use Swoft\Db\Model; use Swoftx\EntityEvent\Annotation\EventListener; use Swoftx\EntityEvent\EventInterface; use SwoftTest\Testing\Entity\User; use Swoft\Bean\Annotation\Bean; /** * @Bean * @EventListener(User::class) */ class UserEventListener implements EventInterface { public function beforeCreate(Model $model): Model { // Do something... return $model; } public function afterCreate(Model $model): Model { // Do something... return $model; } public function beforeUpdate(Model $model): Model { // Do something... return $model; } public function afterUpdate(Model $model): Model { // Do something... return $model; } }
使用
<?php use SwoftTest\Testing\Entity\User; use Swoftx\EntityEvent\Event; $uniqid = uniqid(); $user = new User(); $user->setName($uniqid); $user->setRoleId(1); $event = bean(Event::class); $id = $event->create($user);