Skip to content

Commit 52343b0

Browse files
committed
bug #17694 [2.7] [DoctrineBridge] [Form] fix choice_value in EntityType (HeahDude)
This PR was merged into the 2.7 branch. Discussion ---------- [2.7] [DoctrineBridge] [Form] fix choice_value in EntityType | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17693, #17271, #13964 | License | MIT | Doc PR | symfony/symfony-docs#6260 Commits ------- 2336d5c fix choice_value option in EntityType and add some tests
2 parents dc59e42 + 2336d5c commit 52343b0

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Symfony/Bridge/Doctrine/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();

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

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

743+
public function testOverrideChoicesValues()
744+
{
745+
$entity1 = new SingleIntIdEntity(1, 'Foo');
746+
$entity2 = new SingleIntIdEntity(2, 'Bar');
747+
748+
$this->persist(array($entity1, $entity2));
749+
750+
$field = $this->factory->createNamed('name', 'entity', null, array(
751+
'em' => 'default',
752+
'class' => self::SINGLE_IDENT_CLASS,
753+
'choice_label' => 'name',
754+
'choice_value' => 'name',
755+
));
756+
757+
$field->submit('Bar');
758+
759+
$this->assertEquals(array('Foo' => new ChoiceView($entity1, 'Foo', 'Foo'), 'Bar' => new ChoiceView($entity2, 'Bar', 'Bar')), $field->createView()->vars['choices']);
760+
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
761+
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
762+
$this->assertSame('Bar', $field->getViewData());
763+
}
764+
765+
public function testOverrideChoicesValuesWithCallable()
766+
{
767+
$entity1 = new GroupableEntity(1, 'Foo', 'BazGroup');
768+
$entity2 = new GroupableEntity(2, 'Bar', 'BooGroup');
769+
770+
$this->persist(array($entity1, $entity2));
771+
772+
$field = $this->factory->createNamed('name', 'entity', null, array(
773+
'em' => 'default',
774+
'class' => self::ITEM_GROUP_CLASS,
775+
'choice_label' => 'name',
776+
'choice_value' => function (GroupableEntity $entity) {
777+
return $entity->groupName.'/'.$entity->name;
778+
},
779+
));
780+
781+
$field->submit('BooGroup/Bar');
782+
783+
$this->assertEquals(array(
784+
'BazGroup/Foo' => new ChoiceView($entity1, 'BazGroup/Foo', 'Foo'),
785+
'BooGroup/Bar' => new ChoiceView($entity2, 'BooGroup/Bar', 'Bar'),
786+
), $field->createView()->vars['choices']);
787+
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
788+
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
789+
$this->assertSame('BooGroup/Bar', $field->getViewData());
790+
}
791+
743792
public function testGroupByChoices()
744793
{
745794
$item1 = new GroupableEntity(1, 'Foo', 'Group1');

0 commit comments

Comments
 (0)