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);