2016-07-18 50 views
1

我一直在試圖從控制器傳遞模型數據的陣列到GridView到GridView,但我得到的錯誤:從controlller將數據傳遞到GridView失敗

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

這是控制器代碼:

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; 
    } 

} 

的視圖(assignunits.php)

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

?> 

可能是錯誤

回答

2

ActivedataProvider需要一個query。在你的情況下,你發送query(all())的結果。

刪除all()在您的query

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