Doctrine2 LEFT JOIN with 2 conditions

后端 未结 1 1382
猫巷女王i
猫巷女王i 2020-12-19 03:19

I\'m trying to find a \'Product\' by ID, and to left join all it\'s \'Photo\' on two conditions: the locale AND the active state.

Here\'s my QueryBuilder :

相关标签:
1条回答
  • 2020-12-19 04:11

    For this problem a solution may be:

    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder();
    $qb
        ->select('p', 'pp')
        ->from('Product', 'p')
        ->leftJoin('p.photos', 'pp')
        ->leftJoin('pp.translations', 'ppt', Doctrine\ORM\Query\Expr\Join::WITH, $qb->expr()->andX(
            $qb->expr()->eq('ppt.locale', ':locale'),
            $qb->expr()->eq('ppt.active', ':active')
        ))
        ->where('p.id', ':productId')
        ->setParameters(
            array(
                'productId', $productId,
                'active', $active,
                'locale', $locale
            )
        );
    
        $query = $qb->getQuery();
        return $query->getResult(); // or ->getSingleResult();
    

    NOTE: this example is the way to do it in Symfony2 (2.3) entity repository

    0 讨论(0)
提交回复
热议问题