php Symfony 2:使用学说查询构建器对非相关表进行 INNER JOIN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11116428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:46:37  来源:igfitidea点击:

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

phpmysqlsymfonydoctrinequery-builder

提问by 0s1r1s

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.

错误:在连接路径表达式中使用了标识变量 TPost,但之前未定义。

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.

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

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?

任何想法我做错了什么?

回答by 0s1r1s

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.

因为当我开始这个问题时这是一个大问题,我认为它对其他不了解它的人来说也很有趣。

回答by Yamir

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 版之前是不可能的,在 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

回答by Jzapata

$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();

回答by Peter Kruithof

A DQL join only works if an association is defined in your mapping. In your case, I'd say it's much easier to do a native query and use ResultSetMappingto populate your objects.

只有在映射中定义了关联时,DQL 连接才有效。在您的情况下,我会说执行本机查询并使用ResultSetMapping填充对象要容易得多。