简体   繁体   English

在Doctrine2的右表上合并LEFT OUTER JOIN和WHERE子句

[英]Combining LEFT OUTER JOIN and WHERE clause on right table in Doctrine2

I have the following query. 我有以下查询。

$objEmployee = $this->getEntityManager()
                        ->createQuery(
                                "SELECT e FROM MyProjectEntityBundle:Employee e LEFT OUTER JOIN e.project p  where p.name like % abc %" 
                        )->getResult();

which displays project name like 'abc'. 显示项目名称,例如“ abc”。 How can I display all the employee details of those who have a project which satisfies the condition and also other employees who does not have a project when the condition turns false? 当条件变为假时,我如何显示拥有满足条件的项目的员工的所有员工详细信息,以及没有项目的其他员工?

For Example. 例如。

Employee              Project
    a                     x
    b                     x
    c                     y
    d                     -

Employee 'a' and 'b' has project 'x'. 员工“ a”和“ b”的项目为“ x”。 Employee 'c' has project 'y' d does not have any project. 员工'c'有项目'y'd没有任何项目。 I have been able to retrieve the the employees who have a project. 我已经能够检索到拥有项目的员工。

Now how can i retrieve the employee who has project x and the employees who does not have any project using doctrine2 , createQuery ? 现在,我如何使用doctrine2 , createQuery检索具有项目x的雇员和没有任何项目的雇员?

Just think about how the result would look if you didn't have a WHERE-clause: The result of your left join between employees and projects will result in all your employee records to the left, and the matching projects to the right. 只需考虑一下如果没有WHERE子句的结果将是什么样子:员工与项目之间的左联接结果将导致所有员工记录位于左侧,而匹配的项目则位于右侧。 Any employees without any project at all will still appear to the left, but the project record will be empty. 完全没有任何项目的任何员工仍将出现在左侧,但项目记录将为空。

So try this for your DQL: 因此,请为您的DQL尝试以下操作:

$dql = "SELECT e FROM MyProjectEntityBundle:Employee e LEFT JOIN e.project p WHERE p.id IN :p_ids OR p.id IS NULL"
$query = $this->getEntityManager()->createQuery($dql);
$query->setParameter("p_id", "(" . implode(",", $p_ids) . ")");
$employees = $query->getResult()

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

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