2015-06-18 59 views
0

我加入2個表像下: $model = SalesEntry::find() ->joinWith('salesItems') ->all(); yii2加入模型作爲數據提供程序

然後在視圖中使用的DataProvider類似以下內容: GridView::widget([ 'dataProvider' => $model, 'columns' => [ 'date', // sample field from first table to see if ok ], ]);

和我有以下錯誤:

Call to a member function getCount() on a non-object

我在這裏做錯了什麼?

回答

3

這是因爲ActiveQuery實例不是小部件所期望的DataProvider。您需要將其包裝在ActiveDataProvider中才能正常工作:

GridView::widget([ 
    'dataProvider' => new \yii\data\ActiveDataProvider(['query' => $model]), 
    ' columns' => [ 
     'date', // sample field from first table to see if ok 
    ], 
]); 
0

的DataProvider在GridView的應該是yii\data\DataProviderInterface

實例見docs

相關問題