Doctrine DQL, class table inheritance and access to subclass fields

前端 未结 5 576
萌比男神i
萌比男神i 2020-12-29 06:15

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

I have an Entity called Auction, which is OneToOne relation with Item

5条回答
  •  萌比男神i
    2020-12-29 06:54

    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://stackoverflow.com/a/14854067/347063 . 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.

提交回复
热议问题