Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

php - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try this :

use DoctrineORMQueryExpr;

->leftJoin('a.installations', 'i', ExprJoin::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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...