Skip to content

Paginator returns incorrect IDs in getIterator when a subquery WHERE is present #12255

@pkly

Description

@pkly

Bug Report

Q A
Version 3.5.3

Observed on 3.5.2 as well, possibly before then.

Summary

We're using doctrine alongside EasyAdmin and we observed funky behavior when adding a complex filter on one of our views which then gets passed into Paginator.

The query in question can be mostly boiled down to something like this:

$query = $repo->createQueryBuilder('entity');
$subQuery = $repo->createQueryBuilder('filter');

$subQuery->select('filter.id')
    ->innerJoin('filter.identifierUses', 'identifierUseFilter')
    ->innerJoin('identifierUseFilter.identifier', 'identifierFilter')
    ->where('identifierFilter.code = :code');

$query->andWhere('entity.id IN ('.$subQuery->getDql().')');

Generated DQL in Paginator:

SELECT entity FROM MyEntity entity WHERE entity.id IN((SELECT filter.id FROM MyEntity filter INNER JOIN filter.identifierUses identifierUseFilter INNER JOIN identifierUseFilter.identifier identifierFilter WHERE identifierFilter.code = :code)) AND entity.source = :source AND entity.isArchived = 0 ORDER BY entity.id DESC

Produced SQL on count query from Paginator:

SELECT DISTINCT s0_.id AS id_0, s0_.id AS id_1
FROM my_entity s0_
WHERE s0_.id IN (
(SELECT s1_.id FROM my_entity s1_ INNER JOIN identifier_uses s2_ ON s1_.id = s2_.entity_id INNER JOIN identifiers s3_ ON s2_.identifier_id = s3_.id AND (s3_.deleted_at IS NULL) WHERE s3_.code = ?)
) AND s0_.source = ? AND s0_.is_archived = 0
ORDER BY s0_.id DESC LIMIT 20

Later produces incorrect ids, as only 1 id is expected, but 2 are returned, even though the subquery by itself is correct, I believe the error is caused by multiple columns being present in select for this query, while paginator only expects one. It seems like it's caused by LimitSubqueryWalker adding a second select statement (at least from what I checked)

Current behavior

Incorrect IDs are returned by Paginator (variable: $foundIdRows in getIterator)

Expected behavior

Correct IDs are returned.

How to reproduce

I'll try to provide a repository with example data and entities shortly if needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions