Skip to content

Commit

Permalink
bug #17694 [2.7] [DoctrineBridge] [Form] fix choice_value in EntityTy…
Browse files Browse the repository at this point in the history
…pe (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
  • Loading branch information
fabpot committed Feb 15, 2016
2 parents dc59e42 + 2336d5c commit 52343b0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Expand Up @@ -146,7 +146,7 @@ public function loadChoicesForValues(array $values, $value = null)

// 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();
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Expand Up @@ -740,6 +740,55 @@ public function testOverrideChoices()
$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');
Expand Down

0 comments on commit 52343b0

Please sign in to comment.