Symfony2首先按特定字段值排序

时间:2016-12-13 13:25:35

标签: sql postgresql symfony doctrine-orm query-builder

Symfony2当Case无效时。

想先按特定字段值排序

$query->select('s.firstName as first_name,s.lastName as last_name,sts.name as status_name')
            ->leftJoin('s.studentStatus', 'sts')
            ->where($query->expr()->orx(
                $query->expr()->like('s.internalId', ':internalId'),
            ))
            ->addSelect('(CASE WHEN s.storeID = :location THEN 1 else 2) AS HIDDEN storeOrder')
            ->setParameter('internalId', "%{$internalId}")
            ->setParameter('location', "{$location}")
            ->orderBy('storeOrder', 'ASC')
            ->setMaxResults((int) $limit);

首先要按列特定值排序,之后会有其他值。我有SQL查询,但它没有使用下面的语法

2 个答案:

答案 0 :(得分:1)

您可以添加隐藏字段storeOrder,然后按此字段排序:

$query->select('s.firstName as first_name,s.lastName as last_name,sts.name as status_name')
           ->leftJoin('s.studentStatus', 'sts')
            ->where($query->expr()->orx(
                $query->expr()->like('s.internalId', ':internalId'),
            ))
            ->addSelect('(CASE WHEN s.storeID = :location THEN 1 else 2) AS HIDDEN storeOrder')
            ->setParameter('name', "%{$term}%")
            ->setParameter('internalId', "%{$internalId}")
            ->setParameter('location', "{$location}")
            ->orderBy('storeOrder', 'ASC')
            ->setMaxResults((int) $limit);

答案 1 :(得分:0)

CASE构造是特定于供应商的,默认情况下不属于Doctrine 2。但是,您可以查看DoctrineExtensions。您可以将IfElse函数用于您的用例。