How do I get Yii2 to handle two different instances of a datapath?

This is siteController index.php. Here I want to display two lists from different tables on one page. But it displays only one table data for div.when I set only $ dataProvider in both kinds of divs and if I set $ dataProvider2 that are created in actionIndex (), it gets the error $ dataProvider2 not found.

                        <?= GridView::widget([
                        'dataProvider' => $dataProvider,
                        'columns' => [
                            ['class' => 'yii\grid\SerialColumn'],
                       [

                           'attribute'=>'practiceCode',
                           'format' => 'raw',
                           'value'=>function ($model, $index, $widget) {
                       },
                    ],
                    'firstName',

                        ],

              ]); ?>

            </div>
            <div class="col-lg-6" style="border:solid 1px;">
                <h2>Member</h2>
                <?= GridView::widget([
                        'dataProvider2' => $dataProvider2,//Error $dataProvider2 not found
                        'columns' => [
                            ['class' => 'yii\grid\SerialColumn'],
                       [

                           'attribute'=>'memberCode',
                           'format' => 'raw',
                           'value'=>function ($model, $index, $widget) {

                       },
                    ],
                    'firstName',
              ]); ?>    
            </div>

This is actionIndex () here I am writing two dataProvider ($ dataProvider, $ dataProvider2) But I cannot set this dataProvider to widget index.php

public function actionIndex()
    {
        $query = new \yii\db\Query;
        $query->select('*')
              ->from('practice');
        $query->createCommand();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
            'pageSize' => 10,
             ],
        ]);
        $query = new \yii\db\Query;
        $query->select('*')
              ->from('member');
        $query->createCommand();

        $dataProvider2 = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
            'pageSize' => 10,
             ],
]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,'dataprovider2'=>$dataProvider2 ]);
}
+4
source share
1 answer

You need to change dataProvider2todataProvider

<div class="col-lg-6" style="border:solid 1px;">
<h2>Member</h2>
<?= GridView::widget([
    'dataProvider' => $dataProvider2,//Error $dataProvider2 not found
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [

            'attribute'=>'memberCode',
            'format' => 'raw',
            'value'=>function ($model, $index, $widget) {

            },
        ],
        'firstName',
    ]
]); ?>

dataProvider GridView..

return $this->render('index', [
        'dataProvider' => $dataProvider,'dataProvider2'=>$dataProvider2 ]);
+6

Source: https://habr.com/ru/post/1620480/


All Articles