Symfony 2:INNER JOIN关于与教义查询构建器无关的表 [英] Symfony 2: INNER JOIN on non related table with doctrine query builder

查看:288
本文介绍了Symfony 2:INNER JOIN关于与教义查询构建器无关的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ query = $ this-> createQueryBuilder('gpr')
- > select('gpr,p')
- > innerJoin('TPost','p')
- > where('gpr.contentId = p.contentId')

但这不行。我仍然收到一个错误:


错误:标识变量TPost用于连接路径表达式,但未定义。


我搜索此错误消息,每个人都回答使用表别名+属性,如p.someAttribute。但是我想加入的表与表中没有关系,我开始我的选择。



作为一个正常的mysql查询,我将这样写:

  SELECT * FROM t_group_publication_rel gpr 
INNER JOIN t_post p
WHERE gpr.content_id = p.content_id

任何想法我做错了什么?

解决方案

今天我在做类似的工作,记得我打开了这个问题。我不知道从哪个教义版本起作用,但现在你可以轻松地加入子类中的继承映射。所以这样的查询工作没有任何问题:

  $ 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'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:

Error: Identification Variable TPost used in join path expression but was not defined before.

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.

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.

这篇关于Symfony 2:INNER JOIN关于与教义查询构建器无关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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