How to ORDER BY CASE in Doctrine2 (Symfony2)

前端 未结 4 638
鱼传尺愫
鱼传尺愫 2020-12-28 14:45

I want to run this query by using Doctrine in Symfony 2.3. But it seems like Doctrine does not understand CASE statement. Can anyone help? Thank you in advance!



        
4条回答
  •  不思量自难忘°
    2020-12-28 15:44

    I had similar issue, where i had to put a few number prefix'es on the top of result. So I resolved like this:

        $qb = $this->createQueryBuilder('numberPrefix');
        $qb
            ->select('country.code','numberPrefix.prefix')
            ->addSelect('
                (CASE WHEN country.code = :firstCountryCode THEN 1
                WHEN country.code = :secondCountryCode THEN 2
                WHEN country.code = :thirdCountryCode THEN 3
                WHEN country.code = :fourthCountryCode THEN 4
                ELSE 5 END) AS HIDDEN ORD')
            ->innerJoin('numberPrefix.country','country')
            ->orderBy('ORD, country.id')
            ->setParameters(
                [
                    'firstCountryCode' => $firstCountryCode,
                    'secondCountryCode' => $secondCountryCode,
                    'thirdCountryCode' => $thirdCountryCode,
                    'fourthCountryCode' => $fourthCountryCode,
                ]
            );
    

提交回复
热议问题