mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-2236: SUM(..) with Pagination gives incorrect result #2813
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Jan 11, 2013).
Originally assigned to: @ostrolucky on GitHub.
Jira issue originally created by user olegk:
https://github.com/whiteoctober/Pagerfanta/issues/69
<?php $query = $em->getRepository('M\E\Q') ->createQueryBuilder('q') ->select('q', 'SUM(q.price) AS amount') ->where('q.id IN(19, 20, 22)') ->groupBy('q.customer') ; $pager = new Pagerfanta(new DoctrineORMAdapter($query)); $pager->setMaxPerPage(30); $pager->setCurrentPage($request->query->get('page', 1)); $result = $pager->getCurrentPageResults(); print_r($result[0]['amount']); // 156.71 - Incorrect $result = $query->getQuery()->getResult(); print_r($result[0]['amount']); // 553.47 ?>Sql for the above:
SELECT DISTINCT id0 FROM (SELECT q0_.id AS id0, SUM(q0_.price) AS sclr36 FROM Q q0_ WHERE q0_.id IN (19, 20, 22) GROUP BY q0_.customer_id) dctrn_result LIMIT 30 OFFSET 0
SELECT q0_.id AS id0, SUM(q0_.price) AS sclr36 FROM Q q0_ WHERE q0_.id IN (19, 20, 22) AND q0_.id IN ('19') GROUP BY q0_.customer_id
SELECT q0_.id AS id21, SUM(q0_.price) AS sclr36 FROM Q q0_ WHERE q0_.id IN (19, 20, 22) GROUP BY q0_.customer_id
Sql with fetchJoin = false (new DoctrineORMAdapter($query, false))
SELECT q0_.id AS id0, SUM(q0_.price) AS sclr36 FROM Quote q0_ WHERE q0_.id IN (19, 20, 22) GROUP BY q0_.customer_id LIMIT 30 OFFSET 0
SELECT q0_.id AS id0, SUM(q0_.price) AS sclr36 FROM Quote q0_ WHERE q0_.id IN (19, 20, 22) GROUP BY q0_.customer_id
@doctrinebot commented on GitHub (Feb 9, 2013):
Comment created by @asm89:
Can you also test this with doctrine >= 2.3? The pagination code changed quite a lot.
@doctrinebot commented on GitHub (Feb 10, 2013):
Comment created by olegk:
Looks like no change
composer.json:
"doctrine/orm": "2.3.*",
php composer.phar update
Loading composer repositories with package information
Updating dependencies
Loading from cache
Loading from cache
then cleared cache but result is same
Here's the code
But I understand why that happens, it's due to group by and pagination nature.
The first query returns only one row with id "2", second query should be actually "..WHERE i0_.id IN ('2', '3', '4')"
If I do
I get this sql
I think it should be noted somewhere that if you do groupBy you should set fetchJoin to false?
@doctrinebot commented on GitHub (Feb 10, 2013):
Comment created by @ocramius:
Updating to Documentation issue.
@Ocramius commented on GitHub (Aug 5, 2018):
Handled in #7333