<tfoot id='FmPS1'></tfoot>

  1. <small id='FmPS1'></small><noframes id='FmPS1'>

    • <bdo id='FmPS1'></bdo><ul id='FmPS1'></ul>
    <legend id='FmPS1'><style id='FmPS1'><dir id='FmPS1'><q id='FmPS1'></q></dir></style></legend>

  2. <i id='FmPS1'><tr id='FmPS1'><dt id='FmPS1'><q id='FmPS1'><span id='FmPS1'><b id='FmPS1'><form id='FmPS1'><ins id='FmPS1'></ins><ul id='FmPS1'></ul><sub id='FmPS1'></sub></form><legend id='FmPS1'></legend><bdo id='FmPS1'><pre id='FmPS1'><center id='FmPS1'></center></pre></bdo></b><th id='FmPS1'></th></span></q></dt></tr></i><div id='FmPS1'><tfoot id='FmPS1'></tfoot><dl id='FmPS1'><fieldset id='FmPS1'></fieldset></dl></div>

      Doctrine DQL、类表继承和访问子类字段

      时间:2023-08-19
        <bdo id='KUpbO'></bdo><ul id='KUpbO'></ul>

        <small id='KUpbO'></small><noframes id='KUpbO'>

        • <i id='KUpbO'><tr id='KUpbO'><dt id='KUpbO'><q id='KUpbO'><span id='KUpbO'><b id='KUpbO'><form id='KUpbO'><ins id='KUpbO'></ins><ul id='KUpbO'></ul><sub id='KUpbO'></sub></form><legend id='KUpbO'></legend><bdo id='KUpbO'><pre id='KUpbO'><center id='KUpbO'></center></pre></bdo></b><th id='KUpbO'></th></span></q></dt></tr></i><div id='KUpbO'><tfoot id='KUpbO'></tfoot><dl id='KUpbO'><fieldset id='KUpbO'></fieldset></dl></div>
          1. <tfoot id='KUpbO'></tfoot>

              <tbody id='KUpbO'></tbody>

              • <legend id='KUpbO'><style id='KUpbO'><dir id='KUpbO'><q id='KUpbO'></q></dir></style></legend>
                本文介绍了Doctrine DQL、类表继承和访问子类字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 DQL 查询和实体专业化方面遇到问题.

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

                我有一个名为 Auction 的实体,它是 OneToOneItem 的关系.ItemFilmBookmappedSuperclass.我需要一个可以支持搜索引擎的查询,允许用户查找具有不同属性的拍卖 AND 销售具有不同属性的物品(这是 AND 部分具有挑战性的).

                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).

                问题是,即使 Auction 有一个指向 Item 的关联,我也需要访问 Film- 和 Book 特定字段.用户将指定他们正在寻找的 Item 类型,但除了在我的 DQL 查询中使用 INSTANCE OF 之外,我看不到任何使用此信息的方法.

                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 EntitiesAuction a
                    INNER JOIN a.item i 
                    INNER JOIN i.bookTypes b 
                    WHERE i INSTANCE OF EntitiesBook 
                    AND b.type = 'Fantasy' 
                    AND ...". 
                

                这样的查询会导致错误:

                Such a query results in an error saying that:

                Class EntitiesItem 没有名为 bookTypes

                Class EntitiesItem has no field or association named bookTypes

                对于Book 来说是假的,对于Item 来说是真的.

                which is false for Book, yet true for Item.

                我也试过

                SELECT a FROM EntitiesBook i 
                    INNER JOIN i.auction a ...
                

                但我认为 Doctrine 要求我在 SELECTFROM 语句中引用同一个实体.

                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.

                有什么想法吗?

                推荐答案

                正如 Matt 所说,这是 Doctrine Project 无法解决的老问题 (DDC-16).

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

                问题在于 Doctic 的 DQL 是一种静态类型语言,其内部结构具有一定的复杂性.

                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.

                如 DDC-16 所述,也确实不可能在不引起严重问题的情况下了解该属性属于哪个类,例如多个子类使用不同的列名定义相同的属性.

                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.

                如果您想在 CTI 或 JTI 的子类中过滤数据,您可以使用我在 https 中描述的技术://stackoverflow.com/a/14854067/347063 .这将您的 DQL 与所有涉及的子类结合起来.

                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.

                您的情况最可能需要的 DQL 是(假设 EntitiesBookEntitiesItem 的子类):

                The DQL you would need in your case is most probably (assuming that EntitiesBook is a subclass of EntitiesItem):

                SELECT
                    a
                FROM
                    EntitiesAuction a 
                INNER JOIN
                    a.item i
                INNER JOIN
                    i.bookTypes b
                WHERE
                    i.id IN (
                        SELECT 
                            b.id
                        FROM
                            EntitiesBook b
                        WHERE
                            b.type = 'Fantasy'
                    )
                

                这是您问题的伪代码.这并不好,但请记住,SQL 和 DQL 非常不同,遵循不同的规则.

                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.

                这篇关于Doctrine DQL、类表继承和访问子类字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:原则 2:查询结果作为关联数组 下一篇:Doctrine 2 - 如何添加自定义 DBAL 驱动程序?

                相关文章

                  <tfoot id='9F0UV'></tfoot>
                  <i id='9F0UV'><tr id='9F0UV'><dt id='9F0UV'><q id='9F0UV'><span id='9F0UV'><b id='9F0UV'><form id='9F0UV'><ins id='9F0UV'></ins><ul id='9F0UV'></ul><sub id='9F0UV'></sub></form><legend id='9F0UV'></legend><bdo id='9F0UV'><pre id='9F0UV'><center id='9F0UV'></center></pre></bdo></b><th id='9F0UV'></th></span></q></dt></tr></i><div id='9F0UV'><tfoot id='9F0UV'></tfoot><dl id='9F0UV'><fieldset id='9F0UV'></fieldset></dl></div>

                  <small id='9F0UV'></small><noframes id='9F0UV'>

                1. <legend id='9F0UV'><style id='9F0UV'><dir id='9F0UV'><q id='9F0UV'></q></dir></style></legend>

                    • <bdo id='9F0UV'></bdo><ul id='9F0UV'></ul>