Yii2回显变量外部数据提供者到gridview

时间:2017-12-14 07:24:26

标签: php gridview yii2 yii2-basic-app

<?php
$query1 = (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'])->orWhere(['status' => 'Cancelled'])
        ->groupBy('c_date', 'status');

        $dataProvider1 = new ActiveDataProvider([
            'query' => $query1,
    ]);

        echo GridView::widget([
    'dataProvider' => $dataProvider1,
    'columns' => [
        'c_date:text:Month',
         [
        'header'=>'Cancelled SKU',
        'value' => function($data) {
              return Sku3d::getCancel();
         }
    ],
        'sku:text:Total Sku',
        'total:text:Total hours',

    ]
]); 

?>

我的模特:

public function getCancel()
{
    return (new \yii\db\Query())
    ->select(["DATE_FORMAT(approvedate, '%m-%Y') as c_date"])
    ->from('sku3d')
    ->where(['status' => 'Cancelled'])->groupBy('c_date')->count();
    }

我使用上面的代码将所有已批准的产品和已取消的产品计为总数。我需要计算每月取消sku 的数量并将其列在另一个列中,现在它列出所有没有按月分组。示例:11月有1个已取消的sku(2小时)和1个已批准的sku(4个小时),并且在12月有1个已批准的sku,未取消,应显示:

Month   Cancelled   Sku  Hours
11-2017    1         2     6
12-2017    0         1     3

现在它显示:

Month   Cancelled   Sku  Hours
11-2017    1         2     6
12-2017    1         1     3

我被困在这里,请指教。 感谢。

1 个答案:

答案 0 :(得分:0)

在您看来

echo GridView::widget([
    'dataProvider' => $dataProvider1,
    'columns' => [
        'c_date:text:Month',
        [
            'header'=>'any title',
            'value' => function($data) {
                  return ModelName::getCountSomething();
             }
        ],
        'sku:text:Total Sku',
        'total:text:Total hours',
    ]
]); 

在你的模特中:

public static function getCountSomething()
{
    return (new \yii\db\Query())
    ->select('*')
    ->from('sku3d')
    ->where(['status' => 'Cancelled'])
    ->count();

}

类似的东西。