Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 02a883b

Browse files
committed
[DoctrineBridge] Fixed submitting invalid ids when using queries with limit
1 parent bf877b8 commit 02a883b

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ public function getEntities()
5555
*/
5656
public function getEntitiesByIds($identifier, array $values)
5757
{
58+
if (null !== $this->queryBuilder->getMaxResults()) {
59+
$metadata = $this->queryBuilder->getEntityManager()->getClassMetadata(current($this->queryBuilder->getRootEntities()));
60+
61+
$choices = $unordered = [];
62+
63+
foreach ($this->getEntities() as $entity) {
64+
$id = current($metadata->getIdentifierValues($entity));
65+
if (false !== $i = array_search($id, $values, true)) {
66+
$choices[$i] = $entity;
67+
}
68+
}
69+
foreach ($values as $i => $value) {
70+
if (isset($unordered[$i])) {
71+
$choices[$i] = $unordered[$i];
72+
}
73+
}
74+
75+
return $choices;
76+
}
77+
5878
$qb = clone $this->queryBuilder;
5979
$alias = current($qb->getRootAliases());
6080
$parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,31 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifie
955955
$this->assertNull($field->getData());
956956
}
957957

958+
public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifierWithLimit()
959+
{
960+
$entity1 = new SingleIntIdEntity(1, 'Foo');
961+
$entity2 = new SingleIntIdEntity(2, 'Bar');
962+
$entity3 = new SingleIntIdEntity(3, 'Baz');
963+
964+
$this->persist([$entity1, $entity2, $entity3]);
965+
966+
$repository = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
967+
968+
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
969+
'em' => 'default',
970+
'class' => self::SINGLE_IDENT_CLASS,
971+
'query_builder' => $repository->createQueryBuilder('e')
972+
->where('e.id IN (1, 2, 3)')
973+
->setMaxResults(1),
974+
'choice_label' => 'name',
975+
]);
976+
977+
$field->submit('3');
978+
979+
$this->assertFalse($field->isSynchronized());
980+
$this->assertNull($field->getData());
981+
}
982+
958983
public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleAssocIdentifier()
959984
{
960985
$innerEntity1 = new SingleIntIdNoToStringEntity(1, 'InFoo');

0 commit comments

Comments
 (0)