Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left join ON condition AND other condition syntax in Doctrine

I'm using Doctrine's querybuilder in Symfony2 to create a query to fetch entities.

My current code looks like this:

$repository = $this->getDoctrine()->getRepository('AaaBundle:Application');      $queryBuilder = $repository->createQueryBuilder('a');     $queryBuilder         ->addSelect('u')         ->addSelect('i')         ->orderBy('a.created_date', 'DESC')         ->leftJoin('a.created_by', 'u')         ->leftJoin('a.installations', 'i')         //->where('i.page = :page')         //->setParameter('page', $found)         ; 

Now I can use this to get all the pages regardless of them having an installation or not. But I only want to join them it the $found page is available (so that if there is an installation for the app, but it's on another page, the installation wont be joined). If I unquote the where clause, it will show only apps that have an installation for the page. I want all apps with or without installations for the page.

In SQL I can get this by adding AND to the join

LEFT JOIN installations i ON a.id = i.app AND i.page = :page 

This way I get the installation info for an app that has an installation on the page, but I get null values on the columns for app's that have installations on other pages or not at all.

Is there a way to do this in Doctrine or am I better off just getting all the installations for each application and then checking against the found page with php?

like image 520
juuga Avatar asked Oct 04 '13 15:10

juuga


People also ask

Which condition is applied when using the left join?

The condition in the WHERE clauseis applied so that the statement only retrieves the data from the US, UK, and China rows. Because we use the LEFT JOIN clause, all rows that satisfy the condition in the WHERE clause of the countries table are included in the result set.

Where does the left join clause appear in SQL?

The LEFT JOIN clause appears after the FROM clause. The condition that follows the ON keyword is called the join condition B.n = A.n SQL LEFT JOIN examples SQL LEFT JOIN two tables examples

What is the condition in the where clause?

The condition in the WHERE clause is applied so that the statement only retrieves the data from the US, UK, and China rows. Because we use the LEFT JOIN clause, all rows that satisfy the condition in the WHERE clause of the countries table are included in the result set.

What is the inner join clause in SQL?

Introduction to SQL LEFT JOIN clause In the previous tutorial, you learned about the inner join that returns rows if there is, at least, one row in both tables that matches the join condition. The inner join clause eliminates the rows that do not match with a row of the other table.


1 Answers

You can try this :

use Doctrine\ORM\Query\Expr;  ->leftJoin('a.installations', 'i', Expr\Join::WITH, 'i.page = :page') ->setParameter('page', $page) 

See function leftJoin in doc: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#high-level-api-methods

like image 162
zapcost Avatar answered Sep 18 '22 16:09

zapcost