<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Wed Jun 19 09:31:34 UTC 2013

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary add field=key&field=summary to the URL of your request.
For example:
http://www.doctrine-project.org/jira/si/jira.issueviews:issue-xml/DDC-2236/DDC-2236.xml?field=key&field=summary
-->
<rss version="0.92" >
<channel>
    <title>Doctrine Project</title>
    <link>http://www.doctrine-project.org/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>5.2.7</version>
        <build-number>850</build-number>
        <build-date>21-02-2013</build-date>
    </build-info>

<item>
            <title>[DDC-2236] SUM(..) with Pagination gives incorrect result</title>
                <link>http://www.doctrine-project.org/jira/browse/DDC-2236</link>
                <project id="10032" key="DDC">Doctrine 2 - ORM</project>
                        <description>&lt;p&gt;&lt;a href=&quot;https://github.com/whiteoctober/Pagerfanta/issues/69&quot; class=&quot;external-link&quot;&gt;https://github.com/whiteoctober/Pagerfanta/issues/69&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;?php&lt;br/&gt;
$query = $em-&amp;gt;getRepository(&apos;M\E\Q&apos;)&lt;br/&gt;
       -&amp;gt;createQueryBuilder(&apos;q&apos;)&lt;br/&gt;
       -&amp;gt;select(&apos;q&apos;, &apos;SUM(q.price) AS amount&apos;)&lt;br/&gt;
       -&amp;gt;where(&apos;q.id IN(19, 20, 22)&apos;)&lt;br/&gt;
      -&amp;gt;groupBy(&apos;q.customer&apos;)&lt;br/&gt;
;&lt;/p&gt;

&lt;p&gt;$pager = new Pagerfanta(new DoctrineORMAdapter($query));&lt;br/&gt;
$pager-&amp;gt;setMaxPerPage(30);&lt;br/&gt;
$pager-&amp;gt;setCurrentPage($request-&amp;gt;query-&amp;gt;get(&apos;page&apos;, 1));&lt;/p&gt;

