1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.writesdown.com/ |
4
|
|
|
* @copyright Copyright (c) 2015 WritesDown |
5
|
|
|
* @license http://www.writesdown.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace common\models\search; |
9
|
|
|
|
10
|
|
|
use common\models\Post as PostModel; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\base\Model; |
13
|
|
|
use yii\data\ActiveDataProvider; |
14
|
|
|
use yii\helpers\ArrayHelper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Post represents the model behind the search form about `common\models\Post`. |
18
|
|
|
* |
19
|
|
|
* @author Agiel K. Saputra <[email protected]> |
20
|
|
|
* @since 0.1.0 |
21
|
|
|
*/ |
22
|
|
|
class Post extends PostModel |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function rules() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
[['id', 'author', 'type', 'comment_count'], 'integer'], |
31
|
|
|
[ |
32
|
|
|
[ |
33
|
|
|
'title', |
34
|
|
|
'excerpt', |
35
|
|
|
'content', |
36
|
|
|
'date', |
37
|
|
|
'modified', |
38
|
|
|
'status', |
39
|
|
|
'password', |
40
|
|
|
'slug', |
41
|
|
|
'comment_status', |
42
|
|
|
'username', |
43
|
|
|
], |
44
|
|
|
'safe', |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
public function scenarios() |
53
|
|
|
{ |
54
|
|
|
// bypass scenarios() implementation in the parent class |
55
|
|
|
return Model::scenarios(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Creates data provider instance with search query applied |
60
|
|
|
* |
61
|
|
|
* @param array $params |
62
|
|
|
* @param int $type |
63
|
|
|
* @param null|string $user |
64
|
|
|
* |
65
|
|
|
* @return ActiveDataProvider |
66
|
|
|
*/ |
67
|
|
|
public function search($params, $type, $user = null) |
68
|
|
|
{ |
69
|
|
|
$query = PostModel::find(); |
70
|
|
|
$query->innerJoinWith(['postAuthor'])->from(['post' => static::tableName()]); |
71
|
|
|
$query->andWhere(['type' => $type]); |
72
|
|
|
|
73
|
|
|
if ($user) { |
|
|
|
|
74
|
|
|
$query->andWhere(['author' => $user]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$dataProvider = new ActiveDataProvider([ |
78
|
|
|
'query' => $query, |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
$dataProvider->setSort([ |
82
|
|
|
'attributes' => ArrayHelper::merge($dataProvider->sort->attributes, [ |
83
|
|
|
'username' => [ |
84
|
|
|
'asc' => ['username' => SORT_ASC], |
85
|
|
|
'desc' => ['username' => SORT_DESC], |
86
|
|
|
'label' => 'Author', |
87
|
|
|
'value' => 'username', |
88
|
|
|
], |
89
|
|
|
]), |
90
|
|
|
'defaultOrder' => ['id' => SORT_DESC], |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$this->load($params); |
94
|
|
|
|
95
|
|
|
if (!$this->validate()) { |
96
|
|
|
return $dataProvider; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$query->andFilterWhere([ |
100
|
|
|
'post.id' => $this->id, |
101
|
|
|
'author' => $this->author, |
102
|
|
|
'type' => $this->type, |
103
|
|
|
'comment_count' => $this->comment_count, |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
$query->andFilterWhere(['like', 'title', $this->title]) |
107
|
|
|
->andFilterWhere(['like', 'excerpt', $this->excerpt]) |
108
|
|
|
->andFilterWhere(['like', 'content', $this->content]) |
109
|
|
|
->andFilterWhere(['like', 'post.status', $this->status]) |
110
|
|
|
->andFilterWhere(['like', 'password', $this->password]) |
111
|
|
|
->andFilterWhere(['like', 'slug', $this->slug]) |
112
|
|
|
->andFilterWhere(['like', 'date', $this->date]) |
113
|
|
|
->andFilterWhere(['like', 'modified', $this->modified]) |
114
|
|
|
->andFilterWhere(['like', 'comment_status', $this->comment_status]) |
115
|
|
|
->andFilterWhere(['like', 'username', $this->username]); |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
return $dataProvider; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: