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'
                    ));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题