2017-06-20 69 views
0

我在视图上有下面的代码,但是当我从GridView ::小部件调用$ dataProvider时,我收到null。该查询可以在数据库上正确运行。什么可能是错的?谢谢。如何在Yii2的视图中直接从dataProvider和SqlDataProvider接收数据?

//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', 
    ], 
]); 

回答

0

你应该为SQL和GridView列

$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', 
       ]); 
+0

它的工作使用相同的名称。非常感谢你! –