1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\models\query; |
4
|
|
|
|
5
|
|
|
use yii\db\ActiveQuery; |
6
|
|
|
use app\models\User; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* ActiveQuery for User |
10
|
|
|
*/ |
11
|
|
|
class UserQuery extends ActiveQuery |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Select only active |
15
|
|
|
* |
16
|
|
|
* @return yii\db\ActiveQuery |
17
|
|
|
*/ |
18
|
|
|
public function active(): ActiveQuery |
19
|
|
|
{ |
20
|
|
|
$this->andWhere(['status' => User::STATUS_ACTIVE]); |
21
|
|
|
return $this; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Select only blocked |
26
|
|
|
* |
27
|
|
|
* @return yii\db\ActiveQuery |
28
|
|
|
*/ |
29
|
|
|
public function blocked(): ActiveQuery |
30
|
|
|
{ |
31
|
|
|
$this->andWhere(['status' => User::STATUS_BLOCKED]); |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Select only deleted |
37
|
|
|
* |
38
|
|
|
* @return yii\db\ActiveQuery |
39
|
|
|
*/ |
40
|
|
|
public function deleted(): ActiveQuery |
41
|
|
|
{ |
42
|
|
|
$this->andWhere(['status' => User::STATUS_DELETED]); |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* `Like` search for value in the field |
48
|
|
|
* |
49
|
|
|
* @param string $field |
50
|
|
|
* @param string $value |
51
|
|
|
* @return yii\db\ActiveQuery |
52
|
|
|
*/ |
53
|
|
|
public function like(string $field, string $value): ActiveQuery |
54
|
|
|
{ |
55
|
|
|
$this->andWhere(['like', $field, $value]); |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Select by password reset token |
61
|
|
|
* |
62
|
|
|
* @param string $token password reset token |
63
|
|
|
* @return yii\db\ActiveQuery |
64
|
|
|
*/ |
65
|
|
|
public function passwordResetToken(string $token): ActiveQuery |
66
|
|
|
{ |
67
|
|
|
$this->andWhere([ |
68
|
|
|
'password_reset_token' => $token, |
69
|
|
|
'status' => User::STATUS_ACTIVE, |
70
|
|
|
]); |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Select by confirm email token |
76
|
|
|
* |
77
|
|
|
* @param string $token confirm email token |
78
|
|
|
* @return yii\db\ActiveQuery |
79
|
|
|
*/ |
80
|
|
|
public function emailConfirmToken(string $token): ActiveQuery |
81
|
|
|
{ |
82
|
|
|
$this->andWhere([ |
83
|
|
|
'email_confirm_token' => $token, |
84
|
|
|
'status' => User::STATUS_ACTIVE, |
85
|
|
|
]); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Select by username |
91
|
|
|
* |
92
|
|
|
* @param string $username |
93
|
|
|
* @return yii\db\ActiveQuery |
94
|
|
|
*/ |
95
|
|
|
public function username(string $username): ActiveQuery |
96
|
|
|
{ |
97
|
|
|
$this->andWhere(['username' => $username]); |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Select by email |
103
|
|
|
* |
104
|
|
|
* @param string $email |
105
|
|
|
* @return yii\db\ActiveQuery |
106
|
|
|
*/ |
107
|
|
|
public function email(string $email): ActiveQuery |
108
|
|
|
{ |
109
|
|
|
$this->andWhere(['email' => $email]); |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|