<!-- 
RSS generated by JIRA (5.2.7#850-sha1:b2af0c8dc8537b36121c6a579fabbdf79fc919e5) at Wed Jun 19 02:59:38 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-16/DDC-16.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-16] DQL Ignores properties of subclasses</title>
                <link>http://www.doctrine-project.org/jira/browse/DDC-16</link>
                <project id="10032" key="DDC">Doctrine 2 - ORM</project>
                        <description>&lt;p&gt;I have a classes B and C which inherit from superclass A. I would like&lt;br/&gt;
to get a list of all A&apos;s but filter the list to ignore those in C&lt;br/&gt;
which have a property d set to 2.&lt;/p&gt;

&lt;p&gt;select a from A where a.d == 2 fails because &quot;d&quot; is not a property of A.&lt;/p&gt;</description>
                <environment></environment>
            <key id="10070">DDC-16</key>
            <summary>DQL Ignores properties of subclasses</summary>
                <type id="4" iconUrl="http://www.doctrine-project.org/jira/images/icons/issuetypes/improvement.png">Improvement</type>
                                <priority id="4" iconUrl="http://www.doctrine-project.org/jira/images/icons/priorities/minor.png">Minor</priority>
                    <status id="6" iconUrl="http://www.doctrine-project.org/jira/images/icons/statuses/closed.png">Closed</status>
                    <resolution id="2">Won&apos;t Fix</resolution>
                    <security id="10000">All</security>
                        <assignee username="ocramius">Marco Pivetta</assignee>
                                <reporter username="ablock">Avi Block</reporter>
                        <labels>
                    </labels>
                <created>Tue, 15 Sep 2009 15:10:54 +0000</created>
                <updated>Sun, 24 Feb 2013 00:19:29 +0000</updated>
                    <resolved>Sun, 24 Feb 2013 00:19:29 +0000</resolved>
                            <version>2.0-ALPHA2</version>
                                                <component>DQL</component>
                        <due></due>
                    <votes>6</votes>
                        <watches>6</watches>
                        <comments>
                    <comment id="12534" author="romanb" created="Wed, 31 Mar 2010 14:09:20 +0000"  >&lt;p&gt;Thats indeed tricky. That syntax alone can, however, never work, because there might be several subclasses that have a field named &quot;d&quot;, so Doctrine would not know which field you mean.&lt;/p&gt;

&lt;p&gt;We might need special syntax for such constructs but I&apos;m not sure it is worth it. If anyone has an idea, just shoot.&lt;/p&gt;

&lt;p&gt;Alternatively, apart from using a native query, what about this DQL:&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;select a from A a where a.id not in (select c.id from C c where c.d = 2)
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;</comment>
                    <comment id="12539" author="beberlei" created="Thu, 1 Apr 2010 04:22:04 +0000"  >&lt;p&gt;I am sorry, but isn&apos;t this sort of query a violation of object oriented programming, Accessing a Superclass A and checking for a property that exists on a certain child is not possible in any strict OO language without specific casting into the type beforehand.&lt;/p&gt;</comment>
                    <comment id="12540" author="romanb" created="Thu, 1 Apr 2010 04:35:56 +0000"  >&lt;p&gt;Thats why I said &quot;we would need special syntax for such constructs&quot;, i.e. casting syntax &lt;img class=&quot;emoticon&quot; src=&quot;http://www.doctrine-project.org/jira/images/icons/emoticons/smile.gif&quot; height=&quot;20&quot; width=&quot;20&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;&lt;/p&gt;

&lt;p&gt;However, the alternative query I showed should work just fine for such purposes.&lt;/p&gt;</comment>
                    <comment id="16180" author="holtkamp" created="Mon, 18 Jul 2011 17:46:27 +0000"  >&lt;p&gt;+1 for this request. &lt;/p&gt;

&lt;p&gt;When displaying data (stored in a database) to end-users using generated tables, server-side sorting and filtering on properties that belong to subclasses is a common task, for instance to provide the data presented in a JQuery Datatable: &lt;a href=&quot;http://www.datatables.net&quot; class=&quot;external-link&quot;&gt;http://www.datatables.net&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;For this purpose, DQL is great to generate the required queries dynamically. However, since properties of subclasses can currently not be &apos;accessed&apos;, sorting and filtering on these properties becomes impossible, or at least cumbersome to implement. Currently I have lifted/copied specific properties to a general &apos;value&apos; property of the superclass, which CAN be used for ordering and filtering. Of course, this work-around significantly decreases the power of inheritance strategy in the originally envisioned model.&lt;/p&gt;

&lt;p&gt;In Hibernate this seems to have been solved in HQL using downcasting, which seems a viable approach, but as Benjamin already mentioned not a best practice in a strict OO approach. Therefore a specific syntax might be required to hint on the to-be-expected casts:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;&lt;a href=&quot;http://www.mail-archive.com/hibernate-devel@lists.sourceforge.net/msg03975.html&quot; class=&quot;external-link&quot;&gt;http://www.mail-archive.com/hibernate-devel@lists.sourceforge.net/msg03975.html&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;https://hibernate.onjira.com/browse/HB-792&quot; class=&quot;external-link&quot;&gt;https://hibernate.onjira.com/browse/HB-792&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;https://hibernate.onjira.com/browse/HB-132&quot; class=&quot;external-link&quot;&gt;https://hibernate.onjira.com/browse/HB-132&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;For additional motivation of this subject, check a recent post of mine at the user groups: &lt;a href=&quot;http://groups.google.com/group/doctrine-user/msg/caebf8139e06e01a&quot; class=&quot;external-link&quot;&gt;http://groups.google.com/group/doctrine-user/msg/caebf8139e06e01a&lt;/a&gt;&lt;/p&gt;</comment>
                    <comment id="16181" author="ablock" created="Mon, 18 Jul 2011 18:09:07 +0000"  >&lt;p&gt;Honestly I gave up on all of this a long time ago, and now use a CQRS approach. I make views, or denormalized tables for every view in my application, and just use straight SQL.&lt;/p&gt;

&lt;p&gt;When I need to do creating or updating or whatever, I use doctrine.&lt;/p&gt;</comment>
                    <comment id="16182" author="darkangel" created="Tue, 19 Jul 2011 12:23:10 +0000"  >&lt;p&gt;I would like this as well (though my use case involves joining to the subclass).&lt;/p&gt;

&lt;p&gt;Registration has one Opponent (superclass to Player and TeamSelection). I would like to count the number of registrations for a particular team within a specific tournament. For example:&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;SELECT COUNT(*)
FROM Registration r
INNER JOIN r.opponent CAST TeamSelection ts
INNER JOIN ts.team t
WHERE r.tournament = 1
AND t.name = &lt;span class=&quot;code-quote&quot;&gt;&quot;Team #1&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;</comment>
                    <comment id="16183" author="darkangel" created="Tue, 19 Jul 2011 12:41:49 +0000"  >&lt;p&gt;Thinking about this a bit more, it can actually improve query performance, as it would no longer be necessary to join the other subclasses. In other words, since you&apos;re casting the opponent to a TeamSelection, it&apos;s not necessary to join to the players table.&lt;/p&gt;

&lt;p&gt;A cast should probably also restrict the result using the discriminator column, for example (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-java&quot;&gt;INNER JOIN opponents o ON o.id = r.opponent_d AND o.type = &lt;span class=&quot;code-quote&quot;&gt;&quot;TeamSelection&quot;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;</comment>
                    <comment id="16479" author="guilhermeblanco" created="Thu, 15 Sep 2011 16:16:18 +0000"  >&lt;p&gt;Possible solution:&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;SELECT p FROM CAST(Person AS User) p
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or:&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;SELECT a, p FROM Article a JOIN CAST(a.person AS User) p
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;</comment>
                    <comment id="16481" author="darkangel" created="Fri, 16 Sep 2011 09:19:04 +0000"  >&lt;p&gt;If you&apos;re casting a Person (superclass) to a User (subclass), then shouldn&apos;t the alias be &quot;u&quot; and not &quot;p&quot;?&lt;/p&gt;

&lt;p&gt;i.e. SELECT a, u FROM Article a JOIN CAST(a.person AS User) u&lt;/p&gt;</comment>
                    <comment id="16482" author="beberlei" created="Fri, 16 Sep 2011 10:08:18 +0000"  >&lt;p&gt;the alias is that, an alias, you can use whatever you want.&lt;/p&gt;</comment>
                    <comment id="16483" author="darkangel" created="Fri, 16 Sep 2011 11:14:47 +0000"  >&lt;p&gt;Yes I know that, I&apos;m just making sure that I understand the syntax.&lt;/p&gt;</comment>
                    <comment id="17444" author="jakajancar" created="Sat, 18 Feb 2012 20:18:26 +0000"  >&lt;p&gt;I just ran into a similar problem and am not sure the above cast solution would do.&lt;/p&gt;

&lt;p&gt;I&apos;m trying to 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-java&quot;&gt;SELECT
    s
FROM
    Superclass s
WHERE
    s INSTANCE OF SubclassA OR
    s INSTANCE OF SubclassB AND s.foo = &apos;bar&apos;;
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So I&apos;d need to use CAST in a WHERE condition, not in the FROM/JOIN clause, e.g:&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;SELECT
    s
FROM
    Superclass s
WHERE
    s INSTANCE OF SubclassA OR
    s INSTANCE OF SubclassB AND (CAST s AS SubclassB).foo = &apos;bar&apos;;
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;
</comment>
                    <comment id="17735" author="dimirc" created="Tue, 3 Apr 2012 05:43:46 +0000"  >&lt;p&gt;Is there any workaround? Would be perfect if I could just: &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 a, p FROM Article a JOIN CAST(a.person AS User) p &lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt; 

&lt;p&gt;(+1 for Guilherme Blanco possible solution)&lt;/p&gt;</comment>
                    <comment id="17740" author="dimirc" created="Wed, 4 Apr 2012 05:07:14 +0000"  >&lt;p&gt;I was trying a workaround: &lt;/p&gt;

&lt;p&gt;Before executing the dql query, I change the mapping info of the association to the concrete type that I was going to use. Like this:&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;        $cmf = $this-&amp;gt;em-&amp;gt;getMetadataFactory();
        $class = $cmf-&amp;gt;getMetadataFor(&quot;Article&quot;);
        $class-&amp;gt;associationMappings[&quot;person&quot;][&quot;targetEntity&quot;]=&quot;User&quot;;
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But now my problem is that I&apos;m actually using an inverse side (in query where this is needed), so when I change the targetEntity, the  sql join that is generated, is using the table of the concrete class (User in this case) and not the base class (Person)&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;class Person {
...
	/**
	 * @OneToOne(targetEntity=&quot;Order&quot;,cascade={&quot;persist&quot;})
	 * @JoinColumn(name=&quot;order_id&quot;, referencedColumnName=&quot;orden_id&quot;)
	 */
	private $order;
...
}

class Order {
...
    /**
     * @OneToOne(targetEntity=&quot;Person&quot;, mappedBy=&quot;order&quot;)
     */
    private $person;
...
}

&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The  SQL generated is joining as if the &quot;user&quot; table has orden_id field &lt;img class=&quot;emoticon&quot; src=&quot;http://www.doctrine-project.org/jira/images/icons/emoticons/sad.gif&quot; height=&quot;20&quot; width=&quot;20&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt; &lt;/p&gt;
</comment>
                    <comment id="19679" author="ocramius" created="Sun, 24 Feb 2013 00:19:29 +0000"  >&lt;p&gt;I am closing this one.&lt;/p&gt;

&lt;p&gt;The requirement of this issue is basically violating OO principles.&lt;/p&gt;

&lt;p&gt;If you really need to filter across multiple child-entities in your inheritance, then try something as following instead:&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;
SELECT
    r
FROM
    Root r
WHERE
    r.id IN (
        SELECT
            c.id
        FROM
            Child c
        WHERE
            c.field = :value
    )
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Up-casting is not really a viable solution anyway, since it would be even more weird to cast in DQL and then have hydration still retrieve the root entity type.&lt;/p&gt;</comment>
                </comments>
                <issuelinks>
                        <issuelinktype id="10000">
                <name>Duplicate</name>
                                                <inwardlinks description="is duplicated by">
                            <issuelink>
            <issuekey id="13020">DDC-1377</issuekey>
        </issuelink>
                    </inwardlinks>
                            </issuelinktype>
                    </issuelinks>
                <attachments>
                </attachments>
            <subtasks>
        </subtasks>
        </item>
</channel>
</rss>