Doctrine 2 DQL - how to select inverse side of unidirectional many-to-many query?(Doctrine 2 DQL - 如何选择单向多对多查询的反面?)
问题描述
我有两个类 - Page 和 SiteVersion,它们具有多对多关系.只有 SiteVersion 知道这种关系(因为站点是模块化的,我希望能够删除并放入 SiteVersion 所属的模块).
I have two classes - Page and SiteVersion, which have a many to many relationship. Only SiteVersion is aware of the relationship (because the site is modular and I want to be able to take away and drop in the module that SiteVersion belongs to).
因此,我将如何根据 SiteVersion 标准选择页面?
How would I therefore select pages based on criteria of SiteVersion?
例如,这不起作用:
SELECT p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'
我得到错误:
[DoctrineORMQueryQueryException]
[Semantical Error] line 0, col -1 near 'SELECT p FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.
即使我可以使用此查询选择v".
Even though I can select "v" with this query.
我想我可以通过为关系引入一个类(PageToVersion 类)来解决这个问题,但是有没有办法不这样做,或者让它成为双向的?
I think I could possibly resolve this by introducing a class for the relationship (a PageToVersion class) but is there any way without doing that, or making it bidirectional?
推荐答案
Doctrine ORM 中有两种处理方法.最典型的是使用带有子查询的 IN
条件:
There's two ways of handling this in Doctrine ORM. The most typical one is using an IN
condition with a subquery:
SELECT
p
FROM
SitePage p
WHERE
p.id IN(
SELECT
p2.id
FROM
SiteVersion v
JOIN
v.pages p2
WHERE
v.id = :versionId
AND
p.slug = :slug
)
另一种方法是与 ORM 2.3 版本引入的任意连接功能:
SELECT
p
FROM
SitePage p
JOIN
SiteVersion v
WITH
1 = 1
JOIN
v.pages p2
WHERE
p.id = p2.id
AND
v.id = :versionId
AND
p2.slug = :slug
1 = 1
只是因为解析器的当前限制.
The 1 = 1
is just because of a current limitation of the parser.
请注意导致语义错误的限制是因为水合过程从所选实体的根开始.如果没有根,水合器没有关于如何折叠的参考 提取加入或加入结果.
Please note that the limitation that causes the semantical error is because the hydration process starts from the root of the selected entities. Without a root in place, the hydrator has no reference on how to collapse fetch-joined or joined results.
这篇关于Doctrine 2 DQL - 如何选择单向多对多查询的反面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Doctrine 2 DQL - 如何选择单向多对多查询的反面?


- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Laravel 仓库 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01