2017-07-01 80 views
-1

我想排序使用ID獲取的數據,我想從控制器做到這一點,但它不是與asc和desc一起工作,在哪裏我犯了錯誤?我希望有人能幫助我,在此先感謝yii2排序不工作

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

你不應該使用「排序」作爲數組元素刪除[「排序」 ......]與「排序」 ....

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

    } 
+0

我已經自己嘗試過這一點,它會給出一個語法錯誤,意外「=>」( T_DOUBLE_ARROW),期待','或')' –

+0

錯誤]刪除... – scaisEdge

+0

感謝您的時間花花公子... –

0

我得到的答案兄弟,應該是裏面query.thanks你help.Answer是,['query'=>Uservdo::find()->where('user_id=:uid',['uid'=>$userid]),'sort' =>['defaultOrder'=>['id'=>SORT_DESC]]]