<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Sun May 19 13:28:46 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/DC-914/DC-914.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>[DC-914] Doctrine_Pager ignores custom COUNT query</title>
                <link>http://www.doctrine-project.org/jira/browse/DC-914</link>
                <project id="10031" key="DC">Doctrine 1</project>
                        <description>
&lt;p&gt;I found some problem when I tried to define custom query for results counting. Defined custom COUNT query is totally ignored and executed default one. I will give you full description of problem bellow.&lt;/p&gt;

&lt;p&gt;We have following source code:&lt;/p&gt;

&lt;div class=&quot;preformatted panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;preformattedContent panelContent&quot;&gt;
&lt;pre&gt; 
$q_select = Doctrine_Query::create ()
-&amp;gt;select ( &apos;DISTINCT p.product_name AS product_name&apos; )
-&amp;gt;from ( &apos;Product p&apos; )
-&amp;gt;where( &apos;p.product_name LIKE ?&apos;, &apos;%motorola%&apos;);
				
$q_count = Doctrine_Query::create ()
-&amp;gt;select ( &apos;COUNT (DISTINCT p.product_name) num_results&apos; )
-&amp;gt;from ( &apos;Product p&apos; )
-&amp;gt;where( &apos;p.product_name LIKE ?&apos;, &apos;%motorola%&apos;);
												
$pager = new Doctrine_Pager( $q_select, 1, 25 );										
$pager-&amp;gt;setCountQuery($q_count);
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 

&lt;p&gt;Let&apos;s check custom query before calling $pager-&amp;gt;execute() method:&lt;/p&gt;

&lt;div class=&quot;preformatted panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;preformattedContent panelContent&quot;&gt;
&lt;pre&gt; 
echo $pager-&amp;gt;getCountQuery(); 
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 

&lt;p&gt;Output:&lt;/p&gt;

&lt;div class=&quot;preformatted panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;preformattedContent panelContent&quot;&gt;
&lt;pre&gt; 
SELECT COUNT (DISTINCT p.product_name) num_results FROM Product p WHERE p.product_name LIKE ?
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 

&lt;p&gt;Looks like until now is everything is correct.  Let&apos;s call $pager-&amp;gt;execute() method:&lt;/p&gt;
&lt;div class=&quot;preformatted panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;preformattedContent panelContent&quot;&gt;
&lt;pre&gt; 
$products = $pager-&amp;gt;execute(); 
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 

&lt;p&gt;Let&apos;s check executed queries using Symfony SQL queries log panel:&lt;/p&gt;

&lt;div class=&quot;preformatted panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;preformattedContent panelContent&quot;&gt;
&lt;pre&gt;SELECT COUNT(*) AS num_results FROM product p WHERE p.product_name LIKE &apos;%motorola%&apos;
7.27s, &quot;doctrine&quot; connection

SELECT DISTINCT p.product_name AS p__0 FROM product p WHERE (p.product_name LIKE &apos;%motorola%&apos;) LIMIT 25
3.25s, &quot;doctrine&quot; connection
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 

&lt;p&gt;Executed COUNT query is not same we set using $pager-&amp;gt;setCountQuery($q_count). Our defined custom COUNT query is totally ignored and executed default one:&lt;/p&gt;

&lt;p&gt;INSTEAD OF THIS  CUSTOM COUNT  QUERY:&lt;/p&gt;

&lt;div class=&quot;preformatted panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;preformattedContent panelContent&quot;&gt;
&lt;pre&gt;SELECT COUNT (DISTINCT p.product_name) num_results FROM Product p WHERE p.product_name LIKE &apos;%motorola%&apos;
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;EXECUTED DEFAULT COUNT QUERY:&lt;/p&gt;

&lt;div class=&quot;preformatted panel&quot; style=&quot;border-width: 1px;&quot;&gt;&lt;div class=&quot;preformattedContent panelContent&quot;&gt;
&lt;pre&gt;SELECT COUNT(*) AS num_results FROM product p WHERE p.product_name LIKE &apos;%motorola%&apos;
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; </description>
                <environment>Zend Server CE</environment>
            <key id="12058">DC-914</key>
            <summary>Doctrine_Pager ignores custom COUNT query</summary>
                <type id="1" iconUrl="http://www.doctrine-project.org/jira/images/icons/issuetypes/bug.png">Bug</type>
                                <priority id="3" iconUrl="http://www.doctrine-project.org/jira/images/icons/priorities/major.png">Major</priority>
                    <status id="1" iconUrl="http://www.doctrine-project.org/jira/images/icons/statuses/open.png">Open</status>
                    <resolution id="-1">Unresolved</resolution>
                                <assignee username="guilhermeblanco">Guilherme Blanco</assignee>
                                <reporter username="arnlukas">Arnoldas Lukasevicius</reporter>
                        <labels>
                    </labels>
                <created>Tue, 2 Nov 2010 06:23:30 +0000</created>
                <updated>Mon, 7 Nov 2011 22:54:46 +0000</updated>
                                    <version>1.2.3</version>
                                                <component>Pager</component>
                        <due></due>
                    <votes>0</votes>
                        <watches>0</watches>
                        <comments>
                    <comment id="16774" author="blueskypoa" created="Mon, 7 Nov 2011 22:54:46 +0000"  >&lt;p&gt;I found a possible solution to the problem.&lt;/p&gt;

