Answer the question
In order to leave comments, you need to log in
Audit log (History of actions), the best option?
Good afternoon.
I want to make a flexible change history for some actions in symfony.
The question is how best to do it.
1. Hang an event in the "Entity" entity itself:
class Entity
{
//поля сущности
private array $events = [];
protected function recordEvent(object $event): void
{
$this->events[] = $event;
}
public function someAction($blal): void
{
//что-то делаем
$this->recordEvent(new Event\LogSomeMadeAction($args)); //эвент на сохранения записи в бд
}
}
class ServiceHandler
{
public function handle($args): void
{
$entity->someAction($blal);
//логируем в бд без всяких эвентов
}
}
Answer the question
In order to leave comments, you need to log in
All aggregate events must be placed at the very root of the aggregate, as in option 1, but the method that will apply these events must be called after the data is saved.
In other words, in $events
you add up all the events when working with the aggregate entity, and in the Handler iterate through this array and send each event to the EventDispatcher. Well, there you already make log event subscribers, if necessary. The main thing is that all events get to the dispatcher after the data is saved to the database. Otherwise, it may turn out that an error occurred and the transaction rollback worked, rolling back all the changes, and the events have already gone.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question