Skip to content

Commit

Permalink
Allow empty result; change default *choices* value to **null** instea…
Browse files Browse the repository at this point in the history
…d 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.
  • Loading branch information
RapotOR committed Dec 13, 2011
1 parent 323f80d commit 4d64d90
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php
Expand Up @@ -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);
Expand Down
Expand Up @@ -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');
Expand Down

0 comments on commit 4d64d90

Please sign in to comment.