Skip to content

Commit b55b65a

Browse files
Merge branch '4.4' into 5.0
* 4.4: (21 commits) fix merge CS [FrameworkBundle][ContainerLintCommand] Improve messages when the kernel or the container is not supported [Serializer] Skip uninitialized (PHP 7.4) properties in PropertyNormalizer and ObjectNormalizer stop using deprecated Doctrine persistence classes [Cache] Fix wrong classname in deprecation message Fix regex lookahead syntax in ApplicationTest Fixed syntax in comment [SecurityBundle][FirewallMap] Remove unused property [Messenger][AMQP] Use delivery_mode=2 by default [FrameworkBundle][DependencyInjection] Skip removed ids in the lint container command and its associated pass [SECURITY] Revert "AbstractAuthenticationListener.php error instead info. Rebase of #28462" [FrameworkBundle][Secrets] Hook configured local dotenv file [DI] Improve performance of processDefinition fix redis multi host dsn not recognized fix constructor argument type declaration Fix invalid Windows path normalization [Validator][ConstraintValidator] Safe fail on invalid timezones [DoctrineBridge] Fixed submitting invalid ids when using queries with limit [FrameworkBundle] Add info & example to auto_mapping config ...
2 parents 3af776c + 723a46b commit b55b65a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public function getEntities()
5050
*/
5151
public function getEntitiesByIds(string $identifier, array $values)
5252
{
53+
if (null !== $this->queryBuilder->getMaxResults() || null !== $this->queryBuilder->getFirstResult()) {
54+
// an offset or a limit would apply on results including the where clause with submitted id values
55+
// that could make invalid choices valid
56+
$choices = [];
57+
$metadata = $this->queryBuilder->getEntityManager()->getClassMetadata(current($this->queryBuilder->getRootEntities()));
58+
59+
foreach ($this->getEntities() as $entity) {
60+
if (\in_array(current($metadata->getIdentifierValues($entity)), $values, true)) {
61+
$choices[] = $entity;
62+
}
63+
}
64+
65+
return $choices;
66+
}
67+
5368
$qb = clone $this->queryBuilder;
5469
$alias = current($qb->getRootAliases());
5570
$parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;

Tests/Form/Type/EntityTypeTest.php

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

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

0 commit comments

Comments
 (0)