doujiayao8433 2011-12-13 21:45
浏览 57
已采纳

Doctrine DQL,类表继承和对子类字段的访问

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?

  • 写回答

5条回答 默认 最新

  • douhan8892 2014-01-20 05:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错