【问题标题】:LeftJoin Alias Creation issue without relationship with another table Doctrine SymfonyLeftJoin Alias Creation 问题与另一个表没有关系 Doctrine Symfony
【发布时间】:2017-06-13 08:45:07
【问题描述】:

我在表中遇到关于左连接查询的问题 与其他表没有关系,所以当我加入时 收到 500 内部服务器错误,但是当我与其他 2 有关系而不是工作的表。我试图创建别名 使用不同的方式,如 Leftjoin('BundleName:Entity', 'alias') 但不是 工作。我有5个表如下,关系定义为 下面。

客户:
id - 主键 客户名称 客户名称

site:    
id - Primary key
site name
site details    

report:    
id - Primary key
site_id - Foreign key of site table
client_id - Foreign key of client table
title
description

report_call_out    
id- Foreign key of Report table
call title
call detail

report_works    
id- Foreign key of Report table
works title
works detail

These are the relationship in my database so I am getting all list from Report table when I am doing Left Join with other table like report_call_out and report_works which has no relationship from report table than I am getting internal server error but without adding both table in Join, Rest of 2 tables(Client and site) join working fine.

Can you please guide me that where I am having issue.?

I am using below code to populate all list of records.   

  $qb = $this->createQueryBuilder('r')
                       ->leftJoin('r.client', 'c')
                       ->leftJoin('r.site', 's'); 

在此代码之后,我使用此代码 (echo $qb->getQuery()->getDQL();) 以 DQL 格式打印查询,但它没有显示与 report_call_out 表和 report_works 表的任何左连接。

但是,当我使用此代码 ($qb->getQuery()->getSQL();) 以 SQL 格式打印查询时,它将自动在查询中添加左联接,并使用两个表 report_call_out 和 report_works。

【问题讨论】:

  • 你在哪里打电话给$qb = $this->createQueryBuilder('r')?此外,“500 Internal Server Error”并不是一个非常详细的信息。我假设某个地方抛出了异常。请提供更多详细信息。
  • @W0rma,我从 ReportRepository 文件中调用 $qb = $this->createQueryBuilder('r') 。我想使用 report_call_out 表字段来过滤结果
  • 请提供有关所引发异常的更多详细信息。 “500 内部服务器错误”不是很有帮助
  • 当我为 report_call_out 添加 leftJoin 时,出现 500 错误

标签: php mysql symfony doctrine left-join


【解决方案1】:

你可以这样使用

$qb = $this->createQueryBuilder('r')
                       ->leftJoin('r.client', 'c')
                       ->leftJoin('AcmeReportBundle:report_call_out', 'rc','WITH','r.id = rc.id')
                       ->leftJoin('r.site', 's'); 

使用“WITH”连接外部表。 report_id 是 FK

【讨论】:

  • 我已经添加了上面的代码之后我正在做 $qb->getQuery()->getResult() 而不是得到 500 内部服务器错误。在 ReportCallOut.php 实体中没有定义 id 属性。
  • 您可以在 app_dev.php 环境中运行,因此它会显示确切的错误
  • SELECT r FROM AcmeBundle\Entity\Report r LEFT JOIN r.client c LEFT JOIN AcmeBundle:report_call_out rc WITH r.id = rc.id LEFT JOIN r.site s WHERE s.contractNumber LIKE :site_id ORDER BY r.id desc 这里是以 DQL 格式运行的查询
  • 能否分享您的report_call_out 实体路径和实体名称以及此表字段列表
  • 已经提到的字段列表...实体路径相同,实体名称为报表
猜你喜欢
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
相关资源
最近更新 更多