Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Messenger] Add support for "recording" events from entities#28850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Koc commentedOct 15, 2018
I'm afraid that this listener should be stateful and events should be dispatched on post flush eventhttps://github.com/doctrine/doctrine2/blob/2.7/lib/Doctrine/ORM/UnitOfWork.php#L430 (because Doctrine's transaction may fall) |
9441919 to305438eCompare274e3dd to5d75022CompareNyholm commentedMar 19, 2019
This PR is rebased and squashed |
fabpot commentedApr 8, 2019
/cc@sroze |
sroze left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I agree with@Koc, thedispatch of these events needs to happen atpostFlush otherwise you might dispatch events from rollbacked entities.
But it's a fantastic feature ❤️
Nyholm commentedAug 22, 2019
Thank you. I've updated the PR |
src/Symfony/Bridge/Doctrine/Messenger/EventSubscriber/EntityMessageCollector.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Bridge/Doctrine/Messenger/EventSubscriber/EntityMessageCollector.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
| if ($entity instanceof EntityMessageCollectionInterface) { | ||
| foreach ($entity->getRecordedMessages() as $message) { | ||
| $this->messageBus->dispatch($message, [new DispatchAfterCurrentBusStamp()]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What if the message bus throws an exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
That will bubble up. But the Doctrine transaction will still be committed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
So there's a risk some messages will be dispatched while some won't. Not sure if that's an issue yet, just pointing it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Yes, If we dispatch 5 messages and number 2 fails, then the 3 other will never be handled. But that is an issue with the message bus and has nothing to do with this PR.
Nyholm commentedSep 20, 2019
Maybe@weaverryan could have a look? |
sroze left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looks good to me, sorry for the delay 👍.
I imagine you'll PR onDoctrineBundle to enable this listener from a configuration flag?
sroze commentedSep 29, 2019
@Nyholm can you squad and push again? We should have Travis & AppVeyer rebuilding this :) |
87444c6 to692448fCompareNyholm commentedSep 29, 2019
Comments are squashed. I will create a PR to doctrine bundle right away |
maxhelias commentedSep 29, 2019
Nice feature! This should not be in 4.4 ? |
vudaltsov commentedSep 30, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
What if we avoid leaking events and eliminate the getter on the entity? publicfunctiondispatchEvents(callable$dispatcher):void{$dispatcher($this->events);$this->events = [];} |
Nyholm commentedSep 30, 2019
Hm. Callables makes the stack tree hard to follow. @vudaltsov How would you configure an implementation like this to work with DI? |
vudaltsov commentedSep 30, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@Nyholm , this would be almost the same code you use: privatefunctioncollectEventsFromEntity(LifecycleEventArgs$message):void{$entity =$message->getEntity();if (!$entityinstanceof EntityMessageCollectionInterface) {return; }$entity->dispatchEvents(function (iterable$events):void {foreach ($eventsas$event) {$this->messageBus->dispatch($event, [newDispatchAfterCurrentBusStamp()]); } });} As you see, we only need one command method in this case, not query+command. |
vudaltsov commentedSep 30, 2019 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Also, what if we implement this as a middleware, so that we don't depend on Doctrine here? It could do the same thing as the subscriber after the |
| $this->collectEventsFromEntity($event); | ||
| } | ||
| private function collectEventsFromEntity(LifecycleEventArgs $message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
if the method is calledcollectEventsFromEntity, then it should acceptobject $entity as argument?
| ]; | ||
| } | ||
| public function postFlush(LifecycleEventArgs $event) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This code will fail.
ThepostFlush event is not a lifecycle event. It passes an instance ofDoctrine\ORM\Event\PostFlushEventArgs which is not a subclass ofLifecycleEventArgs. Seehttps://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#postflush .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
So this event cannot be used here at all, because at this point you do not know which entities were flushed.
Nyholm commentedMay 3, 2020
Im closing this in favor of#34310 |
Uh oh!
There was an error while loading.Please reload this page.
This is dependent of#28849.