<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Tue May 21 07:28:14 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-1960/DDC-1960.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-1960] mapping joins in native queries breaks if select columns are starting with columns from joined table</title>
                <link>http://www.doctrine-project.org/jira/browse/DDC-1960</link>
                <project id="10032" key="DDC">Doctrine 2 - ORM</project>
                        <description>&lt;p&gt;Using a simple Testcase like in &lt;a href=&quot;http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/native-sql.html&quot; class=&quot;external-link&quot;&gt;http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/native-sql.html&lt;/a&gt; there are two Tables:&lt;/p&gt;

&lt;p&gt;*) users:&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;
   Column   |  Type   | Modifiers | Storage  | Description 
------------+---------+-----------+----------+-------------
 u_id       | integer | not &lt;span class=&quot;code-keyword&quot;&gt;null&lt;/span&gt;  | plain    | 
 u_name     | text    | not &lt;span class=&quot;code-keyword&quot;&gt;null&lt;/span&gt;  | extended | 
 address_id | integer | not &lt;span class=&quot;code-keyword&quot;&gt;null&lt;/span&gt;  | plain    | 
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;*) address:&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;
  Column  |  Type   | Modifiers | Storage  | Description 
----------+---------+-----------+----------+-------------
 a_id     | integer | not &lt;span class=&quot;code-keyword&quot;&gt;null&lt;/span&gt;  | plain    | 
 a_street | text    | not &lt;span class=&quot;code-keyword&quot;&gt;null&lt;/span&gt;  | extended | 
 a_city   | text    | not &lt;span class=&quot;code-keyword&quot;&gt;null&lt;/span&gt;  | extended | 
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;address_id is a foreign key to address;&lt;/p&gt;

&lt;p&gt;Now i created the Entities and setup a native query using ResultSetMappingBuilder:&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;
$rsm = &lt;span class=&quot;code-keyword&quot;&gt;new&lt;/span&gt; \Doctrine\ORM\Query\ResultSetMappingBuilder($entityManager);
$rsm-&amp;gt;addRootEntityFromClassMetadata(&apos;MyProject\Entity\Users&apos;, &apos;u&apos;);
$rsm-&amp;gt;addJoinedEntityFromClassMetadata(&apos;MyProject\Entity\Address&apos;, &apos;a&apos;, &apos;u&apos;, &apos;address&apos;);

$query = &apos;
    SELECT
        u.*,
        a.*
    FROM
        users u
    LEFT JOIN address a ON (u.address_id = a.a_id)
&apos;;

/** @&lt;span class=&quot;code-keyword&quot;&gt;var&lt;/span&gt; $&lt;span class=&quot;code-keyword&quot;&gt;native&lt;/span&gt; \Doctrine\ORM\NativeQuery */
$&lt;span class=&quot;code-keyword&quot;&gt;native&lt;/span&gt; = $entityManager-&amp;gt;createNativeQuery($query, $rsm);

$ret = $&lt;span class=&quot;code-keyword&quot;&gt;native&lt;/span&gt;-&amp;gt;getResult();
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This returns the Entities correctly:&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;
array(2) {
  [0] =&amp;gt;
  class MyProject\Entity\Users#61 (3) {
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $id =&amp;gt;
    &lt;span class=&quot;code-object&quot;&gt;int&lt;/span&gt;(1)
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $name =&amp;gt;
    string(5) &lt;span class=&quot;code-quote&quot;&gt;&quot;Smith&quot;&lt;/span&gt;
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $address =&amp;gt;
    class MyProject\Entity\Address#63 (4) {
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $id =&amp;gt;
      &lt;span class=&quot;code-object&quot;&gt;int&lt;/span&gt;(1)
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $street =&amp;gt;
      string(8) &lt;span class=&quot;code-quote&quot;&gt;&quot;Broadway&quot;&lt;/span&gt;
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $city =&amp;gt;
      string(8) &lt;span class=&quot;code-quote&quot;&gt;&quot;New York&quot;&lt;/span&gt;
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $users =&amp;gt;
      class Doctrine\ORM\PersistentCollection#64 (9) {
        ...
      }
    }
  }
  [1] =&amp;gt;
  class MyProject\Entity\Users#66 (3) {
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $id =&amp;gt;
    &lt;span class=&quot;code-object&quot;&gt;int&lt;/span&gt;(2)
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $name =&amp;gt;
    string(7) &lt;span class=&quot;code-quote&quot;&gt;&quot;Sherlok&quot;&lt;/span&gt;
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $address =&amp;gt;
    class MyProject\Entity\Address#67 (4) {
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $id =&amp;gt;
      &lt;span class=&quot;code-object&quot;&gt;int&lt;/span&gt;(2)
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $street =&amp;gt;
      string(13) &lt;span class=&quot;code-quote&quot;&gt;&quot;Oxford Street&quot;&lt;/span&gt;
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $city =&amp;gt;
      string(6) &lt;span class=&quot;code-quote&quot;&gt;&quot;London&quot;&lt;/span&gt;
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $users =&amp;gt;
      class Doctrine\ORM\PersistentCollection#68 (9) {
        ...
      }
    }
  }
}
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;BUT if you change the order of the select columns starting with ones from address you get borked Data:&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 = &apos;
    SELECT
        a.*,
        u.*
    FROM
        users u
    LEFT JOIN address a ON (u.address_id = a.a_id)
