Skip to content

Commit 7fc2725

Browse files
kimlaifabpot
authored andcommitted
[DoctrineBridge][Form] Fix performance regression in EntityType
1 parent 137678a commit 7fc2725

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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();

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)