Calling entityManager#flush in preFlush event listener causes infinite recursive loop #5791

Closed
opened 2026-01-22 15:18:05 +01:00 by admin · 4 comments
Owner

Originally created by @egonolieux on GitHub (Nov 29, 2017).

To override the default commit order for collections because of a unique constraint (delete before insert instead of insert before delete), I manually add removed collection items to a separate array inside the entity instead of using orphanRemoval. In a preFlush event listener I call entityManager#remove on each removed collection item followed by entityManager#flush.

However, this seems to cause the preFlush event to be caught in an infinite recursive loop. Although this seems somewhat expected, the documentation says it is safe to call entityManager#flush inside the preFlush event: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preflush, which is why I implemented it anyway.

Is this a mistake in the documentation or could this be a bug?

Originally created by @egonolieux on GitHub (Nov 29, 2017). To override the default commit order for collections because of a unique constraint (delete before insert instead of insert before delete), I manually add removed collection items to a separate array inside the entity instead of using `orphanRemoval`. In a `preFlush` event listener I call `entityManager#remove` on each removed collection item followed by `entityManager#flush`. However, this seems to cause the `preFlush` event to be caught in an infinite recursive loop. Although this seems somewhat expected, the documentation says it is safe to call `entityManager#flush` inside the `preFlush` event: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preflush, which is why I implemented it anyway. Is this a mistake in the documentation or could this be a bug?
admin closed this issue 2026-01-22 15:18:06 +01:00
Author
Owner

@Ocramius commented on GitHub (Nov 29, 2017):

This is a mistake in the documentation: flushing inside lifecycle event listeners is generally NOT supported.

@Ocramius commented on GitHub (Nov 29, 2017): This is a mistake in the documentation: flushing inside lifecycle event listeners is generally NOT supported.
Author
Owner

@egonolieux commented on GitHub (Nov 29, 2017):

Alright, that explains a lot. Could you please fix the documentation so that others won't make the same mistake?

@egonolieux commented on GitHub (Nov 29, 2017): Alright, that explains a lot. Could you please fix the documentation so that others won't make the same mistake?
Author
Owner

@Ocramius commented on GitHub (Nov 29, 2017):

Please send a patch: the documentation is in this repository

On 29 Nov 2017 21:09, "Egon Olieux" notifications@github.com wrote:

Alright, that explains a lot. Could you please fix the documentation so
that others won't make the same mistake?


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6857#issuecomment-347981073,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakMF8HLec1cTTdhXyhvKr6aM096Zmks5s7bn9gaJpZM4QviTA
.

@Ocramius commented on GitHub (Nov 29, 2017): Please send a patch: the documentation is in this repository On 29 Nov 2017 21:09, "Egon Olieux" <notifications@github.com> wrote: Alright, that explains a lot. Could you please fix the documentation so that others won't make the same mistake? — You are receiving this because you commented. Reply to this email directly, view it on GitHub <https://github.com/doctrine/doctrine2/issues/6857#issuecomment-347981073>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAJakMF8HLec1cTTdhXyhvKr6aM096Zmks5s7bn9gaJpZM4QviTA> .
Author
Owner

@egonolieux commented on GitHub (Nov 29, 2017):

Created a pull request: https://github.com/doctrine/doctrine2/pull/6858

@egonolieux commented on GitHub (Nov 29, 2017): Created a pull request: https://github.com/doctrine/doctrine2/pull/6858
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5791