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 1656bf3

Browse filesBrowse files
committed
merged branch bschussek/issue5205 (PR symfony#5277)
Commits ------- 3ad3876 [Form] Fixed support for preferred choices in "entity" type Discussion ---------- [Form] Fixed support for preferred choices in "entity" type Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: symfony#5205 Todo: -
2 parents c15a3a3 + 3ad3876 commit 1656bf3
Copy full SHA for 1656bf3

File tree

Expand file treeCollapse file tree

4 files changed

+65
-5
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+65
-5
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
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ class EntityChoiceList extends ObjectChoiceList
7676
*/
7777
private $loaded = false;
7878

79+
/**
80+
* The preferred entities.
81+
*
82+
* @var array
83+
*/
84+
private $preferredEntities = array();
85+
7986
/**
8087
* Creates a new entity choice list.
8188
*
@@ -88,13 +95,14 @@ class EntityChoiceList extends ObjectChoiceList
8895
* to group the choices. Only allowed if
8996
* the choices are given as flat array.
9097
*/
91-
public function __construct(ObjectManager $manager, $class, $labelPath = null, EntityLoaderInterface $entityLoader = null, $entities = null, $groupPath = null)
98+
public function __construct(ObjectManager $manager, $class, $labelPath = null, EntityLoaderInterface $entityLoader = null, $entities = null, array $preferredEntities = array(), $groupPath = null)
9299
{
93100
$this->em = $manager;
94101
$this->entityLoader = $entityLoader;
95102
$this->classMetadata = $manager->getClassMetadata($class);
96103
$this->class = $this->classMetadata->getName();
97104
$this->loaded = is_array($entities) || $entities instanceof \Traversable;
105+
$this->preferredEntities = $preferredEntities;
98106

99107
$identifier = $this->classMetadata->getIdentifierFieldNames();
100108

@@ -113,7 +121,7 @@ public function __construct(ObjectManager $manager, $class, $labelPath = null, E
113121
$entities = array();
114122
}
115123

116-
parent::__construct($entities, $labelPath, array(), $groupPath);
124+
parent::__construct($entities, $labelPath, $preferredEntities, $groupPath);
117125
}
118126

119127
/**
@@ -359,8 +367,7 @@ private function load()
359367

360368
try {
361369
// The second parameter $labels is ignored by ObjectChoiceList
362-
// The third parameter $preferredChoices is currently not supported
363-
parent::initialize($entities, array(), array());
370+
parent::initialize($entities, array(), $this->preferredEntities);
364371
} catch (StringCastException $e) {
365372
throw new StringCastException(str_replace('argument $labelPath', 'option "property"', $e->getMessage()), null, $e);
366373
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
8181
});
8282
}
8383

84+
$preferredChoiceHashes = $options['preferred_choices'];
85+
86+
if (is_array($preferredChoiceHashes)) {
87+
array_walk_recursive($preferredChoiceHashes, function ($value) {
88+
return spl_object_hash($value);
89+
});
90+
}
91+
8492
// Support for custom loaders (with query builders)
8593
$loaderHash = is_object($options['loader'])
8694
? spl_object_hash($options['loader'])
@@ -97,6 +105,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
97105
$propertyHash,
98106
$loaderHash,
99107
$choiceHashes,
108+
$preferredChoiceHashes,
100109
$groupByHash
101110
)));
102111

@@ -107,6 +116,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
107116
$options['property'],
108117
$options['loader'],
109118
$options['choices'],
119+
$options['preferred_choices'],
110120
$options['group_by']
111121
);
112122
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/EntityChoiceListTest.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ public function testNestedChoicesAreManaged()
181181
array(
182182
'group1' => array($entity1),
183183
'group2' => array($entity2),
184-
)
184+
),
185+
array()
185186
);
186187

187188
$this->assertSame(array(1 => $entity1, 2 => $entity2), $choiceList->getChoices());
@@ -214,6 +215,7 @@ public function testGroupBySupportsString()
214215
$item3,
215216
$item4,
216217
),
218+
array(),
217219
'groupName'
218220
);
219221

@@ -242,6 +244,7 @@ public function testGroupByInvalidPropertyPathReturnsFlatChoices()
242244
$item1,
243245
$item2,
244246
),
247+
array(),
245248
'child.that.does.not.exist'
246249
);
247250

@@ -267,6 +270,7 @@ public function testPossibleToProvideShorthandEntityName()
267270
null,
268271
null,
269272
null,
273+
array(),
270274
null
271275
);
272276

‎src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,45 @@ public function testGroupByChoices()
536536
), $field->createView()->vars['choices']);
537537
}
538538

539+
public function testPreferredChoices()
540+
{
541+
$entity1 = new SingleIdentEntity(1, 'Foo');
542+
$entity2 = new SingleIdentEntity(2, 'Bar');
543+
$entity3 = new SingleIdentEntity(3, 'Baz');
544+
545+
$this->persist(array($entity1, $entity2, $entity3));
546+
547+
$field = $this->factory->createNamed('name', 'entity', null, array(
548+
'em' => 'default',
549+
'class' => self::SINGLE_IDENT_CLASS,
550+
'preferred_choices' => array($entity3, $entity2),
551+
'property' => 'name',
552+
));
553+
554+
$this->assertEquals(array(3 => new ChoiceView($entity3, '3', 'Baz'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['preferred_choices']);
555+
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo')), $field->createView()->vars['choices']);
556+
}
557+
558+
public function testOverrideChoicesWithPreferredChoices()
559+
{
560+
$entity1 = new SingleIdentEntity(1, 'Foo');
561+
$entity2 = new SingleIdentEntity(2, 'Bar');
562+
$entity3 = new SingleIdentEntity(3, 'Baz');
563+
564+
$this->persist(array($entity1, $entity2, $entity3));
565+
566+
$field = $this->factory->createNamed('name', 'entity', null, array(
567+
'em' => 'default',
568+
'class' => self::SINGLE_IDENT_CLASS,
569+
'choices' => array($entity2, $entity3),
570+
'preferred_choices' => array($entity3),
571+
'property' => 'name',
572+
));
573+
574+
$this->assertEquals(array(3 => new ChoiceView($entity3, '3', 'Baz')), $field->createView()->vars['preferred_choices']);
575+
$this->assertEquals(array(2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
576+
}
577+
539578
public function testDisallowChoicesThatAreNotIncluded_choicesSingleIdentifier()
540579
{
541580
$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.