简体   繁体   English

Symfony 2:使用doctrine查询构建器在非相关表上进行INNER JOIN

[英]Symfony 2: INNER JOIN on non related table with doctrine query builder

I'm trying to build a query with the doctrine query builder which joins a non related table like this: 我正在尝试使用doctrine查询构建器构建一个查询,该构建器连接一个非相关的表,如下所示:

$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. 错误:标识变量TPost在连接路径表达式中使用但之前未定义。

I searched for this error message and everybody answered to use the table alias + attribute like p.someAttribute. 我搜索了这个错误消息,每个人都回答使用表别名+属性,如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: 作为一个普通的mysql查询,我会这样写:

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. 因为当我开始这个问题时这是一个大问题,我认为对于其他不了解它的人来说这也很有趣。

Joins between entities without associations were not possible until version 2.4, where you can generate an arbitrary join with the following syntax: 在版本2.4之前,无法实现没有关联的实体之间的连接,您可以使用以下语法生成任意连接:

$query = $em->createQuery('SELECT u FROM User u JOIN Blacklist b WITH u.email = b.email');

Reference: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html 参考: http//docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html

$dql = "SELECT 
    a, md.fisrtName , md.LastName, mj
    FROM MembersBundle:Memberdata md
        INNER JOIN MembersBundle:Address a WITH md = a.empID
        INNER JOIN MembersBundle:Memberjob mj WITH md = mj.memberData
            ...
    WHERE
        a.dateOfChange IS NULL
    AND WHERE
        md.someField = 'SomeValue'";

return $em->createQuery( $dql )->getResult();

A DQL join only works if an association is defined in your mapping. 只有在映射中定义了关联时,DQL连接才有效。 In your case, I'd say it's much easier to do a native query and use ResultSetMapping to populate your objects. 在您的情况下,我会说,执行本机查询并使用ResultSetMapping来填充对象要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM