| @@ 14-92 (lines=79) @@ | ||
| 11 | * | |
| 12 | * @package app\models | |
| 13 | */ | |
| 14 | class ArticleSearch extends Article | |
| 15 | { | |
| 16 | /** | |
| 17 | * @var string | |
| 18 | */ | |
| 19 | public $title; | |
| 20 | ||
| 21 | /** | |
| 22 | * @inheritdoc | |
| 23 | */ | |
| 24 | public function rules() | |
| 25 |     { | |
| 26 | return [ | |
| 27 | [ | |
| 28 | [ | |
| 29 | 'pageId', | |
| 30 | 'active', | |
| 31 | ], | |
| 32 | 'integer', | |
| 33 | ], | |
| 34 | [ | |
| 35 | 'title', | |
| 36 | 'string', | |
| 37 | ], | |
| 38 | ]; | |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 42 | * @inheritdoc | |
| 43 | */ | |
| 44 | public function scenarios() | |
| 45 |     { | |
| 46 | // bypass scenarios() implementation in the parent class | |
| 47 | return Model::scenarios(); | |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * Creates data provider instance with search query applied | |
| 52 | * | |
| 53 | * @param array $params | |
| 54 | * | |
| 55 | * @return ActiveDataProvider | |
| 56 | */ | |
| 57 | public function search($params) | |
| 58 |     { | |
| 59 | $query = Article::find(); | |
| 60 | ||
| 61 | // add conditions that should always apply here | |
| 62 | ||
| 63 | $dataProvider = new ActiveDataProvider([ | |
| 64 | 'query' => $query, | |
| 65 | ]); | |
| 66 | ||
| 67 | $this->load($params); | |
| 68 | ||
| 69 |         if (!$this->validate()) { | |
| 70 | // uncomment the following line if you do not want to return any records when validation fails | |
| 71 |             // $query->where('0=1'); | |
| 72 | return $dataProvider; | |
| 73 | } | |
| 74 | ||
| 75 | // grid filtering conditions | |
| 76 | $query->andFilterWhere([ | |
| 77 | 'pageId' => $this->pageId, | |
| 78 | 'active' => $this->active, | |
| 79 | ]); | |
| 80 | ||
| 81 | $query->andFilterWhere(['like', 'title', $this->title]); | |
| 82 | ||
| 83 | $pagination = new Pagination([ | |
| 84 | 'defaultPageSize' => Yii::$app->params['defaultPageSize'], | |
| 85 | 'totalCount' => $query->count(), | |
| 86 | ]); | |
| 87 | ||
| 88 | $dataProvider->setPagination($pagination); | |
| 89 | ||
| 90 | return $dataProvider; | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| @@ 14-92 (lines=79) @@ | ||
| 11 | * | |
| 12 | * @package app\models | |
| 13 | */ | |
| 14 | class ProductSearch extends Product | |
| 15 | { | |
| 16 | /** | |
| 17 | * @var string | |
| 18 | */ | |
| 19 | public $title; | |
| 20 | ||
| 21 | /** | |
| 22 | * @inheritdoc | |
| 23 | */ | |
| 24 | public function rules() | |
| 25 |     { | |
| 26 | return [ | |
| 27 | [ | |
| 28 | [ | |
| 29 | 'categoryId', | |
| 30 | 'active', | |
| 31 | ], | |
| 32 | 'integer', | |
| 33 | ], | |
| 34 | [ | |
| 35 | 'title', | |
| 36 | 'string', | |
| 37 | ], | |
| 38 | ]; | |
| 39 | } | |
| 40 | ||
| 41 | /** | |
| 42 | * @inheritdoc | |
| 43 | */ | |
| 44 | public function scenarios() | |
| 45 |     { | |
| 46 | // bypass scenarios() implementation in the parent class | |
| 47 | return Model::scenarios(); | |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * Creates data provider instance with search query applied | |
| 52 | * | |
| 53 | * @param array $params | |
| 54 | * | |
| 55 | * @return ActiveDataProvider | |
| 56 | */ | |
| 57 | public function search($params) | |
| 58 |     { | |
| 59 | $query = Product::find(); | |
| 60 | ||
| 61 | // add conditions that should always apply here | |
| 62 | ||
| 63 | $dataProvider = new ActiveDataProvider([ | |
| 64 | 'query' => $query, | |
| 65 | ]); | |
| 66 | ||
| 67 | $this->load($params); | |
| 68 | ||
| 69 |         if (!$this->validate()) { | |
| 70 | // uncomment the following line if you do not want to return any records when validation fails | |
| 71 |             // $query->where('0=1'); | |
| 72 | return $dataProvider; | |
| 73 | } | |
| 74 | ||
| 75 | // grid filtering conditions | |
| 76 | $query->andFilterWhere([ | |
| 77 | 'categoryId' => $this->categoryId, | |
| 78 | 'active' => $this->active, | |
| 79 | ]); | |
| 80 | ||
| 81 | $query->andFilterWhere(['like', 'title', $this->title]); | |
| 82 | ||
| 83 | $pagination = new Pagination([ | |
| 84 | 'defaultPageSize' => Yii::$app->params['defaultPageSize'], | |
| 85 | 'totalCount' => $query->count(), | |
| 86 | ]); | |
| 87 | ||
| 88 | $dataProvider->setPagination($pagination); | |
| 89 | ||
| 90 | return $dataProvider; | |
| 91 | } | |
| 92 | } | |
| 93 | ||