将数据从控制器传递到 gridview 失败
Passing data from a controller to a gridview fails with
我一直在尝试将模型数据数组从控制器传递到 gridview 再到 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
],
],]);
?>
有什么问题吗?
ActivedataProvider
需要 query
。在您的情况下,您发送 query(all())
.
的结果
删除 query
中的 all()
。
$query = Unitslocation::find()->where(['officelocationid'=>$id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
我一直在尝试将模型数据数组从控制器传递到 gridview 再到 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
],
],]);
?>
有什么问题吗?
ActivedataProvider
需要 query
。在您的情况下,您发送 query(all())
.
删除 query
中的 all()
。
$query = Unitslocation::find()->where(['officelocationid'=>$id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);