Skip to content

Commit 46d1d24

Browse files
committed
bug #17990 [DoctrineBridge][Form] Fix performance regression in EntityType (kimlai)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #17990). Discussion ---------- [DoctrineBridge][Form] Fix performance regression in EntityType | Q | A | ------------- | --- | Branch | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | A performance regression was introduced in 2336d5c Before, the default behaviour of the `DoctrineLoader` was to only fetch the entities selected in the submitted form. After, the optimization was only performed when the `choice_value` option was set to `null`. However, the `DoctrineType` sets a non-null default value to `choice_value`, which means that the default behaviour was not using the optimization anymore. This commit restores the default behaviour (while keeping the previous commit intent). References: - https://github.com/symfony/symfony/blob/v2.7.10/src/Symfony/Bridge/Doctrine/Form/ChoiceList/DoctrineChoiceLoader.php#L149 - https://github.com/symfony/symfony/blob/v2.7.10/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php#L216 Commits ------- 64c80a6 [DoctrineBridge][Form] Fix performance regression in EntityType
2 parents 5b79649 + 64c80a6 commit 46d1d24

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/DoctrineChoiceLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ public function loadChoicesForValues(array $values, $value = null)
146146

147147
// Optimize performance in case we have an object loader and
148148
// a single-field identifier
149-
if (null === $value && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
149+
$optimize = null === $value || is_array($value) && $value[0] === $this->idReader;
150+
151+
if ($optimize && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
150152
$unorderedObjects = $this->objectLoader->getEntitiesByIds($this->idReader->getIdField(), $values);
151153
$objectsById = array();
152154
$objects = array();

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,30 @@ public function testOverrideChoicesValuesWithCallable()
789789
$this->assertSame('BooGroup/Bar', $field->getViewData());
790790
}
791791

792+
public function testChoicesForValuesOptimization()
793+
{
794+
$entity1 = new SingleIntIdEntity(1, 'Foo');
795+
$entity2 = new SingleIntIdEntity(2, 'Bar');
796+
797+
$this->persist(array($entity1, $entity2));
798+
799+
$field = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
800+
'em' => 'default',
801+
'class' => self::SINGLE_IDENT_CLASS,
802+
'choice_label' => 'name',
803+
));
804+
805+
$this->em->clear();
806+
807+
$field->submit(1);
808+
809+
$unitOfWorkIdentityMap = $this->em->getUnitOfWork()->getIdentityMap();
810+
$managedEntitiesNames = array_map('strval', $unitOfWorkIdentityMap['Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity']);
811+
812+
$this->assertContains((string) $entity1, $managedEntitiesNames);
813+
$this->assertNotContains((string) $entity2, $managedEntitiesNames);
814+
}
815+
792816
public function testGroupByChoices()
793817
{
794818
$item1 = new GroupableEntity(1, 'Foo', 'Group1');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy