Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public function loadChoicesForValues(array $values, $value = null)

// Optimize performance in case we have an object loader and
// a single-field identifier
if (null === $value && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
$optimize = null === $value || is_array($value) && $value[0] === $this->idReader;

if ($optimize && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
$unorderedObjects = $this->objectLoader->getEntitiesByIds($this->idReader->getIdField(), $values);
$objectsById = array();
$objects = array();
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,30 @@ public function testOverrideChoicesValuesWithCallable()
$this->assertSame('BooGroup/Bar', $field->getViewData());
}

public function testChoicesForValuesOptimization()
{
$entity1 = new SingleIntIdEntity(1, 'Foo');
$entity2 = new SingleIntIdEntity(2, 'Bar');

$this->persist(array($entity1, $entity2));

$field = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'choice_label' => 'name',
));

$this->em->clear();

$field->submit(1);

$unitOfWorkIdentityMap = $this->em->getUnitOfWork()->getIdentityMap();
$managedEntitiesNames = array_map('strval', $unitOfWorkIdentityMap['Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity']);

$this->assertContains((string) $entity1, $managedEntitiesNames);
$this->assertNotContains((string) $entity2, $managedEntitiesNames);
}

public function testGroupByChoices()
{
$item1 = new GroupableEntity(1, 'Foo', 'Group1');
Expand Down