dton37910 2017-05-02 09:16
浏览 43

条件性地连接Doctrine2上的表

I seeked for an answer but anything in my particular case. I need to join contintionnally table with Doctrine 2, depends on value of a field I have to join on two different foreign keys, here my code :

$qb = $this->entityManager->createQueryBuilder();
    $qb ->select('s')
        ->from('AppBundle:MyTable', 's')
        ->join('s.firstJoin', 'o')
        ->join('s.secondJoin', 'd')
        ->join('AppBundle:joinedView', 'view', Join::WITH,
            "(CASE WHEN (d.secondJoinFK = 3) 
            THEN view.did = d.secondJoinFK 
            WHEN (d.secondJoinFK = 2) 
            THEN view.dvid = d.secondJoinFK END)")
        ->addSelect('d')
        ->where('s.endDate IS NULL');

However, with this request, Symfony tells me : [Syntax Error] line 0, col 203: Error: Expected Doctrine\ORM\Query\Lexer::T_ELSE, got '='

Moreover, I cannot use native query because I use PagerFanta for rendering template, and PagerFanta needs to have a ORM\Query on input and not ORM\NativeQuery or other.

Unfortunately, I do not have choice and must implement this request with these prerequisites.

Thanks by advance for your help,

  • 写回答

1条回答 默认 最新

  • dou91808 2017-05-03 10:53
    关注

    It is not possible to do the something like conditional join. You always need to join both tables and specify different join condition.

    You can do something like this:

            ->join('AppBundle:joinedView', 'view1', Join::WITH,
            "view1.did = 3") 
            ->join('AppBundle:joinedView', 'view2', Join::WITH,
            "view2.dvid = 2")
    

    but honestly I am not sure what are you trying to achieve. Joining tables without a condition which uses some joining column seems a bit pointless.

    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