8000 [DoctrineBridge] Fixed submitting invalid ids when using queries with… · symfony/symfony@61b4aac · GitHub
[go: up one dir, main page]

Skip to content

Commit 61b4aac

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

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,32 @@ 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+
,
975+
'choice_label' => 'name',
976+
]);
977+
978+
$field->submit('3');
979+
980+
$this->assertFalse($field->isSynchronized());
981+
$this->assertNull($field->getData());
982+
}
983+
958984
public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleAssocIdentifier()
959985
{
960986
$innerEntity1 = new SingleIntIdNoToStringEntity(1, 'InFoo');
@@ -1007,6 +1033,8 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingle
10071033
$this->assertNull($field->getData());
10081034
}
10091035

1036+
1037+
10101038
public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompositeIdentifier()
10111039
{
10121040
$entity1 = new CompositeIntIdEntity(10, 20, 'Foo');

0 commit comments

Comments
 (0)
0