Permalink
Browse files

Mention flush cannot be called inside postFlush

  • Loading branch information...
1 parent 905c0b9 commit b6c3fc5b1ab8f97ba3a47b5a667ef8986c48059e @beberlei beberlei committed Mar 12, 2013
Showing with 2 additions and 2 deletions.
  1. +2 −2 docs/en/reference/events.rst
@@ -535,8 +535,8 @@ The following restrictions apply to the onFlush event:
postFlush
~~~~~~~~~
-``postFlush`` is called at the end of ``EntityManager#flush()``. ``EntityManager#flush()`` can be
-called safely inside its listeners.
+``postFlush`` is called at the end of ``EntityManager#flush()``.
+``EntityManager#flush()`` can **NOT** be called safely inside its listeners.
.. code-block:: php

6 comments on commit b6c3fc5

Contributor

PowerKiKi replied Dec 8, 2014

Hey @beberlei would you mind explaining why flush() cannot be called anymore ? I had a look at other commits in history, but did not find anything that seemed related to this one.

It would be really useful for my use-case to be able to create and flush entities during a postFlush callback...

Owner

Ocramius replied Dec 8, 2014

You can't flush in flush lifecycle events: it has never been supported

Contributor

PowerKiKi replied Dec 8, 2014

The previous manual before this commit seems to say otherwise, and posts such as http://stackoverflow.com/a/16906067/37706 also seem to say it is possible. That's why I am confused.

Maybe I should re-formulate my question. My use-case is to create a log of (certain) entities modification. So AFAIK my requirements are a callback where:

  • I known the ID of my entity (not prePersist)
  • I can create and write in DB new entities (probably via flush, or any other mechanism if possible)
  • must be working for persist, update and remove

That seems relatively common need, but I could not find an "official" recommendation on how to do that. I must say the documentation on events is hard to read, because there are a lot of information, not very well structure IMHO.

I tried with lifecycle on the entity class, but during preUpdate I could not create Log. I am now trying with a EventSubscriber and onFlush/postFlush events, but it's not successful yet either...

Any recommendations ?

Contributor

PowerKiKi replied Dec 8, 2014

Mhhh, now I'm really confused, I ended up implementing a flush() in the postFlush event and everything is working fine. I doubled checked SQL generated and everything seems fine.

And in-depth explanation of what may fail would really be appreciated.

Owner

Ocramius replied Dec 8, 2014

@PowerKiKi yes, it works, but it has never been officially supported, mainly because every second user applying this kind of logic falls into infinite recursion :-\

Contributor

PowerKiKi replied Dec 9, 2014

Ok, thanks for the explanation, luckily infinite recursion was easy to avoid in my case ;-)

Please sign in to comment.