forked from robregonm/yii2-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserSearch.php
92 lines (83 loc) · 2.24 KB
/
UserSearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace auth\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use auth\models\User;
/**
* UserSearch represents the model behind the search form about User.
*/
class UserSearch extends Model
{
public $id;
public $username;
public $email;
public $password_hash;
public $password_reset_token;
public $auth_key;
public $status;
public $last_visit_time;
public $create_time;
public $update_time;
public $delete_time;
public function rules()
{
return [
[['id', 'status'], 'integer'],
[['username', 'email', 'password_hash', 'password_reset_token', 'auth_key', 'last_visit_time', 'create_time', 'update_time', 'delete_time'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'email' => 'Email',
'password_hash' => 'Password Hash',
'password_reset_token' => 'Password Reset Token',
'auth_key' => 'Auth Key',
'status' => 'Status',
'last_visit_time' => 'Last Visit Time',
'create_time' => 'Create Time',
'update_time' => 'Update Time',
'delete_time' => 'Delete Time',
];
}
public function search($params)
{
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'id');
$this->addCondition($query, 'username', true);
$this->addCondition($query, 'email', true);
$this->addCondition($query, 'password_hash', true);
$this->addCondition($query, 'password_reset_token', true);
$this->addCondition($query, 'auth_key', true);
$this->addCondition($query, 'status');
$this->addCondition($query, 'last_visit_time', true);
$this->addCondition($query, 'create_time', true);
$this->addCondition($query, 'update_time', true);
$this->addCondition($query, 'delete_time', true);
return $dataProvider;
}
protected function addCondition($query, $attribute, $partialMatch = false)
{
$value = $this->$attribute;
if (trim($value) === '') {
return;
}
if ($partialMatch) {
$value = '%' . strtr($value, ['%'=>'\%', '_'=>'\_', '\\'=>'\\\\']) . '%';
$query->andWhere(['like', $attribute, $value]);
} else {
$query->andWhere([$attribute => $value]);
}
}
}