Question

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'. 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'. Employee 'c' has project 'y' d does not have any project. 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?

Was it helpful?

Solution

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. 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 = "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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top