Doctrine DQL,类表继承和对子类字段的访问 [英] Doctrine DQL, class table inheritance and access to subclass fields
问题描述
我有一个DQL查询和实体专业化的问题。
I have a problem with a DQL query and entity specialization.
我有一个实体名为 Auction
,这是 OneToOne
与项目
的关系。 项目
是一个 mappedSuperclass
电影
和书
。我需要一个可以返回搜索引擎的查询,允许用户使用不同的属性($ code>和搜索具有不同属性的商品(这是 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
因此,我需要访问电影
- 和预订
特定字段。用户将指定他们正在寻找的项目
类型,但我没有看到使用这些信息的方式,除了使用 INSTANCE OF
在我的DQL查询。
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:
类
Entities\Item
没有字段或关联名为bookTypes
Class
Entities\Item
has no field or association namedbookTypes
对于
,但对于项目
我也试过
SELECT a FROM Entities\Book i
INNER JOIN i.auction a ...
但是我认为主义要求我在 SELECT
和 FROM
语句。
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.
任何想法?
推荐答案
正如马特所说,原则项目不会修复的旧问题( DDC-16 )
As Matt stated, this is an old issue that Doctrine Project won't fix (DDC-16).
问题在于,教义的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很可能(假设 Entities\Book
是
Entities\Item
的子类):
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'
)
这是伪代码为您的问题。这不是很好,但请记住,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,类表继承和对子类字段的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!