Symfony 2:使用学说查询构建器对非相关表进行 INNER JOIN [英] Symfony 2: INNER JOIN on non related table with doctrine query builder

查看:30
本文介绍了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

知道我做错了什么吗?

推荐答案

今天我在做类似的任务,记得我打开了这个 issue.我不知道它从哪个学说版本开始工作,但现在您可以轻松地在继承映射中加入子类.所以像这样的查询没有任何问题:

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.

这篇关于Symfony 2:使用学说查询构建器对非相关表进行 INNER JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