I
I
Igor Katkov2016-06-13 15:09:44
Doctrine ORM
Igor Katkov, 2016-06-13 15:09:44

Is it good practice to bind to Doctrine events?

Good afternoon!
Ripe question. For example, there is a task to create logs (or send emails) at the time of creating the User entity. We decide to implement this through events. There are two options:
1) Listen to Doctrine:prePersist and call the appropriate service.
2) Create your own event:

$this->userRepository->save($user);
$this->eventDispatcher->dispatch(SomeEvent::EVENT, new UserEvent($user));

Which option is more preferable? What will be BestPractice in this case? Is it generally good to listen to Doctrine events or look for other opportunities?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Kulikovsky, 2016-06-13
@by25

For such purposes, it is better to use events. Better yet, domain-event.
Example here: https://github.com/fesor/domain-events

S
shagguboy, 2016-06-20
@shagguboy

doctrine events are made just to be listened to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question