Yii2未定义的索引

时间:2017-12-29 01:06:23

标签: yii2 yii2-basic-app

我的控制器:

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

    $query4 = (new \yii\db\Query())
    ->select(['monthsubmit', 'modeler'])
    ->from('sku3d')
    ->groupBy(['monthsubmit', 'modeler'])
    ->orderBy(['monthsubmit'=>SORT_DESC]);

    $query4->andFilterWhere(['like', 'monthsubmit', $params['monthsubmit']])
           ->andFilterWhere(['like', 'modeler', $params['modeler']]);

    $dataProvider4 = new ActiveDataProvider([
        'query' => $query4,
    ]);

我的观点:

<?php echo GridView::widget([
    'dataProvider' => $dataProvider4,
    'filterModel' => true,  
    'pjax'=>true,
    'panel' => [
        'type' => GridView::TYPE_PRIMARY,
        'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-user"></i>Submitted SKU by Month</h3>',
    ],
    'columns' => [
        // 'monthsubmit',
        [
        'attribute'=>'monthsubmit', 
        'filter' => Html::input('string', 'monthsubmit')
        'width'=>'310px',
        'group'=>true,  // enable grouping
    ],
        [
        'attribute'=>'modeler', 
        'width'=>'180px',
        'filter' => Html::input('string', 'modeler')
        'group'=>true,  // enable grouping
        ],  
    ]
]); 
?> 

我在我的控制器中创建了sqlDataProvider并且它正在工作。我的问题是当我尝试创建一个过滤器选项,因为我没有使用我的gridview的搜索模型,它返回错误Undefined index: monthsubmit

请告诉我我哪里错了。

谢谢。

1 个答案:

答案 0 :(得分:2)

在你的控制器中,你应该这样做

$modeler = Yii::$app->request->get('modeler');
$monthsubmit = Yii::$app->request->get('monthsubmit');

$query4 = (new \yii\db\Query())
    ->select(['monthsubmit', 'modeler'])
    ->from('sku3d')
    ->groupBy(['monthsubmit', 'modeler'])
    ->orderBy(['monthsubmit'=>SORT_DESC]);

$query4->andFilterWhere(['like', 'monthsubmit', $monthsubmit])
    ->andFilterWhere(['like', 'modeler', $modeler]);

$dataProvider4 = new ActiveDataProvider([
    'query' => $query4,
]);