help working thru date range filter

I am following the instructions here http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#separate-filter-form

to create a simple search for dates in a range.

Instead of simple text fields, im just using kartik datepicker to select the dates




use kartik\date\DatePicker;


<?= $form->field($model, 'saleFrom')->widget(DatePicker::className(), [

        'options' => ['placeholder' => 'FROM'],

        'pluginOptions' => [

            'autoclose' => true,

            'format' => 'yyyy/mm/dd'

        ]

    ]); ?>




    <?= $form->field($model, 'saleTo')->widget(DatePicker::className(), [

        'options' => ['placeholder' => 'TO'],

        'pluginOptions' => [

            'autoclose' => true,

            'format' => 'yyyy/mm/dd'

        ]

    ]); ?>




As per docs, i included the fields in my search model and added the where filters to the query:





public $saleFrom;

public $saleTo;


$query->andFilterWhere(['>=', 'sale_date', $this->saleFrom])

      ->andFilterWhere(['<=', 'sale_date', $this->saleTo]);  




I vardumped the saleFrom/saleTo, and they correctly contain the dates i’ve selected via the widget.

Basically, nothing happens - page just refreshes and filters dont seem to work.

That query where filter is exactly as per docs - do i assume Yii just handles the date conversion and/or sql clause ?

Ie, mysql is storing the sale_date in format "2016-04-30", saleFrom/saleTo contains "2016/04/30" ?

Can anyone walk me through getting this to work, been stuck on it a couple of days.

thanks much

So I just hardcoded the dates as "2016-04-30", as per mysql store and it actually worked.

So in order to get it working, i changed the filter to check the $params like this:




if ($params['SaleSearch']['saleFrom'] && $params['SaleSearch']['saleTo']) {

       $query->andFilterWhere(['>=', 'sale_date', $params['SaleSearch']['saleFrom']])

             ->andFilterWhere(['<=', 'sale_date', $params['SaleSearch']['saleTo']]);

}



its not as elegant as the way the docs explain it, but it works - if anyone has any reasons why i shouldnt do it this way, please let me know. It seems as though the variables $this->saleFrom and $this->saleTo, are empty in the search function.

cheers