1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\page\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\Model; |
7
|
|
|
use yii\data\ActiveDataProvider; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* PageSearch represents the model behind the search form about `app\modules\page\models\Page`. |
11
|
|
|
*/ |
12
|
|
View Code Duplication |
class PageSearch extends Page |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritdoc |
16
|
|
|
*/ |
17
|
1 |
|
public function rules() |
18
|
|
|
{ |
19
|
|
|
return [ |
20
|
1 |
|
[['id'], 'integer'], |
21
|
|
|
[['key', 'title', 'content', 'description', 'created_at', 'updated_at'], 'safe'], |
22
|
|
|
]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
1 |
|
public function scenarios() |
29
|
|
|
{ |
30
|
|
|
// bypass scenarios() implementation in the parent class |
31
|
1 |
|
return Model::scenarios(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates data provider instance with search query applied |
36
|
|
|
* |
37
|
|
|
* @param array $params |
38
|
|
|
* |
39
|
|
|
* @return ActiveDataProvider |
40
|
|
|
*/ |
41
|
1 |
|
public function search($params) |
42
|
|
|
{ |
43
|
1 |
|
$query = Page::find(); |
44
|
|
|
|
45
|
|
|
// add conditions that should always apply here |
46
|
|
|
|
47
|
1 |
|
$dataProvider = new ActiveDataProvider([ |
48
|
|
|
'pagination' => [ |
49
|
1 |
|
'pageSize' => Yii::$app->params['grid']['itemsPrePage'], |
50
|
|
|
], |
51
|
1 |
|
'query' => $query, |
52
|
|
|
]); |
53
|
|
|
|
54
|
1 |
|
$this->load($params); |
55
|
|
|
|
56
|
1 |
|
if (!$this->validate()) { |
57
|
|
|
// uncomment the following line if you do not want to return any records when validation fails |
58
|
|
|
// $query->where('0=1'); |
|
|
|
|
59
|
|
|
return $dataProvider; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// grid filtering conditions |
63
|
1 |
|
$query->andFilterWhere([ |
64
|
1 |
|
'id' => $this->id, |
65
|
1 |
|
'created_at' => $this->created_at, |
66
|
1 |
|
'updated_at' => $this->updated_at, |
67
|
|
|
]); |
68
|
|
|
|
69
|
1 |
|
$query->andFilterWhere(['like', 'key', $this->key]) |
70
|
1 |
|
->andFilterWhere(['like', 'title', $this->title]) |
71
|
1 |
|
->andFilterWhere(['like', 'content', $this->content]) |
72
|
1 |
|
->andFilterWhere(['like', 'description', $this->description]); |
73
|
|
|
|
74
|
1 |
|
return $dataProvider; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.