Symfony2.3 Doctrine Query Builder 复杂查询

Symfony2.3 Doctrine Query Builder complex query

我正在 Doctrine Query Builder 中寻找与此 SQL SELECT 语句等效的语句:

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' )

我这样试过:

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'
            ));
}

它没有return 预期的结果。谁能帮帮我吗?感谢您提前抽出时间。

这应该是等价的。

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']);

我遵循了 doctrine documentation on the QueryBuilder 并找到了解决方案。在这里:

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'
                ));
}