ImportersCheckPriceSearch.php 1.94 KB
<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Importers;

/**
 * ImportersSearch represents the model behind the search form about `backend\models\Importers`.
 */
class ImportersCheckPriceSearch extends Importers
{
    /**
     * @inheritdoc
     */

    public $date_to;

    public function rules()
    {

        return [
            [['price_date_update', 'date_to'], 'string', 'max' => 10],
            [ 'id', 'integer'],
        ];


    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {

        $query = Importers::find()->where(['active' => true])->orderBy(['price_date_update' => SORT_DESC]);
        $provider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);


        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $provider;
        }
        $query->andFilterWhere([
            'id' => $this->id]);

        if( $this->price_date_update != null || $this->date_to != null ){

            $date_from = \Yii::$app->converter->convertTo( 'timestamp', $this->price_date_update );
            $date_to = \Yii::$app->converter->convertTo( 'timestamp', $this->date_to, ['begin_of_the_day' => false] );

            $query->andFilterWhere([
                'between', new yii\db\Expression('UNIX_TIMESTAMP(price_date_update)'), $date_from, $date_to
            ]);
        }

        return $provider;
    }
}