沃梦达 / 编程问答 / php问题 / 正文

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

Doctrine DQL, class table inheritance and access to subclass fields(Doctrine DQL、类表继承和访问子类字段)

本文介绍了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、类表继承和访问子类字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Doctrine DQL、类表继承和访问子类字段

基础教程推荐