Skip to content

Commit 1b6029b

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: fix choice_value option in EntityType and add some tests
2 parents c79a2b5 + fcfbf28 commit 1b6029b

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Form/ChoiceList/DoctrineChoiceLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function loadChoicesForValues(array $values, $value = null)
146146

147147
// Optimize performance in case we have an object loader and
148148
// a single-field identifier
149-
if (!$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
149+
if (null === $value && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
150150
$unorderedObjects = $this->objectLoader->getEntitiesByIds($this->idReader->getIdField(), $values);
151151
$objectsById = array();
152152
$objects = array();

Tests/Form/Type/EntityTypeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,55 @@ public function testOverrideChoices()
766766
$this->assertSame('2', $field->getViewData());
767767
}
768768

769+
public function testOverrideChoicesValues()
770+
{
771+
$entity1 = new SingleIntIdEntity(1, 'Foo');
772+
$entity2 = new SingleIntIdEntity(2, 'Bar');
773+
774+
$this->persist(array($entity1, $entity2));
775+
776+
$field = $this->factory->createNamed('name', 'entity', null, array(
777+
'em' => 'default',
778+
'class' => self::SINGLE_IDENT_CLASS,
779+
'choice_label' => 'name',
780+
'choice_value' => 'name',
781+
));
782+
783+
$field->submit('Bar');
784+
785+
$this->assertEquals(array('Foo' => new ChoiceView($entity1, 'Foo', 'Foo'), 'Bar' => new ChoiceView($entity2, 'Bar', 'Bar')), $field->createView()->vars['choices']);
786+
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
787+
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
788+
$this->assertSame('Bar', $field->getViewData());
789+
}
790+
791+
public function testOverrideChoicesValuesWithCallable()
792+
{
793+
$entity1 = new GroupableEntity(1, 'Foo', 'BazGroup');
794+
$entity2 = new GroupableEntity(2, 'Bar', 'BooGroup');
795+
796+
$this->persist(array($entity1, $entity2));
797+
798+
$field = $this->factory->createNamed('name', 'entity', null, array(
799+
'em' => 'default',
800+
'class' => self::ITEM_GROUP_CLASS,
801+
'choice_label' => 'name',
802+
'choice_value' => function (GroupableEntity $entity) {
803+
return $entity->groupName.'/'.$entity->name;
804+
},
805+
));
806+
807+
$field->submit('BooGroup/Bar');
808+
809+
$this->assertEquals(array(
810+
'BazGroup/Foo' => new ChoiceView($entity1, 'BazGroup/Foo', 'Foo'),
811+
'BooGroup/Bar' => new ChoiceView($entity2, 'BooGroup/Bar', 'Bar'),
812+
), $field->createView()->vars['choices']);
813+
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
814+
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
815+
$this->assertSame('BooGroup/Bar', $field->getViewData());
816+
}
817+
769818
public function testGroupByChoices()
770819
{
771820
$item1 = new GroupableEntity(1, 'Foo', 'Group1');

0 commit comments

Comments
 (0)