将搜索添加到自定义查询Yii2

时间:2019-01-28 06:37:06

标签: yii2 yii2-basic-app

我构建了一个自定义查询以对某些字段进行计数和求和,然后使用gridview显示数据。

这是我的查询:

$query2 = (new \yii\db\Query())
    ->select([
        'date', 
        'sum( IF(status = "Passed", 1, 0) ) AS passed', 
        'sum( IF(status = "Failed", 1, 0) ) AS failed', 
        'sum( IF(status = "On Hold", 1, 0) ) AS onhold', 
        'sum( IF(status <> "NULL", 1, 0) ) AS total'
    ])
    ->from('qa3d')->where(['week'=>$week])
    ->groupBy('date');

    $dataProvider2 = new ActiveDataProvider([
        'query' => $query2,

    ]);

我的表格视图:

<?= GridView::widget([
        'dataProvider' => $dataProvider2,
        // 'filterModel' => $searchModel,
        'headerRowOptions' => ['class' => 'kartik-sheet-style'],
        'filterRowOptions' => ['class' => 'kartik-sheet-style'],
        'panel' => [
            'type' => GridView::TYPE_PRIMARY,
            'heading' => 'Status Report by Analyst - Week to Date',
            ],
        'toolbar' =>  [
            '{toggleData}',
        ],
        'pjax' => true,
        'columns' => [

            'date',
            'passed',
            'failed',
            [
            'attribute' => 'onhold', 
            'label' => "On Hold",
            ],   
            'total',
        ],
    ]); ?>

我注释了filterModel,因为它不起作用。我如何像Gii CRUD生成器那样针对此自定义查询进行搜索?

请帮助我。

谢谢!

1 个答案:

答案 0 :(得分:0)

是的。您必须创建指向该页面操作的搜索表单,然后您必须决定如何转发参数。您可以将其放入搜索模型或获取并传递给您拥有的查询。 IT取决于您的查询在哪里。

$searchModel = new SomeSearchModel();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

或这样,然后访问数组元素。

$post = Yii::$app->request->queryParams;

在您的情况下,我认为您将搜索$week = $post['week'];