NonUniqueResultException: Doctrine Paginator with join fetch
0 reputation · 08 Oct 2025, 19:08 UTC
When paginating a one-to-many query that uses a join fetch, Doctrine's Paginator throws a NonUniqueResultException because the result set contains duplicate root entities.
The Paginator first executes a separate count query to determine the total number of results, then fetches the requested slice. With a join fetch, Doctrine may either load all rows into memory and then slice, or use a subquery to limit the rows returned by the database. The exact strategy is not documented, and it is unclear how the count query treats duplicate rows.
Because the count query may not use DISTINCT on the root entity, the reported total can be inflated, and deep pagination can become very expensive. It is also unknown whether the Paginator can be configured to enforce distinct counting or to switch between in‑memory and subquery strategies.
1) Does Doctrine’s Paginator perform pagination in memory or via a subquery when a join fetch is present?
2) How does the count query handle duplicate root entities, and can we force it to use DISTINCT?
3) Is there a configuration option to switch between these strategies or to optimize deep pagination?