[Form] Fixed EntityChoiceList when loading objects with negative integer IDs
This commit is contained in:
parent
cd04bd7a12
commit
55aa0120cf
@ -329,7 +329,7 @@ class EntityChoiceList extends ObjectChoiceList
|
|||||||
protected function createIndex($entity)
|
protected function createIndex($entity)
|
||||||
{
|
{
|
||||||
if ($this->idAsIndex) {
|
if ($this->idAsIndex) {
|
||||||
return current($this->getIdentifierValues($entity));
|
return $this->fixIndex(current($this->getIdentifierValues($entity)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::createIndex($entity);
|
return parent::createIndex($entity);
|
||||||
@ -355,6 +355,23 @@ class EntityChoiceList extends ObjectChoiceList
|
|||||||
return parent::createValue($entity);
|
return parent::createValue($entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function fixIndex($index)
|
||||||
|
{
|
||||||
|
$index = parent::fixIndex($index);
|
||||||
|
|
||||||
|
// If the ID is a single-field integer identifier, it is used as
|
||||||
|
// index. Replace any leading minus by underscore to make it a valid
|
||||||
|
// form name.
|
||||||
|
if ($this->idAsIndex && $index < 0) {
|
||||||
|
$index = strtr($index, '-', '_');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $index;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the list with entities.
|
* Loads the list with entities.
|
||||||
*/
|
*/
|
||||||
|
@ -312,4 +312,47 @@ class EntityChoiceListTest extends DoctrineOrmTestCase
|
|||||||
|
|
||||||
$this->assertSame(array(0 => $entity1, 1 => $entity2), $choiceList->getChoices());
|
$this->assertSame(array(0 => $entity1, 1 => $entity2), $choiceList->getChoices());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMinusReplacedByUnderscoreInNegativeIntIds()
|
||||||
|
{
|
||||||
|
$entity1 = new SingleIdentEntity(-1, 'Foo');
|
||||||
|
$entity2 = new SingleIdentEntity(1, 'Bar');
|
||||||
|
|
||||||
|
// Persist for managed state
|
||||||
|
$this->em->persist($entity1);
|
||||||
|
$this->em->persist($entity2);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$choiceList = new EntityChoiceList(
|
||||||
|
$this->em,
|
||||||
|
self::SINGLE_IDENT_CLASS,
|
||||||
|
'name'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame(array('_1' => $entity1, 1 => $entity2), $choiceList->getChoices());
|
||||||
|
$this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2)));
|
||||||
|
$this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMinusReplacedByUnderscoreIfNotLoaded()
|
||||||
|
{
|
||||||
|
$entity1 = new SingleIdentEntity(-1, 'Foo');
|
||||||
|
$entity2 = new SingleIdentEntity(1, 'Bar');
|
||||||
|
|
||||||
|
// Persist for managed state
|
||||||
|
$this->em->persist($entity1);
|
||||||
|
$this->em->persist($entity2);
|
||||||
|
$this->em->flush();
|
||||||
|
|
||||||
|
$choiceList = new EntityChoiceList(
|
||||||
|
$this->em,
|
||||||
|
self::SINGLE_IDENT_CLASS,
|
||||||
|
'name'
|
||||||
|
);
|
||||||
|
|
||||||
|
// no getChoices()!
|
||||||
|
|
||||||
|
$this->assertSame(array('_1', 1), $choiceList->getIndicesForChoices(array($entity1, $entity2)));
|
||||||
|
$this->assertSame(array('_1', 1), $choiceList->getIndicesForValues(array('-1', '1')));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user