Yii2将参数应用于DB排序

时间:2016-10-03 15:16:16

标签: mysql gridview yii2

我的BlogPostSearch ActiveRecord中有以下搜索功能:

public function search($params)
{
    $query = BlogPost::find();

    $dataProvider = new ActiveDataProvider([
      'query' => $query,
      'sort' => [
        'attributes' => [
          'publishDate' => [
            'asc' => ['(CASE state 
                          WHEN '.static::STATE_ONLINE.' THEN published_at
                          WHEN '.static::STATE_OFFLINE.' THEN updated_at
                        END)' => SORT_ASC],
            'desc' => ['(CASE state 
                           WHEN '.static::STATE_ONLINE.' THEN published_at
                           WHEN '.static::STATE_OFFLINE.' THEN updated_at
                         END)' => SORT_DESC],
            'default' => SORT_DESC
          ],
        ],
      ],
    ]);

    return $dataProvider;
}

但是每次将参数连接到SQL时,我的灵魂都会死掉。是否可以在这里绑定变量?

类似的东西:

...
'asc' => [
  'expression'=> [
     '(CASE state 
         WHEN :online THEN published_at
         WHEN :offline THEN updated_at
       END)' => SORT_ASC
  ],
  'params' => [
    'online' => static::STATE_ONLINE,
    'offline' => static::STATE_OFFLINE,
  ],
], 
...

0 个答案:

没有答案