Merge branch '2.7' into 2.8
* 2.7: fix choice_value option in EntityType and add some tests
This commit is contained in:
commit
e692752f08
@ -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();
|
||||
|
@ -766,6 +766,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');
|
||||
|
Reference in New Issue
Block a user