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

2024-08-10php开发问题
0

本文介绍了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.

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17