Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get flushed entities on Doctrine postFlush event

Consider next steps in Symfony application:

  1. Register listener for Doctrine`s postFlush event that dump results of getScheduledEntityInsertions() of Unit of Work.
  2. Create instance of SomeEntity class.
  3. Call Entity Manager`s persist and flush methods with previously created entity instance
  4. postFlush listener fired
  5. getScheduledEntityInsertions() call return nothing

Expected behaviour is that at last step I see a list of inserted entities, but if you look at executeInserts() method of UoF, unset($this->entityInsertions[$oid]) called for each entity so nothig is returned from getScheduledEntityInsertions() call.

Iam found declined patch for that case: https://github.com/doctrine/doctrine2/pull/5674 and Ocramius suggestion to use "onFlush" event to collect all needed data and fired custom event: https://groups.google.com/forum/#!topic/doctrine-user/GLJEx0p5kL4

But, I dont understand, how it can be done: if I register custom listener for myCustomEvent and fire it from onFlush with list of not inserted entities - it will be executed before entities actually saved to database (postFlush fired) and cant be received from database in this listener.

like image 929
Alex Avatar asked Feb 24 '17 09:02

Alex


Video Answer


1 Answers

One of possible solutions is to use one Listener class for onFlush and postFlush event, and in onFlush populate internal property with necessary data, in postFlush read data from property. But this solution has limitations: both listener-methods must be defined same class; listener is not thread-safe.

like image 156
Alex Avatar answered Sep 21 '22 15:09

Alex