本文介绍了Yii2 ActiveDataProvider - 为 foreach() 提供的参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在调用 project/index 控制器时遇到 Invalid argument provided for foreach() 错误.似乎是什么问题?
这是模型中的搜索:
公共函数搜索($params){$query = Projects::find();//添加应始终适用于此处的条件$dataProvider = new ActiveDataProvider(['查询' =>$查询,]);$this->load($params);如果 (!$this->validate()) {//如果您不想在验证失败时返回任何记录,请取消注释以下行//$query->where('0=1');返回 $dataProvider;}//网格过滤条件$query->andFilterWhere(['id' =>$this->id,'created_at' =>$this->created_at,'updated_at' =>$this->updated_at,]);$query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'description', $this->description]);返回 $dataProvider;
控制器:
公共函数 actionIndex(){$searchModel = new ProjectsQuery();$dataProvider = $searchModel->search(Yii::$app->request->queryParams);返回 $this->render('index', ['搜索模型' =>$搜索模型,'数据提供者' =>$数据提供者,]);}
查看文件:
<h1><?= Html::encode($this->title) ?></h1><?php echo $this->render('_search', ['model' => $searchModel]);?><p><?= Html::a('Create Projects', ['create'], ['class' => 'btn btn-success']) ?></p><?= GridView::widget(['数据提供者' =>$数据提供者,'过滤器模型' =>$搜索模型,'过滤器位置' =>'标题','列' =>[['类' =>'yii\grid\SerialColumn'],'姓名','说明:ntext','created_at:date','updated_at:date',['类' =>'yii\grid\ActionColumn'],],]);?>
解决方案
尝试使用
公共函数 actionIndex(){$searchModel = new ProjectsSearch();
代替
$searchModel = new ProjectsQuery();
I am getting Invalid argument supplied for foreach() error when I call project/index controller. What seems to be the problem?
This is the search in the model:
public function search($params)
{
$query = Projects::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
Controller:
public function actionIndex()
{
$searchModel = new ProjectsQuery();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
View file:
<div class="projects-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Projects', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'filterPosition' => 'header',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'description:ntext',
'created_at:date',
'updated_at:date',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
解决方案
Try using
public function actionIndex()
{
$searchModel = new ProjectsSearch();
Instead of
$searchModel = new ProjectsQuery();
这篇关于Yii2 ActiveDataProvider - 为 foreach() 提供的参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!