简体   繁体   English

如何在Doctrine2中按案例排序(Symfony2)

[英]How to ORDER BY CASE in Doctrine2 (Symfony2)

I want to run this query by using Doctrine in Symfony 2.3. 我想在Symfony 2.3中使用Doctrine来运行此查询。 But it seems like Doctrine does not understand CASE statement. 但似乎Doctrine不理解CASE声明。 Can anyone help? 有人可以帮忙吗? Thank you in advance! 先感谢您!

SELECT max(id) id, name
FROM cards
WHERE name like '%John%'
GROUP BY name
ORDER BY CASE WHEN name like 'John %' THEN 0
           WHEN name like 'John%' THEN 1
           WHEN name like '% John%' THEN 2
           ELSE 3
      END, name

If you are using createQueryBuilder then you can use like 如果您正在使用createQueryBuilder,那么您可以使用like

$query->addSelect("(CASE WHEN name like 'John %' THEN 0
           WHEN name like 'John%' THEN 1
           WHEN name like '% John%' THEN 2
           ELSE 3 END) AS HIDDEN ORD ");
$query->orderBy('ORD', 'DESC');

Note that you must have "HIDDEN". 请注意,您必须拥有“隐藏”。

You can do with doctrine native query as well. 您也可以使用doctrine本机查询。

CASE is vendor-specific and not supported natively by doctrine. CASE是特定于供应商的,并且不受原则本身的支持。

If the result is smallish, my recommendation is to pull the whole result set then sort the array. 如果结果很小,我的建议是拉出整个结果集然后对数组进行排序。

If the result set will be too large, you should write a native query and hydrate the entity. 如果结果集太大,则应编写本机查询并对实体进行水合。 See the Doctrine Documentation on Native SQL for more information on this. 有关此内容的更多信息,请参阅Native SQL上Doctrine文档 It looks scary, but makes sense once you walk through an example. 它看起来很可怕,但是一旦你走过一个例子就有意义了。

As a last resort, you could just bypass doctrine and use low-level native SQL. 作为最后的手段,您可以绕过doctrine并使用低级本机SQL。 See this post for details. 有关详细信息,请参阅此帖

I know Doctrine Extensions has an IfElse function that may work, but I haven't heard many success stories. 我知道Doctrine Extensions有一个可以工作的IfElse函数 ,但我没有听过很多成功案例。

如果没有关系,那么当按关系表或本地列进行排序时,这个工作对我来说是有用的:

$doctrineQuery->add('orderBy', '(CASE WHEN COUNT(relation_table.uid)>0 THEN relation_table.price ELSE current_table.generic_price END) ASC');

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM