我想将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']);
},
],
]
]);
}
?>
答案 0 :(得分:1)
<强>控制器强>
Ref
<强>索引强>
public function actionIndex()
{
$query = new \yii\db\Query;
$dataProvider = new ArrayDataProvider([
'allModels' =>$query->from('business_main_categories')->all(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
答案 1 :(得分:0)
我在不使用gridview的情况下解决了我的问题。如下 -
我的SiteController -
public function actionIndex()
{
$searchModel = new BusinessMainCategoriesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->pageSize = $dataProvider->getTotalCount(); //-1 : disable
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
使用此代码我从db获取dataProvider中的所有记录。 (请注意,我在我的&#39; BusinessMainCategoriesSearch&#39;模型中使用ActiveDataProvider)
而且,我的index.php是 -
<?php
$m = $dataProvider->getModels();
foreach ($m as $dp) {
echo "<img src = '".$dp['bmc_image']."' />";
echo '<center><font color = "white">'.$dp['bmc_name'].'<font/></center>';
}
?>
对我来说很有用这是最简单的方法。