Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9641c55

Browse filesBrowse files
committed
merged branch RapotOR/2.0-PR2504-squashed (PR symfony#2868)
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.
2 parents 12ea756 + 4d64d90 commit 9641c55
Copy full SHA for 9641c55

File tree

Expand file treeCollapse file tree

3 files changed

+28
-4
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+28
-4
lines changed

‎src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class EntityChoiceList extends ArrayChoiceList
9090
* @param QueryBuilder|\Closure $queryBuilder An optional query builder
9191
* @param array|\Closure $choices An array of choices or a function returning an array
9292
*/
93-
public function __construct(EntityManager $em, $class, $property = null, $queryBuilder = null, $choices = array())
93+
public function __construct(EntityManager $em, $class, $property = null, $queryBuilder = null, $choices = null)
9494
{
9595
// If a query builder was passed, it must be a closure or QueryBuilder
9696
// instance
@@ -118,7 +118,11 @@ public function __construct(EntityManager $em, $class, $property = null, $queryB
118118
$this->propertyPath = new PropertyPath($property);
119119
}
120120

121-
parent::__construct($choices);
121+
if (!is_array($choices) && !$choices instanceof \Closure && !is_null($choices)) {
122+
throw new UnexpectedTypeException($choices, 'array or \Closure or null');
123+
}
124+
125+
$this->choices = $choices;
122126
}
123127

124128
/**
@@ -136,7 +140,7 @@ protected function load()
136140
{
137141
parent::load();
138142

139-
if ($this->choices) {
143+
if (is_array($this->choices)) {
140144
$entities = $this->choices;
141145
} else if ($qb = $this->queryBuilder) {
142146
$entities = $qb->getQuery()->execute();

‎src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getDefaultOptions(array $options)
4747
'class' => null,
4848
'property' => null,
4949
'query_builder' => null,
50-
'choices' => array(),
50+
'choices' => null,
5151
);
5252

5353
$options = array_replace($defaultOptions, $options);

‎tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ public function testFlattenedChoicesAreManaged()
8888
$this->assertSame(array(1 => 'Foo', 2 => 'Bar'), $choiceList->getChoices());
8989
}
9090

91+
public function testEmptyChoicesAreManaged()
92+
{
93+
$entity1 = new SingleIdentEntity(1, 'Foo');
94+
$entity2 = new SingleIdentEntity(2, 'Bar');
95+
96+
// Persist for managed state
97+
$this->em->persist($entity1);
98+
$this->em->persist($entity2);
99+
100+
$choiceList = new EntityChoiceList(
101+
$this->em,
102+
self::SINGLE_IDENT_CLASS,
103+
'name',
104+
null,
105+
array()
106+
);
107+
108+
$this->assertSame(array(), $choiceList->getChoices());
109+
}
110+
91111
public function testNestedChoicesAreManaged()
92112
{
93113
$entity1 = new SingleIdentEntity(1, 'Foo');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.