Skip to content

Loading…

DDC-2496: Unexpected behaviour flushing single entity #3216

Closed
doctrinebot opened this Issue · 5 comments

2 participants

@doctrinebot

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

@doctrinebot

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

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

Issue was closed with resolution "Invalid"

@doctrinebot

Comment created by hrajchert:

So how would you recommend to resolve the findOrCreate issues?

@doctrinebot

Comment created by @ocramius:

You would have to flush afterwards.

@Ocramius Ocramius was assigned by doctrinebot
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.