Repository navigation
[Pagination] Fix the Doctrine ORM count when a parameter is only used by the SELECT or the ORDER BY - #4193
Merged
Conversation
ker0x
force-pushed
the
pagination-orm-count-unused-parameters
branch
from
November 3, 2026 17:53
7c0996d to
39207af
Compare
… by the SELECT or the ORDER BY | Q | A | -------------- | --- | Bug fix? | yes | New feature? | no | Deprecations? | no | Documentation? | no | Issues | Fix symfony#4191 | License | MIT `DoctrineOrmAdapter::count()` replaces the SELECT with `COUNT(alias)` and removes the ORDER BY, but it keeps every bound parameter. When a parameter is only used by the removed parts, for example a scalar subquery or a `HIDDEN` sort key added with `addSelect()`, or a `CASE` expression in `orderBy()`, the count query no longer uses it and Doctrine rejects it: ```php $queryBuilder ->select('a') ->addSelect('(SELECT COUNT(b.id) FROM App\Entity\Book b WHERE b.author = a AND b.price > :minPrice) AS expensiveBooks') ->from(Author::class, 'a') ->setParameter('minPrice', 10); $pagination->getItems(); // works $pagination->getTotalItems(); // QueryException: Too many parameters: the query defines 0 parameters and you bound 1 ``` The count query now keeps only the parameters that its DQL still uses. It does this the same way as Doctrine's own `Paginator` (`unbindUnusedQueryParams()`): it parses the query and reads the parameter mappings. Parameters used by the WHERE clause, including ones shared with the SELECT, stay bound. The query is parsed only when it has parameters. | Count query | Before | After | | ---------------------------------------------------- | ----------------- | ----------- | | Parameter only used by a subquery in the SELECT | `QueryException` | Total | | Parameter only used by the ORDER BY | `QueryException` | Total | | Parameter used by the WHERE clause | Total | Total | The DBAL adapter is not affected: it wraps the whole query in a subquery, so every parameter stays in use. Checks: - The three new tests (named parameter in the SELECT, parameter in the ORDER BY, positional parameters) fail on 3.x with the `Too many parameters` error, and pass with the fix. - Also probed: a parameter shared by the SELECT and the WHERE clause, and a collection join (`COUNT(DISTINCT)`) with a SELECT-only parameter. - The full Pagination suite passes, except `UXPaginationExtensionTest::testNamedPaginatorsInheritRootConfigurationAndOverrideSelectedValues`, which fails the same way on 3.x with symfony/dependency-injection 8.2.x-dev. - The adapter tests pass with `--prefer-lowest` (doctrine/orm 3.0.0-beta1, doctrine/dbal 4.4.0).
Kocal
force-pushed
the
pagination-orm-count-unused-parameters
branch
from
November 3, 2026 19:50
39207af to
7e389c6
Compare
Kocal
approved these changes
Nov 3, 2026
Member
|
Thank you @ker0x. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DoctrineOrmAdapter::count()replaces the SELECT withCOUNT(alias)and removes the ORDER BY, but it keeps every bound parameter. When a parameter is only used by the removed parts, for example a scalar subquery or aHIDDENsort key added withaddSelect(), or aCASEexpression inorderBy(), the count query no longer uses it and Doctrine rejects it:The count query now keeps only the parameters that its DQL still uses. It does this the same way as Doctrine's own
Paginator(unbindUnusedQueryParams()): it parses the query and reads the parameter mappings. Parameters used by the WHERE clause, including ones shared with the SELECT, stay bound. The query is parsed only when it has parameters.QueryExceptionQueryExceptionThe DBAL adapter is not affected: it wraps the whole query in a subquery, so every parameter stays in use.
Checks:
Too many parameterserror, and pass with the fix.COUNT(DISTINCT)) with a SELECT-only parameter.UXPaginationExtensionTest::testNamedPaginatorsInheritRootConfigurationAndOverrideSelectedValues, which fails the same way on 3.x with symfony/dependency-injection 8.2.x-dev.--prefer-lowest(doctrine/orm 3.0.0-beta1, doctrine/dbal 4.4.0).