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
This commit is contained in:
Fabien Potencier 2016-02-15 09:11:42 +01:00
commit 52343b02b7
2 changed files with 50 additions and 1 deletions

View File

@ -146,7 +146,7 @@ class DoctrineChoiceLoader implements ChoiceLoaderInterface
// Optimize performance in case we have an object loader and
// a single-field identifier
if (!$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
if (null === $value && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
$unorderedObjects = $this->objectLoader->getEntitiesByIds($this->idReader->getIdField(), $values);
$objectsById = array();
$objects = array();

View File

@ -740,6 +740,55 @@ class EntityTypeTest extends TypeTestCase
$this->assertSame('2', $field->getViewData());
}
public function testOverrideChoicesValues()
{
$entity1 = new SingleIntIdEntity(1, 'Foo');
$entity2 = new SingleIntIdEntity(2, 'Bar');
$this->persist(array($entity1, $entity2));
$field = $this->factory->createNamed('name', 'entity', null, array(
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'choice_label' => 'name',
'choice_value' => 'name',
));
$field->submit('Bar');
$this->assertEquals(array('Foo' => new ChoiceView($entity1, 'Foo', 'Foo'), 'Bar' => new ChoiceView($entity2, 'Bar', 'Bar')), $field->createView()->vars['choices']);
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
$this->assertSame('Bar', $field->getViewData());
}
public function testOverrideChoicesValuesWithCallable()
{
$entity1 = new GroupableEntity(1, 'Foo', 'BazGroup');
$entity2 = new GroupableEntity(2, 'Bar', 'BooGroup');
$this->persist(array($entity1, $entity2));
$field = $this->factory->createNamed('name', 'entity', null, array(
'em' => 'default',
'class' => self::ITEM_GROUP_CLASS,
'choice_label' => 'name',
'choice_value' => function (GroupableEntity $entity) {
return $entity->groupName.'/'.$entity->name;
},
));
$field->submit('BooGroup/Bar');
$this->assertEquals(array(
'BazGroup/Foo' => new ChoiceView($entity1, 'BazGroup/Foo', 'Foo'),
'BooGroup/Bar' => new ChoiceView($entity2, 'BooGroup/Bar', 'Bar'),
), $field->createView()->vars['choices']);
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
$this->assertSame('BooGroup/Bar', $field->getViewData());
}
public function testGroupByChoices()
{
$item1 = new GroupableEntity(1, 'Foo', 'Group1');