Question: Why are all inserts executed when single entity is committed? #5777

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

Originally created by @ossinkine on GitHub (Nov 21, 2017).

Originally assigned to: @Ocramius on GitHub.

According to UnitOfWork.php#L512 the all insertions will be executed even I want to commit single entity.
It looks doubtful. See example:

$user = $entityManager->find(User::class, $id);
foreach ($uris as $uri) {
    $document = new Document();
    $document->setUri($uri);
    $document->setUser($user);
    $entity->persist($document);
    $user->setDocumentCounter($user->getDocumentCounter() + 1);
}

$someData = $entityManager->find(SomeData::class, $id);
$someData->setFoo('bar');
$entityManager->flush($someData);
// It's unexpected for me that all documents will be persisted here, but user not.
// If this is done for reasons of data integrity, then obviously the integrity is violated.
// User should be commited with documents in single transaction.

$entityManager->flush();
Originally created by @ossinkine on GitHub (Nov 21, 2017). Originally assigned to: @Ocramius on GitHub. According to [UnitOfWork.php#L512](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/UnitOfWork.php#L512) the all insertions will be executed even I want to commit single entity. It looks doubtful. See example: ```php $user = $entityManager->find(User::class, $id); foreach ($uris as $uri) { $document = new Document(); $document->setUri($uri); $document->setUser($user); $entity->persist($document); $user->setDocumentCounter($user->getDocumentCounter() + 1); } $someData = $entityManager->find(SomeData::class, $id); $someData->setFoo('bar'); $entityManager->flush($someData); // It's unexpected for me that all documents will be persisted here, but user not. // If this is done for reasons of data integrity, then obviously the integrity is violated. // User should be commited with documents in single transaction. $entityManager->flush(); ```
admin added the Question label 2026-01-22 15:17:31 +01:00
admin closed this issue 2026-01-22 15:17:31 +01:00
Author
Owner

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

$entityManager->flush($someData); is to be avoided. We removed support for the parameters being passed to flush() exactly because it was a performance optimisation that just caused more problems than what it solved.

@Ocramius commented on GitHub (Nov 21, 2017): `$entityManager->flush($someData);` is to be avoided. We removed support for the parameters being passed to `flush()` exactly because it was a performance optimisation that just caused more problems than what it solved.
Author
Owner

@ossinkine commented on GitHub (Nov 23, 2017):

Guys, you are guru of DDD and of other cool things, give me an advice.
I want to keep separately my Doctrine entities and Domain models according to persistence ignorance practice.
My business logiс should work with models and domain managers (which can create and update models), and domain managers should work with entities or documents or something else.
What is your opinion, is it a good way?
If yes, where flush method should be called considering that different domain managers can use different storage?

@ossinkine commented on GitHub (Nov 23, 2017): Guys, you are guru of DDD and of other cool things, give me an advice. I want to keep separately my Doctrine entities and Domain models according to persistence ignorance practice. My business logiс should work with models and domain managers (which can create and update models), and domain managers should work with entities or documents or something else. What is your opinion, is it a good way? If yes, where flush method should be called considering that different domain managers can use different storage?
Author
Owner

@lcobucci commented on GitHub (Nov 24, 2017):

If yes, where flush method should be called considering that different domain managers can use different storage?

@ossinkine if you're using a service bus to encapsulate the execution of handlers (what you're calling a "domain manager") you can use a middleware that would start the transaction and call the flush() after things were processed (if you don't have an exception, sure).

With that your handlers should simple use the repositories to fetch/append/remove objects and modify the object state to update data.

@lcobucci commented on GitHub (Nov 24, 2017): > If yes, where flush method should be called considering that different domain managers can use different storage? @ossinkine if you're using a service bus to encapsulate the execution of handlers (what you're calling a "domain manager") you can use a middleware that would start the transaction and call the `flush()` after things were processed (if you don't have an exception, sure). With that your handlers should simple use the repositories to fetch/append/remove objects and modify the object state to update data.
Author
Owner

@lcobucci commented on GitHub (Nov 24, 2017):

I'll be closing this issue since it has already been answered by @Ocramius but feel free to continue with the off-topic =)

@lcobucci commented on GitHub (Nov 24, 2017): I'll be closing this issue since it has already been answered by @Ocramius but feel free to continue with the off-topic =)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5777