【发布时间】:2016-04-05 05:21:58
【问题描述】:
我想将ArrayDataProvider 与以下siteController 代码一起使用。我写了以下代码,但它不起作用。
这是我的 actionIndex :
public function actionIndex()
{
$query = new \yii\db\Query;
$query->select('*')->from('business_main_categories');
$query->createCommand();
$dataProviders = [];
foreach ($query as $category) {
$dataProviders[] = new ArrayDataProvider([
'allModels' => $category,
'sort' => false,
'pagination' => false,
]);
}
return $this->render('index', [
'dataProvider' => $dataProviders,
]);
}
并希望它在 gridView 中进行迭代。于是,我写了如下代码(不知道对不对):
这是我的 index.php:
<?php
$dataProviders[] = 'dataProvider';
foreach ($dataProviders as $dataProvider) {
echo GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'attribute' => 'bmc_image',
'format' => 'html',
'label' => '',
'value' => function ($data) {
return Html::img($data['bmc_image'],
['width' => '210px', 'height' => '190px']);
},
],
]
]);
}
?>
【问题讨论】:
-
PHP 致命错误 – yii\base\ErrorException : 在字符串上调用成员函数 getCount()。在 C:\xampp\htdocs\efa-webv1\vendor\yiisoft\yii2\widgets\BaseListView.php
-
谢谢。我会尝试并让您知道是否有效。
-
其实我知道如何使用gridview。但无法理解如何在循环中迭代它。所以,请帮我解决这个问题。
标签: php yii2 yii2-advanced-app