Revision: Tue, 16 Aug 2022 19:49:12 GMT

Events

The package dispatch several events, allowing you to hook into the following moments in an entity's lifecycle:

An event object contains the following public properties:

// Entity role
print_r($event->role);      

// Entity mapper
print_r($event->mapper);     

// Read only entity object. Change entity property values carefully.
// Property value changes won't affect persisting data, but will affect next event listeners.
print_r($event->entity);    

// Use for changing persisting data. Data from state will be stored to the database
print_r($event->state);

// DateTime object shared between all events
print_r($event->timestamp); 

// Entity state before changes
print_r($event->node); 

OnCreate

The event will dispatch before an entity is stored to the database.


use Cycle\ORM\Entity\Behavior\Attribute\Listen;
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command;

#[Listen(OnCreate::class)]
public function onCreate(Command\OnCreate $event): void
{
    $event->state->register('id', Uuid::uuid4());
}

OnUpdate

The event will dispatch before an entity is updated in the database.

use Cycle\ORM\Entity\Behavior\Event\Mapper\Command;

public function onUpdate(Command\OnUpdate $event): void
{
    $event->state->register('updated_at', new \DateTimeImmutable());
}

OnDelete

The event will dispatch before an entity is deleted from the database.

use Cycle\ORM\Entity\Behavior\Event\Mapper\Command;

public function onDelete(Command\OnDelete $event): void
{
    if (!$event->entity->getAuthor()->hasRole('admin')) {
        throw new UnauthorizedException('You don\'t have right to delete this entity.');
    }
}

QueueCommand

The event will dispatch all the events for an entity.

use Cycle\ORM\Entity\Behavior\Attribute\Listen;
use Cycle\ORM\Entity\Behavior\Event\Mapper\QueueCommand;
use Cycle\ORM\Entity\Behavior\Event\Mapper\Command;

#[Listen(QueueCommand::class)]
public function onCreate(QueueCommand $event): void
{
    if ($event instanceof Command\OnCreate){
        // ...
    }
    
    if ($event instanceof Command\OnUpdate){
        // ...
    }
    
    if ($event instanceof Command\OnDelete){
        // ...
    }
}
Edit this page