S
S
Silverviql2018-10-01 12:33:38
Yii
Silverviql, 2018-10-01 12:33:38

How to properly organize search by date range in yii2?

view.

<?php

/* @var $this yii\web\View */
/* @var $searchModel app\models\ZakazSearch */

use yii\helpers\Html;
use kartik\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\data\ActiveDataProvider;
use frontend\models\NumberColumn;
use kartik\widgets\DateTimePicker;

$dataProvider = new ActiveDataProvider([
    'query' => \app\models\Zakaz::find()
        ->where('action <= 0') ->andWhere(['>=', 'date_close', '2018-08-01 00:00:00'])->
andWhere(['<=', 'date_close', '2018-09-17 00:00:00']),
    'pagination' => [
        'pageSize' => 10,
    ],
]);

?>

 <?php $date_yesterday = new DateTime('-1 day');

    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'showFooter' => true,
        'filterModel'=>$searchModel,
        'showPageSummary'=>false,
        'pjax'=>true,
        'striped'=>true,
        'hover'=>true,
        'panel'=>['type'=>'primary', 'heading'=>$date_yesterday->format('d-M')],
        'columns' => [
            [
                'attribute' => 'date_close',
            ],
            [
                'attribute' => 'id_zakaz',
            ],
            [
                'attribute' => 'description',
            ],
            [
                'attribute' => 'fact_oplata',
            ],
            [
                'attribute' => 'id_shop',
                'value'=> function ($model) {
                    switch ($model->id_shop) {
                        case 2;
                            return 'Московский';
                        case 5;
                            return 'Админ';
                        case 6;
                            return 'Пушкина';
                        case 9;
                            return 'Сибирский';
                        case 12;
                            return 'Четаева';
                        case 16;
                            return 'Маркса';
                    }
                    return null;
                }
            ],
        ],
    ]);;?>

I'm thinking of adding 2 DatePicker fields to select dates, and so do the selection by date range.
Is it correct to make 2 variables and save dates in them. And then pass to the andWhere fields
$dataProvider = new ActiveDataProvider([
    'query' => \app\models\Zakaz::find()
        ->where('action <= 0') ->andWhere(['>=', 'date_close', '2018-08-01 00:00:00'])
->andWhere(['<=', 'date_close', '2018-09-17 00:00:00']),
    'pagination' => [
        'pageSize' => 10,
    ],
]);

Or do as in this example https://xn--d1acnqm.xn--j1amh/%D0%B7%D0%B0%D0%BF%D...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-10-01
@webinar

I'm thinking of adding 2 DatePicker fields to select dates, and so do the selection by date range.

And what's stopping you?
What's the question? Is it possible to write data to variables in order to receive them from there? Yes, that's what they actually exist for.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question