&lt;p&gt;$result = $pager-&amp;gt;getCurrentPageResults();&lt;br/&gt;
print_r($result&lt;span class=&quot;error&quot;&gt;&amp;#91;0&amp;#93;&lt;/span&gt;&lt;span class=&quot;error&quot;&gt;&amp;#91;&amp;#39;amount&amp;#39;&amp;#93;&lt;/span&gt;); // 156.71 - Incorrect&lt;/p&gt;

&lt;p&gt;$result = $query-&amp;gt;getQuery()-&amp;gt;getResult();&lt;br/&gt;
print_r($result&lt;span class=&quot;error&quot;&gt;&amp;#91;0&amp;#93;&lt;/span&gt;&lt;span class=&quot;error&quot;&gt;&amp;#91;&amp;#39;amount&amp;#39;&amp;#93;&lt;/span&gt;); // 553.47&lt;br/&gt;
?&amp;gt;&lt;/p&gt;

&lt;p&gt;Sql for the above:&lt;/p&gt;

&lt;p&gt;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&lt;br/&gt;
SELECT q0_.id AS id0, SUM(q0_.price) AS sclr36 FROM Q q0_ WHERE q0_.id IN (19, 20, 22) AND q0_.id IN (&apos;19&apos;) GROUP BY q0_.customer_id&lt;br/&gt;
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&lt;/p&gt;


&lt;p&gt;Sql with fetchJoin = false (new DoctrineORMAdapter($query, false))&lt;/p&gt;

&lt;p&gt;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&lt;br/&gt;
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&lt;/p&gt;</description>
                <environment>Linux</environment>
            <key id="14375">DDC-2236</key>
            <summary>SUM(..) with Pagination gives incorrect result</summary>
                <type id="6" iconUrl="http://www.doctrine-project.org/jira/images/icons/issuetypes/documentation.png">Documentation</type>
                                <priority id="4" iconUrl="http://www.doctrine-project.org/jira/images/icons/priorities/minor.png">Minor</priority>
                    <status id="3" iconUrl="http://www.doctrine-project.org/jira/images/icons/statuses/inprogress.png">In Progress</status>
                    <resolution id="-1">Unresolved</resolution>
                                <assignee username="beberlei">Benjamin Eberlei</assignee>
                                <reporter username="olegk">Oleg</reporter>
                        <labels>
                        <label>paginator</label>
                    </labels>
                <created>Fri, 11 Jan 2013 16:24:56 +0000</created>
                <updated>Sun, 10 Feb 2013 18:21:58 +0000</updated>
                                    <version>2.2.3</version>
                                                <component>Tools</component>
                        <due></due>
                    <votes>1</votes>
                        <watches>4</watches>
                        <comments>
                    <comment id="19511" author="asm89" created="Sat, 9 Feb 2013 23:48:30 +0000"  >&lt;p&gt;Can you also test this with doctrine &amp;gt;= 2.3? The pagination code changed quite a lot.&lt;/p&gt;</comment>
                    <comment id="19517" author="olegk" created="Sun, 10 Feb 2013 11:30:24 +0000"  >&lt;p&gt;Looks like no change&lt;/p&gt;

&lt;p&gt;composer.json:&lt;br/&gt;
&quot;doctrine/orm&quot;: &quot;2.3.*&quot;,&lt;br/&gt;
&amp;#8211;&lt;br/&gt;
php composer.phar update&lt;br/&gt;
Loading composer repositories with package information&lt;br/&gt;
Updating dependencies&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;Installing doctrine/common (2.3.0)&lt;br/&gt;
    Loading from cache&lt;/li&gt;
&lt;/ul&gt;


&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;Installing doctrine/dbal (2.3.2)&lt;br/&gt;
    Loading from cache&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;then cleared cache but result is same&lt;br/&gt;
Here&apos;s the code&lt;/p&gt;

&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-none&quot;&gt; 
$query = $this-&amp;gt;getDoctrine()-&amp;gt;getEntityManager()-&amp;gt;getRepository(&apos;MyBundle:Invoice&apos;)
  -&amp;gt;createQueryBuilder(&apos;q&apos;)
  -&amp;gt;select(&apos;q&apos;, &apos;SUM(q.amount) AS amount&apos;)
  -&amp;gt;groupBy(&apos;q.customer&apos;)
;
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 


&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-sql&quot;&gt; 
95 Connect	root@localhost on **
95 Query	&lt;span class=&quot;code-keyword&quot;&gt;SELECT&lt;/span&gt; DISTINCT id0 &lt;span class=&quot;code-keyword&quot;&gt;FROM&lt;/span&gt; (&lt;span class=&quot;code-keyword&quot;&gt;SELECT&lt;/span&gt; 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 &lt;span class=&quot;code-keyword&quot;&gt;FROM&lt;/span&gt; Invoice i0_ &lt;span class=&quot;code-keyword&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;code-keyword&quot;&gt;BY&lt;/span&gt; i0_.customer_id) dctrn_result LIMIT 30 OFFSET 0
95 Query	&lt;span class=&quot;code-keyword&quot;&gt;SELECT&lt;/span&gt; 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 &lt;span class=&quot;code-keyword&quot;&gt;FROM&lt;/span&gt; Invoice i0_ &lt;span class=&quot;code-keyword&quot;&gt;WHERE&lt;/span&gt; i0_.id IN (&apos;2&apos;) &lt;span class=&quot;code-keyword&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;code-keyword&quot;&gt;BY&lt;/span&gt; i0_.customer_id
95 Query	&lt;span class=&quot;code-keyword&quot;&gt;SELECT&lt;/span&gt; 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 &lt;span class=&quot;code-keyword&quot;&gt;FROM&lt;/span&gt; Invoice i0_ &lt;span class=&quot;code-keyword&quot;&gt;GROUP&lt;/span&gt; &lt;span class=&quot;code-keyword&quot;&gt;BY&lt;/span&gt; i0_.customer_id
130210 16:08:25	   95 Quit	
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 


&lt;p&gt;But I understand why that happens, it&apos;s due to group by and pagination nature. &lt;br/&gt;
The first query returns only one row with id &quot;2&quot;, second query should be actually &quot;..WHERE i0_.id IN (&apos;2&apos;, &apos;3&apos;, &apos;4&apos;)&quot;&lt;/p&gt;

&lt;p&gt;If I do&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-none&quot;&gt;
$pager = new Pagerfanta(new DoctrineORMAdapter($query, false));
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I get this sql&lt;/p&gt;
&lt;div class=&quot;code panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;codeContent panelContent&quot;&gt;
&lt;pre class=&quot;code-sql&quot;&gt;
&lt;span class=&quot;code-keyword&quot;&gt;SELECT&lt;/span&gt; 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 &lt;span class=&quot;code-keyword&quot;&gt;FROM&lt;/span&gt; Invoice i0_ LIMIT 30 OFFSET 0
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;


&lt;p&gt;I think it should be noted somewhere that if you do groupBy you should set fetchJoin to false? &lt;/p&gt;</comment>
                    <comment id="19525" author="ocramius" created="Sun, 10 Feb 2013 18:21:58 +0000"  >&lt;p&gt;Updating to Documentation issue.&lt;/p&gt;</comment>
                </comments>
                    <attachments>
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>