Symfony 2:使用理论查询构建器在非相关表上进行 INNER JOIN
我正在尝试使用原则查询构建器构建一个查询,该查询构建器连接一个不相关的表,如下所示:
I'm trying to build a query with the doctrine query builder which joins a non related table like this:
$query = $this->createQueryBuilder('gpr')
->select('gpr, p')
->innerJoin('TPost', 'p')
->where('gpr.contentId = p.contentId')
但这不起作用.我仍然收到错误:
But this doesn't work. I still get an error:
错误:连接路径表达式中使用了标识变量 TPost,但之前未定义.
Error: Identification Variable TPost used in join path expression but was not defined before.
我搜索了此错误消息,每个人都回答使用表别名 + 属性,如 p.someAttribute.但我要加入的表与我开始选择的表无关.
I searched for this error message and everybody answered to use the table alias + attribute like p.someAttribute. But the table I want to join isn't related in the table I start my select from.
作为一个普通的mysql查询,我会这样写:
As a normal mysql query i would write it like this:
SELECT * FROM t_group_publication_rel gpr
INNER JOIN t_post p
WHERE gpr.content_id = p.content_id
任何想法我做错了什么?
Any ideas what i'm doing wrong?
推荐答案
今天我正在处理类似的任务,并记得我打开了这个问题.我不知道它是从哪个学说版本开始工作的,但现在您可以轻松地将子类加入继承映射中.所以像这样的查询没有任何问题:
Today I was working on similar task and remembered that I opened this issue. I don't know since which doctrine version it's working but right now you can easily join the child classes in inheritance mapping. So a query like this is working without any problem:
$query = $this->createQueryBuilder('c')
->select('c')
->leftJoin('MyBundleName:ChildOne', 'co', 'WITH', 'co.id = c.id')
->leftJoin('MyBundleName:ChildTwo', 'ct', 'WITH', 'ct.id = c.id')
->orderBy('c.createdAt', 'DESC')
->where('co.group = :group OR ct.group = :group')
->setParameter('group', $group)
->setMaxResults(20);
我在使用继承映射的父类中启动查询.在我之前的帖子中,如果我没记错的话,这是一个不同的起点,但同样的问题.
I start the query in my parent class which is using inheritance mapping. In my previous post it was a different starting point but the same issue if I remember right.
因为当我开始这个问题时这是一个大问题,所以我认为对于其他不了解它的人来说也可能很有趣.
Because it was a big problem when I started this issue I think it could be also interesting for other people which don't know about it.
相关文章