Skip to content

Commit c2d12c4

Browse files
committed
Better code
1 parent b60d41f commit c2d12c4

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -725,29 +725,31 @@ protected function prepareUpdateData($entity, bool $isInsert = false)
725725
*/
726726
private function isExtraUpdateRequired($entity, $newVal): bool
727727
{
728+
// The association is left empty
728729
if ($newVal === null) {
729730
return false;
730731
}
731732

732-
$oid = spl_object_id($newVal);
733733
$uow = $this->em->getUnitOfWork();
734-
if (! (isset($this->queuedInserts[$oid]) || $uow->isScheduledForInsert($newVal))) {
734+
$oid = spl_object_id($newVal);
735+
736+
// The value is not scheduled for insert so an extra update is not required
737+
if (! isset($this->queuedInserts[$oid]) && ! $uow->isScheduledForInsert($newVal)) {
735738
return false;
736739
}
737740

741+
// The entity is not self-referencing
738742
if ($newVal !== $entity) {
739743
return true;
740744
}
741745

742-
$identifiers = $this->class->getIdentifier();
743-
// Only single-column identifiers are supported
746+
// An extra update is useless for application-generated single-column identifiers
747+
$identifiers = $this->class->getIdentifier();
744748
$entityChangeSet = $uow->getEntityChangeSet($entity);
745-
if (count($identifiers) === 1 && isset($entityChangeSet[$identifiers[0]])) {
746-
// Extra update is required if the current entity does not have yet a value for its identifier
747-
return $entityChangeSet[$identifiers[0]][1] === null;
748-
}
749749

750-
return true;
750+
return count($identifiers) !== 1
751+
|| ! isset($entityChangeSet[$identifiers[0]])
752+
|| $entityChangeSet[$identifiers[0]][1] === null;
751753
}
752754

753755
/**

tests/Doctrine/Tests/ORM/Functional/GH7877Test.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,15 @@ public function testNoExtraUpdateWithApplicationGeneratedId(): void
3535
}
3636

3737
$this->_em->flush();
38-
if ($this->isQueryLogAvailable()) {
39-
if ($this->getQueryLog()->queries[0]['sql'] === '"START TRANSACTION"') {
40-
self::assertQueryCount(3);
41-
} else {
42-
self::assertQueryCount(1);
43-
}
44-
}
38+
$this->checkQueryCount(false);
4539

4640
$this->_em->clear();
4741

4842
$child = $this->_em->find(GH7877ApplicationGenerated::class, $entityId);
4943
$this->assertSame($entityId, $child->parent->id);
5044
}
5145

52-
public function textExtraUpdateWithDatabaseGeneratedId(): void
46+
public function testExtraUpdateWithDatabaseGeneratedId(): void
5347
{
5448
$entity = new GH7877DatabaseGenerated();
5549
$entity->parent = $entity;
@@ -60,20 +54,27 @@ public function textExtraUpdateWithDatabaseGeneratedId(): void
6054
}
6155

6256
$this->_em->flush();
63-
if ($this->isQueryLogAvailable()) {
64-
if ($this->getQueryLog()->queries[0]['sql'] === '"START TRANSACTION"') {
65-
self::assertQueryCount(4);
66-
} else {
67-
self::assertQueryCount(2);
68-
}
69-
}
57+
$this->checkQueryCount(
58+
$this->_em->getClassMetadata(GH7877DatabaseGenerated::class)->idGenerator->isPostInsertGenerator()
59+
);
7060

7161
$entityId = $entity->id;
7262
$this->_em->clear();
7363

7464
$child = $this->_em->find(GH7877DatabaseGenerated::class, $entityId);
7565
$this->assertSame($entityId, $child->parent->id);
7666
}
67+
68+
private function checkQueryCount(bool $extra): void
69+
{
70+
if ($this->isQueryLogAvailable()) {
71+
if ($this->getQueryLog()->queries[0]['sql'] === '"START TRANSACTION"') {
72+
self::assertQueryCount($extra ? 4 : 3);
73+
} else {
74+
self::assertQueryCount($extra ? 2 : 1);
75+
}
76+
}
77+
}
7778
}
7879

7980
/**

0 commit comments

Comments
 (0)