From a430f3d8a6caffde3a6439cf0c5778abb1508413 Mon Sep 17 00:00:00 2001 From: "Thomas Chmielowiec (chmielot)" Date: Wed, 4 Apr 2012 15:35:00 +0200 Subject: [PATCH] [#3446] [Form] Fix getChoicesForValues of EntityChoiceList on empty values --- .../Form/ChoiceList/EntityChoiceList.php | 3 +++ .../Form/ChoiceList/EntityChoiceListTest.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php index 6ad960bff19a..2debb97e25e2 100644 --- a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php +++ b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php @@ -175,6 +175,9 @@ public function getChoicesForValues(array $values) // Optimize performance in case we have an entity loader and // a single-field identifier if (count($this->identifier) === 1 && $this->entityLoader) { + if (empty($values)) { + return array(); + } return $this->entityLoader->getEntitiesByIds(current($this->identifier), $values); } diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php index e0bc7f67636e..97867e759125 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php @@ -11,6 +11,7 @@ namespace Symfony\Bridge\Doctrine\Tests\Form\ChoiceList; +use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader; use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase; use Symfony\Bridge\Doctrine\Tests\Fixtures\ItemGroupEntity; use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity; @@ -250,4 +251,19 @@ public function testPossibleToProvideShorthandEntityName() $this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2))); $this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2))); } + + // Ticket #3446 + public function testGetEmptyArrayChoicesForEmptyValues() + { + $qb = $this->em->createQueryBuilder()->select('s')->from(self::SINGLE_IDENT_CLASS, 's'); + $entityLoader = new ORMQueryBuilderLoader($qb); + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_IDENT_CLASS, + null, + $entityLoader + ); + + $this->assertEquals(array(), $choiceList->getChoicesForValues(array())); + } }