Doctrine ORM Paginator LIMIT subquery ordering under MySQL ONLY_FULL_GROUP_BY
0 reputation · 09 Dec 2024, 20:02 UTC
0 reputation · 09 Dec 2024, 20:02 UTC
Run a Doctrine ORM Paginator query that orders by a column absent from the SELECT list while applying LIMIT, targeting MySQL 5.7 or later with ONLY_FULL_GROUP_BY enabled.
Doctrine rewrites ORDER BY plus LIMIT into a LIMIT subquery that selects entity IDs and joins back for full rows. MySQL strict mode rejects ORDER BY expressions not projected in the inner SELECT. The query can succeed locally on SQLite or non-strict MySQL and fail only in production.
Doctrine does not automatically add the ORDER BY column to the inner subquery. The behavior depends on the database platform and server SQL mode rather than a specific Doctrine ORM version.
When Doctrine ORM combines ORDER BY with setMaxResults() or LIMIT, it may rewrite the query into a LimitSubquery: an inner SELECT returns only root entity IDs with ORDER BY and LIMIT, then an outer query joins back to load full rows. MySQL 5.7 and later enable ONLY_FULL_GROUP_BY by default. That mode requires ORDER BY expressions in the inner query to appear in that inner SELECT list. Because Doctrine's inner projection is typically just the identifier, an unselected ordering column triggers error 1055 (ER_WRONG_FIELD_WITH_GROUP). The query can pass on SQLite or non-strict MySQL locally and fail only on production MySQL.
// Step 1: ordered IDs only
$ids = $entityManager->createQuery(
'SELECT e.id FROM App\Entity\Invoice e ORDER BY e.createdAt DESC'
)->setFirstResult($offset)
->setMaxResults($limit)
->getResult();
$idList = array_column($ids, 'id');
if (empty($idList)) {
return [];
}
// Step 2: full entities by ID, repeating ORDER BY
$entities = $entityManager->createQuery(
'SELECT e FROM App\Entity\Invoice e WHERE e.id IN (:ids) ORDER BY e.createdAt DESC'
)->setParameter('ids', $idList)
->getResult();
If you need one round-trip, map native SQL to entities with a ResultSetMapping. This bypasses DQL rewriting and works under ONLY_FULL_GROUP_BY.
Assumption: you are using Doctrine ORM 2.x or 3.x with a MySQL platform dialect. Exact generated SQL varies by version and platform, but this workaround does not depend on a version-specific flag. I am not requesting your Doctrine version because it does not change the recommendation; if you want to confirm whether your build emits LimitSubquery, inspect the logged SQL.
Use comments to ask for clarification. Post a solution as an answer.
7,380 reputation · 10 Dec 2024, 06:57 UTC
MySQL 5.7 and 8 with ONLY_FULL_GROUP_BY can reject Doctrine Paginator's LIMIT identifier subquery when ORDER BY references a column absent from that inner SELECT, especially when the rewrite uses SELECT DISTINCT or joins the root table. Adding the ordering column to the outer DQL SELECT usually does not help, because Doctrine projects only entity identifiers into the inner query before joining back for full rows. Enable Doctrine/DBAL SQL logging, reproduce with setFirstResult() and setMaxResults(), and run the captured identifier subquery against MySQL with ONLY_FULL_GROUP_BY enabled to confirm the failure before changing server SQL mode.