duandaotui5633
duandaotui5633
2016-07-18 12:15

将数据从控制器传递到gridview失败

已采纳

I have been trying to pass an array of model data from a controller to a gridview to a gridview but I get an error of:

The "query" property must be an instance of a class that implements the 
QueryInterface e.g. yii\db\Query or its subclasses.

This is the controller code:

 public function actionAddunits($id){

  $countUnits = Unitslocation::find()->where(['officelocationid'=>$id])->count();
    if(count($countUnits)>0){

   $dataProvider = new ActiveDataProvider([
        'query' =>Unitslocation::find()->where(['officelocationid'=>$id])->all()
    ]);

      return $this->render('assignunits', ['dataProvider'=>$dataProvider]);

    }else{
        return 0;
    }

}

The view (assignunits.php)

<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
    // ...
    [
        'class' => 'yii\grid\CheckboxColumn',
        // you may configure additional properties here
    ],
],]);

?>

What could be wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqudtskm49788 dqudtskm49788 5年前

    ActivedataProvider needs a query. In your case you send the result of the query(all()).

    Remove all() in your query.

    $query = Unitslocation::find()->where(['officelocationid'=>$id]);
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
    
    点赞 评论 复制链接分享