OptionSearch.php 3.09 KB
<?php
    
    namespace artbox\catalog\models;
    
    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use yii\db\ActiveQuery;
    
    /**
     * OptionSearch represents the abstract search model for Option models.
     */
    abstract class OptionSearch extends Option
    {
        
        public $value;
        
        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'id',
                        'sort',
                    ],
                    'integer',
                ],
                [
                    [
                        'status',
                    ],
                    'boolean',
                ],
                [
                    [
                        'value',
                    ],
                    'safe',
                ],
            ];
        }
        
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [];
        }
        
        /**
         * @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
         *
         * @param OptionGroup $group
         *
         * @return \yii\data\ActiveDataProvider
         */
        public function search($params, $group)
        {
            $query = $this->createQuery()
                          ->joinWith('lang');
            
            // add conditions that should always apply here
            
            $dataProvider = new ActiveDataProvider(
                [
                    'query' => $query,
                ]
            );
            $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(
                [
                    'id'     => $this->id,
                    'sort'   => $this->sort,
                    'status' => $this->status,
                ]
            );
            
            return $dataProvider;
        }
        
        /**
         * Return ActiveQuery for current model
         *
         * @return \yii\db\ActiveQuery
         */
        abstract protected function createQuery(): ActiveQuery;
    
        /**
         * @inheritdoc
         */
        public function getGroupId()
        {
        
        }
    
        /**
         * @inheritdoc
         */
        public function setGroupId(int $id)
        {
        
        }
    
        /**
         * @inheritdoc
         */
        public function getGroup(): ActiveQuery
        {
            return new ActiveQuery(self::className());
        }
    }