&lt;p&gt;That occurs not because the Pager countQuery but in a method used inside the Query class.&lt;/p&gt;

&lt;p&gt;When you set the Query or CountQuery for Pager and execute it, it calls a Query method called count(). This method by yourself call another Query class method named Query::getCountSqlQuery().&lt;/p&gt;

&lt;p&gt;This method rather than simply execute the query that you passed earlier, simply create a new query.&lt;/p&gt;

&lt;p&gt;Below is a possible solution to the problem:&lt;/p&gt;

&lt;p&gt;Query.php (Doctrine Stable 1.2.4)&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-java&quot;&gt;
--- Query.php	2011-11-07 20:52:48.000000000 -0200
+++ Query.php	2011-11-07 20:51:58.000000000 -0200
@@ -2049,40 +2049,7 @@
         &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; (count($&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;_queryComponents) == 1 &amp;amp;&amp;amp; empty($having)) {
             $q .= $from . $where . $groupby . $having;
         } &lt;span class=&quot;code-keyword&quot;&gt;else&lt;/span&gt; {
-
-            &lt;span class=&quot;code-comment&quot;&gt;// Subselect fields will contain only the pk of root entity
&lt;/span&gt;-            $ta = $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;_conn-&amp;gt;quoteIdentifier($tableAlias);
-
-            $map = $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;getRootDeclaration();
-            $idColumnNames = $map[&apos;table&apos;]-&amp;gt;getIdentifierColumnNames();
-
-            $pkFields = $ta . &apos;.&apos; . implode(&apos;, &apos; . $ta . &apos;.&apos;, $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;_conn-&amp;gt;quoteMultipleIdentifier($idColumnNames));
-
-            &lt;span class=&quot;code-comment&quot;&gt;// We need to &lt;span class=&quot;code-keyword&quot;&gt;do&lt;/span&gt; some magic in select fields &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; the query contain anything in having clause
&lt;/span&gt;-            $selectFields = $pkFields;
-
-            &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; ( ! empty($having)) {
-                &lt;span class=&quot;code-comment&quot;&gt;// For each field defined in select clause
&lt;/span&gt;-                foreach ($&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;_sqlParts[&apos;select&apos;] as $field) {
-                    &lt;span class=&quot;code-comment&quot;&gt;// We only include aggregate expressions to count query
&lt;/span&gt;-                    &lt;span class=&quot;code-comment&quot;&gt;// This is needed because HAVING clause will use field aliases
&lt;/span&gt;-                    &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; (strpos($field, &apos;(&apos;) !== &lt;span class=&quot;code-keyword&quot;&gt;false&lt;/span&gt;) {
-                        $selectFields .= &apos;, &apos; . $field;
-                    }
-                }
-                &lt;span class=&quot;code-comment&quot;&gt;// Add having fields that got stripped out of select
&lt;/span&gt;-                preg_match_all(&apos;/`[a-z0-9_]+`\.`[a-z0-9_]+`/i&apos;, $having, $matches, PREG_PATTERN_ORDER);
-                &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; (count($matches[0]) &amp;gt; 0) {
-                    $selectFields .= &apos;, &apos; . implode(&apos;, &apos;, array_unique($matches[0]));
-                }
-            }
-
-            &lt;span class=&quot;code-comment&quot;&gt;// If we &lt;span class=&quot;code-keyword&quot;&gt;do&lt;/span&gt; not have a custom group by, apply the &lt;span class=&quot;code-keyword&quot;&gt;default&lt;/span&gt; one
&lt;/span&gt;-            &lt;span class=&quot;code-keyword&quot;&gt;if&lt;/span&gt; (empty($groupby)) {
-                $groupby = &apos; GROUP BY &apos; . $pkFields;
-            }
-
-            $q .= &apos;(SELECT &apos; . $selectFields . &apos; FROM &apos; . $from . $where . $groupby . $having . &apos;) &apos;
+            $q .= &apos;( &apos;.$&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;getSqlQuery().&apos; ) &apos;
                 . $&lt;span class=&quot;code-keyword&quot;&gt;this&lt;/span&gt;-&amp;gt;_conn-&amp;gt;quoteIdentifier(&apos;dctrn_count_query&apos;);
         }
         &lt;span class=&quot;code-keyword&quot;&gt;return&lt;/span&gt; $q;
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;</comment>
                </comments>
                    <attachments>
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>