Search code examples
mysqlsymfonydoctrinedql

Symfony2 doctrine nested join relation


I want to do something like this with my Query:

SELECT * FROM `Parent` P 
LEFT JOIN `child` C ON P.id = C.parentID
LEFT JOIN `childOfChild` CoC ON C.id = CoC.cID
WHERE P.id = 1

I try this QueryBuilder but don't work.

$query = $this->getEntityManager()
    ->createQueryBuilder()
    ->select('P,C,CoC')
    ->from('DspAppsBundle:Parent', 'P')
    ->leftJoin('P.child', 'C')
    ->leftJoin('C.childOfChild', 'CoC')
    ->where('P.id = 1')
    ->getQuery();

Can somebody tell me, how I cam make this work ?


Solution

  • Hi try something like this (you can replace getResult() with getSingleResult() or whatever suits you):

    $query = $this->getEntityManager()
        ->createQueryBuilder()
        ->select('P,C,CoC')
        ->from('DspAppsBundle:Parent', 'P')
        ->leftJoin('DspAppsBundle:Child', 'C', 'WITH', 'P.id = C.parentId')
        ->leftJoin('DspAppsBundle:ChildOfChild', 'CoC', 'WITH', 'C.id = CoC.cId')
        ->where('P.id = 1')
        ->getQuery()
        ->getResult();