[DoctrineBridge] Fixed submitting invalid ids when using queries with limit
This commit is contained in:
parent
bf877b811c
commit
09bb1e49d8
@ -55,6 +55,21 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
|
|||||||
*/
|
*/
|
||||||
public function getEntitiesByIds($identifier, array $values)
|
public function getEntitiesByIds($identifier, array $values)
|
||||||
{
|
{
|
||||||
|
if (null !== $this->queryBuilder->getMaxResults() || null !== $this->queryBuilder->getFirstResult()) {
|
||||||
|
// an offset or a limit would apply on results including the where clause with submitted id values
|
||||||
|
// that could make invalid choices valid
|
||||||
|
$choices = [];
|
||||||
|
$metadata = $this->queryBuilder->getEntityManager()->getClassMetadata(current($this->queryBuilder->getRootEntities()));
|
||||||
|
|
||||||
|
foreach ($this->getEntities() as $entity) {
|
||||||
|
if (\in_array(current($metadata->getIdentifierValues($entity)), $values, true)) {
|
||||||
|
$choices[] = $entity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $choices;
|
||||||
|
}
|
||||||
|
|
||||||
$qb = clone $this->queryBuilder;
|
$qb = clone $this->queryBuilder;
|
||||||
$alias = current($qb->getRootAliases());
|
$alias = current($qb->getRootAliases());
|
||||||
$parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;
|
$parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;
|
||||||
|
@ -955,6 +955,31 @@ class EntityTypeTest extends BaseTypeTest
|
|||||||
$this->assertNull($field->getData());
|
$this->assertNull($field->getData());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifierWithLimit()
|
||||||
|
{
|
||||||
|
$entity1 = new SingleIntIdEntity(1, 'Foo');
|
||||||
|
$entity2 = new SingleIntIdEntity(2, 'Bar');
|
||||||
|
$entity3 = new SingleIntIdEntity(3, 'Baz');
|
||||||
|
|
||||||
|
$this->persist([$entity1, $entity2, $entity3]);
|
||||||
|
|
||||||
|
$repository = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
|
||||||
|
|
||||||
|
$field = $this->factory->createNamed('name', static::TESTED_TYPE, null, [
|
||||||
|
'em' => 'default',
|
||||||
|
'class' => self::SINGLE_IDENT_CLASS,
|
||||||
|
'query_builder' => $repository->createQueryBuilder('e')
|
||||||
|
->where('e.id IN (1, 2, 3)')
|
||||||
|
->setMaxResults(1),
|
||||||
|
'choice_label' => 'name',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$field->submit('3');
|
||||||
|
|
||||||
|
$this->assertFalse($field->isSynchronized());
|
||||||
|
$this->assertNull($field->getData());
|
||||||
|
}
|
||||||
|
|
||||||
public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleAssocIdentifier()
|
public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleAssocIdentifier()
|
||||||
{
|
{
|
||||||
$innerEntity1 = new SingleIntIdNoToStringEntity(1, 'InFoo');
|
$innerEntity1 = new SingleIntIdNoToStringEntity(1, 'InFoo');
|
||||||
|
Reference in New Issue
Block a user