User::search()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 25
c 0
b 0
f 0
rs 9.7998
cc 3
nc 2
nop 1
1
<?php
2
3
namespace toir427\admin\models\searchs;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\ActiveDataProvider;
8
9
/**
10
 * User represents the model behind the search form about `toir427\admin\models\User`.
11
 */
12
class User extends Model
13
{
14
    public $id;
15
    public $username;
16
    public $email;
17
    public $status;
18
    
19
    /**
20
     * @inheritdoc
21
     */
22
    public function rules()
23
    {
24
        return [
25
            [['id', 'status',], 'integer'],
26
            [['username', 'email'], 'safe'],
27
        ];
28
    }
29
30
    /**
31
     * Creates data provider instance with search query applied
32
     *
33
     * @param array $params
34
     *
35
     * @return ActiveDataProvider
36
     */
37
    public function search($params)
38
    {
39
        /* @var $query \yii\db\ActiveQuery */
40
        $class = Yii::$app->getUser()->identityClass ? : 'toir427\admin\models\User';
41
        $query = $class::find();
42
43
        $dataProvider = new ActiveDataProvider([
44
            'query' => $query,
45
        ]);
46
47
        $this->load($params);
48
        if (!$this->validate()) {
49
            $query->where('1=0');
50
            return $dataProvider;
51
        }
52
53
        $query->andFilterWhere([
54
            'id' => $this->id,
55
            'status' => $this->status,
56
        ]);
57
58
        $query->andFilterWhere(['like', 'username', $this->username])
59
            ->andFilterWhere(['like', 'email', $this->email]);
60
61
        return $dataProvider;
62
    }
63
}
64