Skip to content

Commit

Permalink
merged branch RapotOR/2.0-PR2504-squashed (PR symfony#2868)
Browse files Browse the repository at this point in the history
Commits
-------

4d64d90 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.

Discussion
----------

[Doctrine][Bridge] EntityType: Allow empty result; default `choices` value changed to null

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
References the following tickets: symfony#2504

- 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.

I squashed commits from PR symfony#2504 as requested.
  • Loading branch information
fabpot committed Dec 13, 2011
2 parents 12ea756 + 4d64d90 commit 9641c55
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 @@ -90,7 +90,7 @@ class EntityChoiceList extends ArrayChoiceList
* @param QueryBuilder|\Closure $queryBuilder An optional query builder
* @param array|\Closure $choices An array of choices or a function returning an array
*/
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 @@ -118,7 +118,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 @@ -136,7 +140,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 @@ -47,7 +47,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 9641c55

Please sign in to comment.