DDC-2496: Unexpected behaviour flushing single entity #3136

Closed
opened 2026-01-22 14:13:16 +01:00 by admin · 5 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 10, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user hrajchert:

The following works as expected:
$existingElement = $repository->find(1);
$existingElement->setSomething('foo');
$newElement = new Element('bar');
$em->persist($newElement);
$em->flush($newElement);
// The new element is added and the existing is not modified

The following does not:
$existingElement = $repository->find(1);
$em->remove(existingElement);
$newElement = new Element('bar');
$em->persist($newElement);
$em->flush($newElement);
// The new element is added and the existing is deleted (i didnt flush THAT element)

The following does not:
$newElement2 = new Element('foo');
$em->persist($newElement2);
$newElement = new Element('bar');
$em->persist($newElement);
$em->flush($newElement);
// Both elements are persisted, even if I only flushed $newElement

Originally created by @doctrinebot on GitHub (Jun 10, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user hrajchert: The following works as expected: $existingElement = $repository->find(1); $existingElement->setSomething('foo'); $newElement = new Element('bar'); $em->persist($newElement); $em->flush($newElement); // The new element is added and the existing is not modified The following does not: $existingElement = $repository->find(1); $em->remove(existingElement); $newElement = new Element('bar'); $em->persist($newElement); $em->flush($newElement); // The new element is added and the existing is deleted (i didnt flush THAT element) The following does not: $newElement2 = new Element('foo'); $em->persist($newElement2); $newElement = new Element('bar'); $em->persist($newElement); $em->flush($newElement); // Both elements are persisted, even if I only flushed $newElement
admin added the Bug label 2026-01-22 14:13:16 +01:00
admin closed this issue 2026-01-22 14:13:17 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 12, 2013):

Comment created by hrajchert:

By the way, I noticed in the code that this seems to be "expected behaviour", but it doesn't make too much sense, as the idea is to not disturb the original unit of work.

The problem arises when I call some findOrCreate methods that I've done. The idea is that those entities should be there, but not always are, and they usually occur on the middle of some other processing, so if I have some new elements before calling the findOrCreate I dont want them to be inserted until I flush them in particular.

Right now Im creating a new EntityManager (which involves a new connection), doing the insert there and then merging into the other EntityManager, but the merge is very error prone to the order you merge stuff.

@doctrinebot commented on GitHub (Jun 12, 2013): Comment created by hrajchert: By the way, I noticed in the code that this seems to be "expected behaviour", but it doesn't make too much sense, as the idea is to not disturb the original unit of work. The problem arises when I call some findOrCreate methods that I've done. The idea is that those entities should be there, but not always are, and they usually occur on the middle of some other processing, so if I have some new elements before calling the findOrCreate I dont want them to be inserted until I flush them in particular. Right now Im creating a new EntityManager (which involves a new connection), doing the insert there and then merging into the other EntityManager, but the merge is very error prone to the order you merge stuff.
Author
Owner

@doctrinebot commented on GitHub (Jun 12, 2013):

Comment created by @ocramius:

Flushing with a given parameter is only about optimizing changeset computation - it is not meant to flush only the new element.

@doctrinebot commented on GitHub (Jun 12, 2013): Comment created by @ocramius: Flushing with a given parameter is only about optimizing changeset computation - it is not meant to flush only the new element.
Author
Owner

@doctrinebot commented on GitHub (Jun 12, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Jun 12, 2013): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Jun 17, 2013):

Comment created by hrajchert:

So how would you recommend to resolve the findOrCreate issues?

@doctrinebot commented on GitHub (Jun 17, 2013): Comment created by hrajchert: So how would you recommend to resolve the findOrCreate issues?
Author
Owner

@doctrinebot commented on GitHub (Jun 17, 2013):

Comment created by @ocramius:

You would have to flush afterwards.

@doctrinebot commented on GitHub (Jun 17, 2013): Comment created by @ocramius: You would have to flush afterwards.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3136