ProductSearch.php 4.19 KB
<?php
    
    namespace common\modules\product\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use yii\db\ActiveQuery;
    
    /**
     * ProductSearch represents the model behind the search form about
     * `common\modules\product\models\Product`.
     */
    class ProductSearch extends Product
    {
        
        public $brand_id;
        
        public $category_id;
        
        public $variant_sku;
        
        public function behaviors()
        {
            $behaviors = parent::behaviors();
            if(isset( $behaviors[ 'language' ] )) {
                unset( $behaviors[ 'language' ] );
            }
            return $behaviors;
        }
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [ 'variant_sku' ],
                    'safe',
                ],
                [
                    [
                        'brand_id',
                        'product_id',
                        'category_id',
                        'brand_id',
                    ],
                    'integer',
                ],
                [
                    [
                        'is_top',
                        'is_new',
                        'akciya',
                    ],
                    'boolean',
                ],
            ];
        }
        
        /**
         * @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();
            
            $query->joinWith([
                'categories',
                /*'variant'*/
            ])
                  ->joinWith([
                      'brand' => function($query) {
                          /**
                           * @var ActiveQuery $query
                           */
                          $query->joinWith('lang');
                      },
                  ]);
            
            $query->groupBy([ 'product.product_id' ]);
            
            $dataProvider = new ActiveDataProvider([
                'query' => $query,
            ]);
            
            if(!( $this->load($params) && $this->validate() )) {
                return $dataProvider;
            }
            
            $dataProvider->setSort([
                'attributes' => [
                    'brand_id' => [
                        'asc'     => [ 'brand_lang.name' => SORT_ASC ],
                        'desc'    => [ 'brand_lang.name' => SORT_DESC ],
                        'default' => SORT_DESC,
                        'label'   => 'Brand name',
                    ],
                ],
            ]);
            
            if(isset( $this->is_top )) {
                $query->andFilterWhere([
                    'is_top' => (bool) $this->is_top,
                ]);
            }
            if(isset( $this->is_new )) {
                $query->andFilterWhere([
                    'is_new' => (bool) $this->is_new,
                ]);
            }
            if(isset( $this->akciya )) {
                $query->andFilterWhere([
                    'akciya' => (bool) $this->akciya,
                ]);
            }
            $query->andFilterWhere([
                'product.brand_id'             => $this->brand_id,
                'product.product_id'           => $this->product_id,
                'product_category.category_id' => $this->category_id,
            ]);
            
            //        $query->andFilterWhere(['ilike', 'brand.name', $this->brand_name]);
            //        $query->andFilterWhere(['ilike', 'product_variant.sku', $this->variant_sku]);
            
            return $dataProvider;
        }
    }