From 4d64d90f131e57612d398980e31d6adef18bab47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Lahouste?= Date: Fri, 28 Oct 2011 09:38:14 +0300 Subject: [PATCH] Allow empty result; change default *choices* value to **null** instead of **array()**. - added *testEmptyChoicesAreManaged* test - `null` as default value for choices. - is_array() used to test if choices are user-defined. - `null` as default value in __construct too. - `null` as default value for choices in EntityType. --- .../Form/ChoiceList/EntityChoiceList.php | 10 +++++++--- .../Bridge/Doctrine/Form/Type/EntityType.php | 2 +- .../Form/ChoiceList/EntityChoiceListTest.php | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php index e0ac9e9e19fe..708c008a9ef7 100644 --- a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php +++ b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php @@ -81,7 +81,7 @@ class EntityChoiceList extends ArrayChoiceList private $propertyPath; - public function __construct(EntityManager $em, $class, $property = null, $queryBuilder = null, $choices = array()) + public function __construct(EntityManager $em, $class, $property = null, $queryBuilder = null, $choices = null) { // If a query builder was passed, it must be a closure or QueryBuilder // instance @@ -109,7 +109,11 @@ public function __construct(EntityManager $em, $class, $property = null, $queryB $this->propertyPath = new PropertyPath($property); } - parent::__construct($choices); + if (!is_array($choices) && !$choices instanceof \Closure && !is_null($choices)) { + throw new UnexpectedTypeException($choices, 'array or \Closure or null'); + } + + $this->choices = $choices; } /** @@ -127,7 +131,7 @@ protected function load() { parent::load(); - if ($this->choices) { + if (is_array($this->choices)) { $entities = $this->choices; } else if ($qb = $this->queryBuilder) { $entities = $qb->getQuery()->execute(); diff --git a/src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php b/src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php index 693419bf12df..d505541e6a00 100644 --- a/src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php +++ b/src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php @@ -48,7 +48,7 @@ public function getDefaultOptions(array $options) 'class' => null, 'property' => null, 'query_builder' => null, - 'choices' => array(), + 'choices' => null, ); $options = array_replace($defaultOptions, $options); diff --git a/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php b/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php index 7e260a0c6e57..cf6febfa2a5a 100644 --- a/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php +++ b/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php @@ -88,6 +88,26 @@ public function testFlattenedChoicesAreManaged() $this->assertSame(array(1 => 'Foo', 2 => 'Bar'), $choiceList->getChoices()); } + public function testEmptyChoicesAreManaged() + { + $entity1 = new SingleIdentEntity(1, 'Foo'); + $entity2 = new SingleIdentEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_IDENT_CLASS, + 'name', + null, + array() + ); + + $this->assertSame(array(), $choiceList->getChoices()); + } + public function testNestedChoicesAreManaged() { $entity1 = new SingleIdentEntity(1, 'Foo');