&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-java&quot;&gt;
array(2) {
  [0] =&amp;gt;
  class MyProject\Entity\Users#61 (3) {
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $id =&amp;gt;
    &lt;span class=&quot;code-object&quot;&gt;int&lt;/span&gt;(1)
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $name =&amp;gt;
    string(5) &lt;span class=&quot;code-quote&quot;&gt;&quot;Smith&quot;&lt;/span&gt;
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $address =&amp;gt;
    class MyProject\Entity\Address#63 (4) {
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $id =&amp;gt;
      &lt;span class=&quot;code-object&quot;&gt;int&lt;/span&gt;(2)
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $street =&amp;gt;
      string(13) &lt;span class=&quot;code-quote&quot;&gt;&quot;Oxford Street&quot;&lt;/span&gt;
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $city =&amp;gt;
      string(6) &lt;span class=&quot;code-quote&quot;&gt;&quot;London&quot;&lt;/span&gt;
      &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $users =&amp;gt;
      class Doctrine\ORM\PersistentCollection#64 (9) {
        ...
      }
    }
  }
  [1] =&amp;gt;
  class MyProject\Entity\Users#66 (3) {
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $id =&amp;gt;
    &lt;span class=&quot;code-object&quot;&gt;int&lt;/span&gt;(2)
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $name =&amp;gt;
    string(7) &lt;span class=&quot;code-quote&quot;&gt;&quot;Sherlok&quot;&lt;/span&gt;
    &lt;span class=&quot;code-keyword&quot;&gt;protected&lt;/span&gt; $address =&amp;gt;
    NULL
  }
}
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This happens because the function Doctrine\ORM\Internal\Hydration\AbstractHydrator::_gatherRowData does not consider the Mapping i set up. Instead it just add the columns as they get starting with address ones.&lt;/p&gt;

&lt;p&gt;Doctrine\ORM\Internal\Hydration\ObjectHydrator::_hydrateRow then knows the Mapping and ignores the first Address as there is no User to map on, cycling to the next row will then add the address of the second row to the user from the first one.&lt;/p&gt;

&lt;p&gt;There are multiple ways to fix this. One would be to consider the mapping in _gatherRowData, the second to rewrite the _hydrateRow generating the Entities first and then the mapping in a second foreach loop.&lt;/p&gt;

&lt;p&gt;This bugger had me for 2 days until i finally figured it out.&lt;/p&gt;

&lt;p&gt;thanks&lt;/p&gt;</description>
                <environment>ubuntu kernel 2.6.32-40-server&lt;br/&gt;
php 5.3.10-1ubuntu2ppa6~lucid with Suhosin-Patch (cli)&lt;br/&gt;
apache 2 2.2.14-5ubuntu8.9&lt;br/&gt;
postgres 9.1.4-1~lucid4</environment>
            <key id="13900">DDC-1960</key>
            <summary>mapping joins in native queries breaks if select columns are starting with columns from joined table</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>
                    <security id="10000">All</security>
                        <assignee username="beberlei">Benjamin Eberlei</assignee>
                                <reporter username="dready">Thomas Subera</reporter>
                        <labels>
                    </labels>
                <created>Tue, 31 Jul 2012 11:23:21 +0000</created>
                <updated>Wed, 21 Nov 2012 23:48:37 +0000</updated>
                                    <version>2.1.4</version>
                <version>2.1.7</version>
                                                <component>ORM</component>
                        <due></due>
                    <votes>0</votes>
                        <watches>3</watches>
                        <comments>
                    <comment id="19022" author="frederes" created="Wed, 21 Nov 2012 23:30:34 +0000"  >&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;Has same issue with using DQL /createQuery() ! Try all the day to find where was my mistake but seems to be a CRITICAL bug !&lt;br/&gt;
