[DDC-2236] SUM(..) with Pagination gives incorrect result Created: 11/Jan/13 Updated: 10/Feb/13 |
|
| Status: | In Progress |
| Project: | Doctrine 2 - ORM |
| Component/s: | Tools |
| Affects Version/s: | 2.2.3 |
| Fix Version/s: | None |
| Type: | Documentation | Priority: | Minor |
| Reporter: | Oleg | Assignee: | Benjamin Eberlei |
| Resolution: | Unresolved | Votes: | 1 |
| Labels: | paginator | ||
| Environment: |
Linux |
||
| Description |
|
https://github.com/whiteoctober/Pagerfanta/issues/69 <?php $pager = new Pagerfanta(new DoctrineORMAdapter($query)); $result = $pager->getCurrentPageResults(); $result = $query->getQuery()->getResult(); 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 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 |
| Comments |
| Comment by Alexander [ 09/Feb/13 ] |
|
Can you also test this with doctrine >= 2.3? The pagination code changed quite a lot. |
| Comment by Oleg [ 10/Feb/13 ] |
|
Looks like no change composer.json:
then cleared cache but result is same
$query = $this->getDoctrine()->getEntityManager()->getRepository('MyBundle:Invoice')
->createQueryBuilder('q')
->select('q', 'SUM(q.amount) AS amount')
->groupBy('q.customer')
;
95 Connect root@localhost on ** 95 Query SELECT DISTINCT id0 FROM (SELECT i0_.id AS id0, i0_.invoice_num AS invoice_num1, i0_.date AS date2, i0_.amount AS amount3, i0_.vat_amount AS vat_amount4, i0_.amount_paid AS amount_paid5, i0_.md5 AS md56, i0_.is_exported AS is_exported7, i0_.created AS created8, SUM(i0_.amount) AS sclr9 FROM Invoice i0_ GROUP BY i0_.customer_id) dctrn_result LIMIT 30 OFFSET 0 95 Query SELECT i0_.id AS id0, i0_.invoice_num AS invoice_num1, i0_.date AS date2, i0_.amount AS amount3, i0_.vat_amount AS vat_amount4, i0_.amount_paid AS amount_paid5, i0_.md5 AS md56, i0_.is_exported AS is_exported7, i0_.created AS created8, SUM(i0_.amount) AS sclr9, i0_.customer_id AS customer_id10 FROM Invoice i0_ WHERE i0_.id IN ('2') GROUP BY i0_.customer_id 95 Query SELECT i0_.id AS id0, i0_.invoice_num AS invoice_num1, i0_.date AS date2, i0_.amount AS amount3, i0_.vat_amount AS vat_amount4, i0_.amount_paid AS amount_paid5, i0_.md5 AS md56, i0_.is_exported AS is_exported7, i0_.created AS created8, SUM(i0_.amount) AS sclr9, i0_.customer_id AS customer_id10 FROM Invoice i0_ GROUP BY i0_.customer_id 130210 16:08:25 95 Quit But I understand why that happens, it's due to group by and pagination nature. If I do $pager = new Pagerfanta(new DoctrineORMAdapter($query, false)); I get this sql SELECT i0_.id AS id0, i0_.invoice_num AS invoice_num1, i0_.date AS date2, i0_.amount AS amount3, i0_.vat_amount AS vat_amount4, i0_.amount_paid AS amount_paid5, i0_.md5 AS md56, i0_.is_exported AS is_exported7, i0_.created AS created8, SUM(i0_.amount) AS sclr9, i0_.customer_id AS customer_id10 FROM Invoice i0_ LIMIT 30 OFFSET 0 I think it should be noted somewhere that if you do groupBy you should set fetchJoin to false? |
| Comment by Marco Pivetta [ 10/Feb/13 ] |
|
Updating to Documentation issue. |