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

Open
opened 2026-01-22 15:17:28 +01:00 by admin · 0 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:28 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5776