在Yii中使用Sum Points订购?

时间:2016-05-10 09:52:31

标签: yii

 $resource_cnt = ShopVisituser::model()->findAll(array(
            'select' => '*, sum(points) as amt',
            'condition' => 'user_id=:user_id AND merchant_id=:merchant_id',
            'order' => 'amt DESC',
            'params' => array(':user_id' => $datas->user_id, ':merchant_id' => $datas->merchant_id))
        );

我无法通过总和(点数)来命令。为什么?如何在Yii中订购Sum(点数)。

1 个答案:

答案 0 :(得分:0)

在模型上添加功能,例如:

public function scopes() {
   return array(
      'byamt' => array('order' => 'amt DESC'),
   );
}

现在,您可以像这样使用您的查询:

$resource_cnt = ShopVisituser::model()->byamt()->findAll(array(
        'select' => '*, sum(points) as amt',
        'condition' => 'user_id=:user_id AND merchant_id=:merchant_id',
        'params' => array(':user_id' => $datas->user_id, ':merchant_id' => $datas->merchant_id))
    );