Skip to content

Commit

Permalink
Mention flush cannot be called inside postFlush
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Mar 12, 2013
1 parent 905c0b9 commit b6c3fc5
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/en/reference/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

6 comments on commit b6c3fc5

@PowerKiKi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

@Ocramius
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@PowerKiKi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

@PowerKiKi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Ocramius
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 :-\

@PowerKiKi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.