doutao1282
2015-03-11 08:44
浏览 66
已采纳

Symfony2.3 Doctrine Query Builder复杂查询

I am looking for the equivalent of this SQL SELECT statement in Doctrine Query Builder:

SELECT p.*
FROM position p, fonction f
WHERE ( (p.id = f.position_id) AND (p.type ='MONO_MEMBRE') AND (f.date_fin IS NOT NULL) )
OR ( p.type='MULTI_MEMBRE' )

I tried this way :

function(PositionRepository $er) {
    return $er->createQueryBuilder('p')
        ->leftJoin('p.fonctions', 'f', 'WITH', '(f.dateFin IS NOT NULL) AND (p.type= :type_mono)')
        ->orWhere('p.type = :type_multi')
        ->setParameters(array(
            'type_multi' => 'MULTI_MEMBRE',
            'type_mono'  => 'MONO_MEMBRE'
            ));
}

It doesn't return the expected results. Can anyone help me please? Thank you for your time in advance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpo15099 2015-03-11 15:52
    已采纳

    I followed the doctrine documentation on the QueryBuilder and I found the solution. Here it is :

    function(PositionRepository $er) {
        $qb= $er->createQueryBuilder('p')
                ->leftJoin('p.fonctions', 'f');
    
        $andModule = $qb->expr()->andX();
        $andModule->add($qb->expr()->isNotNull('f.dateFin'));
        $andModule->add($qb->expr()->eq('p.type', ':mono'));    
    
        return $qb->where('f IS NULL')
                  ->orWhere('p.type = :multi')
                  ->orWhere($andModule)
                  ->setParameters(array(
                    'mono' => 'MONO_MEMBRE',
                    'multi' => 'MULTI_MEMBRE'
                    ));
    }
    
    点赞 评论
  • dongzhan1570 2015-03-11 14:19

    This should be equivalent.

    return $er->createQueryBuilder('p')
        ->leftJoin('p.fonctions', 'f')
        ->where('p.type = :mono')
        ->andWhere('f.date_fin IS NOT NULL')
        ->orWhere('p.type = :muli')
        ->setParameter(['mono' => 'MONO_MEMBRE', 'multi' => 'MULTI_MEMBRE']);
    
    点赞 评论

相关推荐 更多相似问题