ProductSearch.php 4.08 KB
<?php
    
    namespace artbox\catalog\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use yii\helpers\VarDumper;

    /**
     * ProductSearch represents the model behind the search form about `artbox\catalog\models\Product`.
     */
    class ProductSearch extends Product
    {
    
        public $title;
        public $brand_title;
        public $category_title;
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [];
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'id',
                    ],
                    'integer',
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
                [
                    [
                        'title',
                        'brand_title',
                        'category_title',
                    ],
                    'string',
                    'max' => 255,
                ],
            ];
        }
        
        /**
         * @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 = Product::find()
                            ->joinWith('lang')
                            ->joinWith('brand.lang')
                            ->joinWith('category.lang');
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => $query,
                    'sort'  => [
                        'attributes' => [
                            'id',
                            'title'       => [
                                'asc'     => [ 'product_lang.title' => SORT_ASC ],
                                'desc'    => [ 'product_lang.title' => SORT_DESC ],
                                'default' => SORT_ASC,
                            ],
                            'brand_title' => [
                                'asc'     => [ 'brand_lang.title' => SORT_ASC ],
                                'desc'    => [ 'brand_lang.title' => SORT_DESC ],
                                'default' => SORT_ASC,
                            ],
                            'created_at',
                            'status',
                            'sort',
                        ],
                    ],
                ]
            );
            
            $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 $dataProvider;
            }
            
            // grid filtering conditions
            $query->andFilterWhere(
                [
                    'product.id'     => $this->id,
                    'product.status' => $this->status,
                ]
            )
                  ->andFilterWhere(
                      [
                          'like',
                          'product_lang.title',
                          $this->title,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'like',
                          'brand_lang.title',
                          $this->brand_title,
                      ]
                  )
                  ->andFilterWhere(
                      [
                          'category.id' => $this->category_title,
                      ]
                  );
            
            return $dataProvider;
        }
    }