Doctrine DQL, class table inheritance and access to subclass fields
Asked Answered
N

5

21

I have a problem with a DQL query and entity specialization.

I have an Entity called Auction, which is OneToOne relation with Item. Item is a mappedSuperclass for Film and Book. I need a query that could back a search engine, allowing the user to look for auctions with different properties AND selling items with different properties (it is the AND part that makes it challenging).

The problem is that even though Auction has an association pointing to Item as such, I need to have access to Film- and Book-specific fields. The users will specify the Item type they're looking for, but I don't see any way of using this information other than using INSTANCE OF in my DQL query.

So far, I have tried using a query like:

SELECT a FROM Entities\Auction a
    INNER JOIN a.item i 
    INNER JOIN i.bookTypes b 
    WHERE i INSTANCE OF Entities\Book 
    AND b.type = 'Fantasy' 
    AND ...". 

Such a query results in an error saying that:

Class Entities\Item has no field or association named bookTypes

which is false for Book, yet true for Item.

I have also tried

SELECT a FROM Entities\Book i 
    INNER JOIN i.auction a ...

but I reckon Doctrine requires that I refer to the same Entity in SELECT and FROM statements.

If that's of importance, I am using class table inheritance. Still, I don't think switching to single table inheritance would do the trick.

Any ideas?

Nidus answered 13/12, 2011 at 21:45 Comment(1)
possible duplicate of Where-ing in discriminated tablesAxenic
A
15

As Matt stated, this is an old issue that Doctrine Project won't fix (DDC-16).

The problem is that doctrine's DQL is a statically typed language that comes with a certain amount of complexity in its internals.

We thought about allowing upcasting a couple of times, but the effort to get that working is simply not worth it, and people would simply abuse the syntax doing very dangerous things.

As stated on DDC-16, it is also indeed not possible to understand which class the property belongs to without incurring in nasty problems such as multiple subclasses defining same properties with different column names.

If you want to filter data in subclasses in a CTI or JTI, you may use the technique that I've described at https://mcmap.net/q/660589/-where-ing-in-discriminated-tables . That couples your DQL with all involved subclasses.

The DQL you would need in your case is most probably (assuming that Entities\Book is a subclass of Entities\Item):

SELECT
    a
FROM
    Entities\Auction a 
INNER JOIN
    a.item i
INNER JOIN
    i.bookTypes b
WHERE
    i.id IN (
        SELECT 
            b.id
        FROM
            Entities\Book b
        WHERE
            b.type = 'Fantasy'
    )

That is the pseudo-code for your problem. It is not nice, but keep in mind that SQL and DQL are very different and follow different rules.

Axenic answered 20/1, 2014 at 5:56 Comment(0)
B
4

Updated:

I've discovered a solution for this. See my answer for this related question:

Doctrine2: Polymorphic Queries: Searching on properties of subclasses

Billings answered 6/8, 2013 at 23:13 Comment(0)
C
4

You can easily solve this by left-joining your base entity with your inheritance class using the id:

SELECT a FROM Entities\Auction a
    INNER JOIN a.item i 
    INNER JOIN Entities\Book b WITH b.id = i.id 
    INNER JOIN b.bookTypes bt
    WHERE bt.type = 'Fantasy' 
    AND...

or with a queryBuilder:

$queryBuilderb->select('a')
   ->from('Entities\Auction', 'a')
   ->innerJoin('a.item', 'i')
   ->innerJoin('Entities\Book', 'b', 'WITH', 'b.id = i.id')
   ->innerJoin('b.bookTypes', 'bt')
   ->where('bt.type = :type')
   ->andWhere(...
   ->setParameter('type', 'Fantasy');

This is based on the answer given by Ian Philips in the question here

Curst answered 9/3, 2015 at 11:27 Comment(0)
G
2

The Doctrine team has stated that they're not going to add support for this:

https://github.com/doctrine/orm/issues/2237

Pertinent comments from that page:

Thats indeed tricky. That syntax alone can, however, never work, because there might be several subclasses that have a field named "d", so Doctrine would not know which field you mean.


I am closing this one.

The requirement of this issue is basically violating OO principles.

If you really need to filter across multiple child-entities in your inheritance, then try something as following instead:

SELECT r FROM Root r WHERE r.id IN ( SELECT c.id FROM Child c WHERE c.field = :value )

Gromme answered 12/11, 2013 at 23:50 Comment(0)
S
0

I had the same issue, and didn't find a solution without using separate queries for each subclass and merging them later on the application level.

One thing I'm sure, single table inheritance will not solve this, completely the same thing.

There is another alternative, although being logically dirty. Define all the fields (the ones you need) in the superclass. If the record logically doesn't have that field it will be empty. Not a pretty sight, but hey, more optimized than 2-3-4-... queries. Also in this scenario single table inheritance is definitely the better way to go

Schnorr answered 22/12, 2011 at 14:46 Comment(2)
Merging results is not exactly the case here, since I never need Films and Books to appear in the results of my query at the same time. The workaround I've finally come up with was to add the inverse side of the Auction-Item relationship and query for Items rather than Auctions ('SELECT i FROM Entities\Book INNER JOIN i.auction a WHERE (...)'). Since I know which subclass I'm looking for, I can build my query accordingly. Thanks for the reply, though, your idea could be a good way to go, as it seems there's little point in using inheritance with Doctrine in cases like this one.Nidus
That is a quite bad idea and it's more of a hack - you don't need to break your models to fix the DQL. As a last resort, you can always use native SQL at the price of maintaining some queries more.Axenic

© 2022 - 2024 — McMap. All rights reserved.