简体   繁体   中英

How to received data from dataProvider and SqlDataProvider directly on the view in Yii2?

I have the following code on the view, however when I called to $dataProvider from a GridView::widget I received null. The query works correctly running directly on the database. What could be wrong? Thanks.

//SQL Statement
   $dataProvider = new SqlDataProvider([
        'sql' => 'SELECT a.att1,
                         b.att2,
                         a.att3
                           FROM table1 a,
                                table2 b 
                                WHERE b.id = a.user_id
                                 ORDER BY a.att3 ASC',
                    ]);

//GridView
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'Att1',
        'Att2',
        'Att3',
    ],
]); 

You should use the same name for sql and gridview column

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT a.att1 Att1,
                     b.att2 Att2,
                     a.att3 Att3
                       FROM table1 a,
                            table2 b 
                            WHERE b.id = a.user_id
                             ORDER BY a.att3 ASC',
                ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM