yii2搜索中缺少必需的参数

时间:2018-08-12 16:11:25

标签: yii2

我要搜索股票直到特定日期。用户将填写此日期表格。用户输入日期字段的第一页是_formtilldate。 控制器动作-

public function actionIndex3() {
    //$enddate = '2018-03-01';
    $searchModel1 = new SellitemSearch();
    $dataProvider1 = $searchModel1->search ( Yii::$app->request->queryParams );
    //$searchModel2 = new PuritemtilldateSearch();
    //$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);

    return $this->render ( '_formtilldate' , [
                'model1' => $searchModel1 ,
                    //'enddate' => $enddate,
                    //'model2' => $searchModel2,
                    //'searchModel1' => $searchModel2,
    ] );
}

以及表单字段

<?php

$form = ActiveForm::begin ( [
            'id' => 'form-id' ,
            'action' => [ '/stock/sellitem/stocktilldate' , 'enddate' => $model1->enddate ] ,
            'method' => 'get' ,
            'enableClientScript' => false ,
        ] );
?>
<?=

$form->field ( $model1 , 'enddate' )->widget (
        DatePicker::className () , [
    // inline too, not bad
    'options' => [ 'placeholder' => 'End Date ...' , 'id' => 'enddate1' ] ,
    'inline' => false ,
    //'id' => 'startdate1',
    // modify template for custom rendering
    //'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
    'clientOptions' => [
        'autoclose' => true ,
        'todayHighlight' => true ,
        'format' => 'yyyy-mm-dd'
    ]
] );
?>

点击提交按钮后,搜索查询将运行- 控制器动作-

public function actionStocktilldate( $enddate ) {
    //$enddate = '2018-03-01';
    $searchModel1 = new SellitemtilldateSearch();
    $dataProvider1 = $searchModel1->search ( Yii::$app->request->queryParams );
    //$searchModel2 = new PuritemtilldateSearch();
    //$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);

    return $this->render ( 'indexstocktilldate' , [
                //'enddate' => $enddate,
                'searchModel1' => $searchModel1 ,
                'dataProvider1' => $dataProvider1 ,
                    //'searchModel2' => $searchModel2,          
                    //'dataProvider2' => $dataProvider2,
    ] );
}

搜索模型-

public function search( $params , $enddate ) {
    //$query = Sellitem::find();
    $subQuery1 = (new Query() )->select ( [ 'pi_upc' , 'sum(pi_qty) as purchased' ] )->from ( 'puritem' )->leftJoin ( 'pursum' , 'pursum.ps_id = puritem.psi_id' )->andwhere ( [ '<' , 'pursum.ps_date' , $enddate ] )->groupby ( 'pi_upc' );
    $subQuery2 = (new Query() )->select ( [ 'si_iupc' , 'sum(si_qty) as sold' ] )->from ( 'sellitem' )->leftJoin ( 'sellsum' , 'sellsum.ss_id = sellitem.si_ssid' )->andwhere ( [ '<' , 'sellsum.ss_date' , $enddate ] )->groupby ( 'si_iupc' );
    $subQuery3 = (new Query() )->select ( [ 'i_upc' , 'i_category' , 'i_brand' , 'i_desc' , 'i_unit' , 'i_buyprice' , 'coalesce(p.purchased,0) as purchased' ] )->from ( 'item' )->leftJoin ( [ 'p' => $subQuery1 ] , 'p.pi_upc = i_upc' );
    $query = (new Query() )->select ( [ 'tp.i_upc as upc' , 'tp.i_category as category' , 'tp.i_brand as brand' , 'tp.i_desc as description' , 'tp.i_unit as unit' , 'tp.purchased as purchased' , 'coalesce(ts.sold,0) as sold' , '(coalesce(purchased,0) - coalesce(sold,0)) as stock' , 'tp.i_buyprice as rate' , 'round(((coalesce(purchased,0) - coalesce(sold,0))*tp.i_buyprice),2) as value' ] )->from ( [ 'tp' => $subQuery3 ] )->leftJoin ( [ 'ts' => $subQuery2 ] , 'ts.si_iupc = tp.i_upc' );
    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
    'query' => $query ,
    'pagination' => [
    'pageSize' => 10000000000 ,
    ] ,
    ]);
    ......
}

现在,在url中,我可以看到参数已通过-

http://localhost/chayanika/frontend/web/index.php?r=stock%2Fsellitem%2Fstocktilldate&SellitemSearch%5Benddate%5D=2018-08-13

但是,我遇到了错误-

Bad Request (#400)
Missing required parameter: enddate

我找不到想念的地方。

我在这里取得了一些进步-

我刚刚了解到,URL中的日期来自表单中的“操作”。但这到目前为止并没有太大用处。

如果我稍微改变控制器动作,就会得到结果-

public function actionStocktilldate() {
    $enddate = date ( 'Y-m-d' );
    //$enddate = yii::$app->request->get('enddate');
    //var_dump($enddate); 
    $searchModel1 = new SellitemtilldateSearch();
    $dataProvider1 = $searchModel1->search ( Yii::$app->request->queryParams , $enddate );
    //$searchModel2 = new PuritemtilldateSearch();
    //$dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);

    return $this->render ( 'indexstocktilldate' , [
                //'enddate' => $enddate,
                'searchModel1' => $searchModel1 ,
                'dataProvider1' => $dataProvider1 ,
                    //'searchModel2' => $searchModel2,          
                    //'dataProvider2' => $dataProvider2,
    ] );
}

请告诉我如何将表格收集的日期传递给此控制器。

2 个答案:

答案 0 :(得分:0)

在您的网址中,我看不到enddate参数,只是看到:

frontend/web/index.php?r=stock/sellitem/stocktilldate&SellitemSearch[enddate]=2018-08-13"

这是SellitemSearch [enddate],而不是enddate 您应该将网址修改为

frontend/web/index.php?r=stock/sellitem/stocktilldate&enddate=2018-08-13" 或从$enddate函数中删除actionStocktilldate参数。

答案 1 :(得分:0)

如何在ActiveForm中处理action参数。

  

对于GET方法,将忽略操作中的查询参数。我们用   隐藏的字段以将其添加回去   提交,因此您的表单操作会转到正确的位置。

因此,如果您从浏览器查看表单源,则会创建一个名为enddate的隐藏输入,它将自动提交。

<form id="w0" action="http://localhost/chayanika/frontend/web/index.php?r=/stock/sellitem/stocktilldate" method="get">
<input type="hidden" name="enddate" value="2018-03-01"><button type="submit">Submit</button> 
</form>

原因

但是,在您的情况下,由于没有创建任何隐藏字段,因此未提交。为什么?因为您的$model->enddate的分配返回了null。确保保存了有效的时间戳或日期字符串。

跟踪

转到\yii\web\UrlManager function createUrl($params)的第395行。

如果您使用的是enablePrettyUrl=>true,它将进入第一个检查,并在foreach中将查询字符串参数附加为key,其中 value!==null

if($this->enablePrettyUrl){
            $cacheKey = $route . '?';
            foreach($params as $key => $value){
                if($value !== null){
                    $cacheKey .= $key . '&';
                }
            }

如果您使用的是enablePrettyUrl=>false,它将跳到最后一部分

 $url = "$baseUrl?{$this->routeParam}=" . urlencode($route);
        if(!empty($params) && ($query = http_build_query($params)) !== ''){
            $url .= '&' . $query;
        }

        return $url . $anchor;

它将在检查if(!empty($params) && ($query = http_build_query($params)) !== ''){上失败,因为 http_build_query($params) 为具有键值为空值的数组返回空字符串。

因此,请确保您已保存日期,并且当前模型没有将date保存为数据库中的空值