merged branch bschussek/issue6610 (PR #6617)

This PR was merged into the 2.1 branch.

Commits
-------

55aa012 [Form] Fixed EntityChoiceList when loading objects with negative integer IDs

Discussion
----------

[Form] Fixed EntityChoiceList when loading objects with negative integer IDs

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #6610
Todo: -
License of the code: MIT
Documentation PR: -

---------------------------------------------------------------------------

by stof at 2013-01-08T10:21:57Z

wouldn't you need to do the opposite replacement when loading the result ? And shouldn't it be done in the propel bridge too ?

---------------------------------------------------------------------------

by bschussek at 2013-01-08T10:25:29Z

@stof No, indices aren't used for loading the result, just values. Yes this should be done for the propel bridge, but that bridge is missing even the tests for the ID-as-identifier usage. Do you want to create a PR?
This commit is contained in:
Fabien Potencier 2013-01-08 13:06:20 +01:00
commit 87160a7051
2 changed files with 61 additions and 1 deletions

View File

@ -329,7 +329,7 @@ class EntityChoiceList extends ObjectChoiceList
protected function createIndex($entity)
{
if ($this->idAsIndex) {
return current($this->getIdentifierValues($entity));
return $this->fixIndex(current($this->getIdentifierValues($entity)));
}
return parent::createIndex($entity);
@ -355,6 +355,23 @@ class EntityChoiceList extends ObjectChoiceList
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.
*/

View File

@ -312,4 +312,47 @@ class EntityChoiceListTest extends DoctrineOrmTestCase
$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')));
}
}