yii2排序不起作用

时间:2017-07-01 06:02:31

标签: sorting yii2

我想对使用id获取的数据进行排序,我想从控制器执行此操作但是它不能同时使用asc和desc,我在做错了吗?我希望有人可以帮助我,提前谢谢

public function actionIndex()
    {  
        $userid =  Yii::$app->User->id;
        $searchModel = new UservdoSearch();
        $video= new ActiveDataProvider(
        ['query'=>Uservdo::find()->where('user_id=:uid',['uid'=>$userid])],**['sort' =>['defaultOrder'=>['id'=>SORT_DESC]]]**
        );

         return $this->render('index', [
               'model' => $searchModel,'dataProvider'=>$video
      ]);

    }

2 个答案:

答案 0 :(得分:2)

你不应该使用' sort'作为一个数组元素删除['排序' ......]与'排序' ....

    public function actionIndex()
    {  
          $userid =  Yii::$app->User->id;
          $searchModel = new UservdoSearch();
          $video= new ActiveDataProvider(
          ['query'=> Uservdo::find()->where('user_id=:uid',['uid'=>$userid]),
           'sort' => ['defaultOrder'=>['id'=>SORT_DESC]],
          );

           return $this->render('index', [
                 'model' => $searchModel,'dataProvider'=>$video
        ]);

    }

答案 1 :(得分:0)

我得到答案兄弟,它应该在查询内部。谢谢你的帮助。答案是,['query'=>Uservdo::find()->where('user_id=:uid',['uid'=>$userid]),'sort' =>['defaultOrder'=>['id'=>SORT_DESC]]]