Skip to content

Commit 658c714

Browse files
committed
bug #34900 [DoctrineBridge] Fixed submitting invalid ids when using queries with limit (HeahDude)
This PR was merged into the 3.4 branch. Discussion ---------- [DoctrineBridge] Fixed submitting invalid ids when using queries with limit | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #31559 <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | ~ <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch master. --> Commits ------- 09bb1e4 [DoctrineBridge] Fixed submitting invalid ids when using queries with limit
2 parents 8f42a76 + 09bb1e4 commit 658c714

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ public function getEntities()
5555
*/
5656
public function getEntitiesByIds($identifier, array $values)
5757
{
58+
if (null !== $this->queryBuilder->getMaxResults() || null !== $this->queryBuilder->getFirstResult()) {
59+
// an offset or a limit would apply on results including the where clause with submitted id values
60+
// that could make invalid choices valid
61+
$choices = [];
62+
$metadata = $this->queryBuilder->getEntityManager()->getClassMetadata(current($this->queryBuilder->getRootEntities()));
63+
64+
foreach ($this->getEntities() as $entity) {
65+
if (\in_array(current($metadata->getIdentifierValues($entity)), $values, true)) {
66+
$choices[] = $entity;
67+
}
68+
}
69+
70+
return $choices;
71+
}
72+
5873
$qb = clone $this->queryBuilder;
5974
$alias = current($qb->getRootAliases());
6075
$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
@@ -956,6 +956,31 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifie
956956
$this->assertNull($field->getData());
957957
}
958958

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy