T
T
tukreb2021-01-15 01:02:02
symfony
tukreb, 2021-01-15 01:02:02

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)); //эвент на сохранения записи в бд
    }
}

2. In the service where all the main calls to entity methods take place:
class ServiceHandler
{
    public function handle($args): void
    {
        $entity->someAction($blal);
        //логируем в бд без всяких эвентов
       
    }
}

3. Do all this in the controllers themselves, along with Monolog.

I think towards option 1, it looks more flexible, but is it a good solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-01-15
@tukreb

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 $eventsyou 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 question

Ask a Question

731 491 924 answers to any question