1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\models; |
4
|
|
|
|
5
|
|
|
use app\components\AccountManager; |
6
|
|
|
use app\models\AccountStats; |
|
|
|
|
7
|
|
|
use app\models\AccountTag; |
8
|
|
|
use app\models\Tag; |
|
|
|
|
9
|
|
|
use yii\base\Model; |
10
|
|
|
use yii\data\ActiveDataProvider; |
11
|
|
|
use yii\db\Expression; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* AccountSearch represents the model behind the search form of `app\models\Account`. |
15
|
|
|
*/ |
16
|
|
|
class AccountSearch extends Account |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
*/ |
22
|
|
|
public function rules() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
[['id', 'monitoring', 'proxy_id'], 'integer'], |
26
|
|
|
[['username', 'profile_pic_url', 'full_name', 'biography', 'external_url', 'instagram_id', 's_tags'], 'safe'], |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function scenarios() |
34
|
|
|
{ |
35
|
|
|
// bypass scenarios() implementation in the parent class |
36
|
|
|
return Model::scenarios(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Creates data provider instance with search query applied |
41
|
|
|
* |
42
|
|
|
* @param array $params |
43
|
|
|
* |
44
|
|
|
* @return ActiveDataProvider |
45
|
|
|
* @throws \yii\base\InvalidConfigException |
46
|
|
|
*/ |
47
|
|
|
public function search($params) |
48
|
|
|
{ |
49
|
|
|
$userId = (int)\Yii::$app->user->id; |
50
|
|
|
|
51
|
|
|
$query = Account::find() |
52
|
|
|
->select([ |
53
|
|
|
'account.*', |
54
|
|
|
new Expression('GROUP_CONCAT(tag.name SEPARATOR \', \') as s_tags'), |
55
|
|
|
]) |
56
|
|
|
->leftJoin(AccountTag::tableName(), 'account.id=account_tag.account_id AND account_tag.user_id=' . $userId) |
57
|
|
|
->leftJoin(Tag::tableName(), 'account_tag.tag_id=tag.id') |
58
|
|
|
->groupBy('account.id'); |
59
|
|
|
|
60
|
|
|
// add conditions that should always apply here |
61
|
|
|
|
62
|
|
|
$dataProvider = new ActiveDataProvider([ |
63
|
|
|
'query' => $query, |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$dataProvider->sort->defaultOrder = [ |
67
|
|
|
'is_valid' => SORT_ASC, |
68
|
|
|
'invalidation_type_id' => SORT_DESC, |
69
|
|
|
'invalidation_count' => SORT_DESC, |
70
|
|
|
'id' => SORT_DESC, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$dataProvider->sort->attributes['username'] = [ |
74
|
|
|
'asc' => ['name' => SORT_ASC, 'username' => SORT_ASC], |
75
|
|
|
'desc' => ['name' => SORT_DESC, 'username' => SORT_DESC], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$this->load($params); |
79
|
|
|
|
80
|
|
|
if (!$this->validate()) { |
81
|
|
|
// uncomment the following line if you do not want to return any records when validation fails |
82
|
|
|
// $query->where('0=1'); |
83
|
|
|
return $dataProvider; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// grid filtering conditions |
87
|
|
|
$query->andFilterWhere([ |
88
|
|
|
'monitoring' => $this->monitoring, |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
$query->andFilterWhere(['or', |
92
|
|
|
['like', 'username', $this->username], |
93
|
|
|
['like', 'account.name', $this->username], |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
if ($this->s_tags) { |
97
|
|
|
$manager = \Yii::createObject(AccountManager::class); |
98
|
|
|
$accountIds = $manager->findByTags($this->s_tags, $userId); |
99
|
|
|
|
100
|
|
|
$query->andWhere(['account.id' => $accountIds]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $dataProvider; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: