Search code examples
phpgridviewyii2yii-extensionsyii2-advanced-app

Passing data from a controller to a gridview fails with


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?


Solution

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