<?php $sums = app\models\Sku3d::find()->select(["DATE_FORMAT(approvedate, '%m-%Y') as c_date", 'sum(totalhours) as total', 'count(sku) as sku'])
->where(['status' => 'Approved'])->groupBy('c_date')
->createCommand()
->queryAll();
foreach ($sums as $sum){ ?>
<tr>
<td><?=$sum['c_date'] ?></td>
<td><?=$sum['sku'] ?></td>
<td><?=$sum['total'] ?></td>
</tr>
<?php } ?>
我使用上面的代码来计算一个月内的总产品和总小时数。目前我使用表结构来显示数据。我可以使用yii2 gridview显示上面的数据吗?
感谢。
答案 0 :(得分:1)
见Data Providers section of Yii2 documentation。您可以创建Query对象并使用ActiveDataProvider或将查询结果放入数组并使用ArrayDataProvider。
<?php
$query = (new \yii\db\Query())
->select(["DATE_FORMAT(approvedate, '%m-%Y') as c_date", 'sum(totalhours) as total', 'count(sku) as sku'])
->from('sku3d')
->where(['status' => 'Approved'])
->groupBy('c_date');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'c_date:text:Month',
'total:text:Total hours',
'sku:text:Sku'
]
]);
?>