How did you solve this ?&lt;/p&gt;


&lt;p&gt;Doctrine version used : 2.3.1-DEV&lt;/p&gt;

&lt;p&gt;&amp;lt;code&amp;gt;&lt;br/&gt;
$query = $this-&amp;gt;getEntityManager()-&amp;gt;createQuery(&quot;&lt;br/&gt;
            SELECT cc, oc&lt;br/&gt;
            FROM  category cc&lt;br/&gt;
                JOIN cc.offer_category oc&lt;br/&gt;
            WHERE cc.catalog = :catalog_id&lt;br/&gt;
            ORDER BY oc.name ASC&lt;br/&gt;
            &quot;)&lt;br/&gt;
            -&amp;gt;setParameter(&quot;:catalog_id&quot;, $catalog_id)&lt;br/&gt;
            ;&lt;/p&gt;

&lt;p&gt;&amp;lt;/code&amp;gt;&lt;/p&gt;

&lt;p&gt;Problem is that the order of the Aliases (cc, oc) is not considered on building SQL .&lt;br/&gt;
In my case, in the ObjectHydrator::hydrateRowData method :&lt;/p&gt;

&lt;p&gt;$rowData = $this-&amp;gt;gatherRowData($row, $cache, $id, $nonemptyComponents);&lt;/p&gt;

&lt;p&gt;returns &lt;/p&gt;

&lt;p&gt;Array&lt;br/&gt;
(&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;error&quot;&gt;&amp;#91;oc&amp;#93;&lt;/span&gt; =&amp;gt; Array&lt;br/&gt;
          (&lt;br/&gt;
            &lt;span class=&quot;error&quot;&gt;&amp;#91;id&amp;#93;&lt;/span&gt; =&amp;gt; 14&lt;br/&gt;
            &lt;span class=&quot;error&quot;&gt;&amp;#91;name&amp;#93;&lt;/span&gt; =&amp;gt; toto&lt;br/&gt;
        )&lt;br/&gt;
&lt;span class=&quot;error&quot;&gt;&amp;#91;cc&amp;#93;&lt;/span&gt; =&amp;gt; Array&lt;br/&gt;
        (&lt;br/&gt;
            &lt;span class=&quot;error&quot;&gt;&amp;#91;catalog_id&amp;#93;&lt;/span&gt; =&amp;gt; 1&lt;br/&gt;
            &lt;span class=&quot;error&quot;&gt;&amp;#91;offer_category_id&amp;#93;&lt;/span&gt; =&amp;gt; 14&lt;br/&gt;
        )&lt;br/&gt;
)&lt;/p&gt;

&lt;p&gt;As &quot;oc&quot; is a mapping, on the first loop the $parentAlias is not yet known and so :&lt;br/&gt;
&amp;lt;code&amp;gt;&lt;br/&gt;
if ($this-&amp;gt;_rsm-&amp;gt;isMixed &amp;amp;&amp;amp; isset($this-&amp;gt;_rootAliases&lt;span class=&quot;error&quot;&gt;&amp;#91;$parentAlias&amp;#93;&lt;/span&gt;)) &lt;/p&gt;
{
                    echo &quot;parentObject 1\n&quot;;
                    $first = reset($this-&amp;gt;_resultPointers);
                    $parentObject = $first[key($first)];
                }
&lt;p&gt; else if (isset($this-&amp;gt;_resultPointers&lt;span class=&quot;error&quot;&gt;&amp;#91;$parentAlias&amp;#93;&lt;/span&gt;)) &lt;/p&gt;
{
                    echo $parentAlias.&quot; parentObject 2\n&quot;;
                    $parentObject = $this-&amp;gt;_resultPointers[$parentAlias];
                }
&lt;p&gt; else &lt;/p&gt;
{
                    // HERE : on first loop, for &quot;oc&quot;, parent not yet known so skipped !!!
                    continue;
                }
&lt;p&gt;&amp;lt;/code&amp;gt;&lt;/p&gt;

&lt;p&gt;using a workaround on ObjectHydrator::hydrateRowData like this :&lt;br/&gt;
$rowData = array_reverse($rowData);&lt;/p&gt;

&lt;p&gt;make it work...&lt;/p&gt;

&lt;p&gt;Sorry for my dirty explanation... &lt;/p&gt;</comment>
                </comments>
                    <attachments>
                    <attachment id="11215" name="testcase.zip" size="5941" author="dready" created="Tue, 31 Jul 2012 11:23:21 +0000" />
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>