1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created for IG Monitoring. |
4
|
|
|
* User: jakim <[email protected]> |
5
|
|
|
* Date: 14.05.2018 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace app\modules\api\v1\models; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use app\components\AccountManager; |
12
|
|
|
use app\models\AccountStats; |
|
|
|
|
13
|
|
|
use yii\base\Model; |
14
|
|
|
use yii\data\ActiveDataFilter; |
15
|
|
|
use yii\data\ActiveDataProvider; |
16
|
|
|
use yii\helpers\ArrayHelper; |
17
|
|
|
|
18
|
|
|
class AccountSearch extends Model |
19
|
|
|
{ |
20
|
|
|
public function search(array $params) |
21
|
|
|
{ |
22
|
|
|
$query = Account::find() |
23
|
|
|
->leftJoin( |
24
|
|
|
AccountStats::tableName(), |
25
|
|
|
'account.id=account_stats.account_id and account_stats.id = (SELECT MAX(id) FROM account_stats WHERE account_stats.account_id=account.id)' |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$tags = ArrayHelper::getValue($params, 'filter.tags'); |
29
|
|
|
unset($params['filter']['tags']); |
30
|
|
|
|
31
|
|
|
if ($tags) { |
32
|
|
|
$manager = \Yii::createObject(AccountManager::class); |
33
|
|
|
$accountIds = $manager->findByTags($tags, \Yii::$app->user->id); |
34
|
|
|
|
35
|
|
|
$query->andWhere(['account.id' => $accountIds]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$dataFilter = \Yii::createObject([ |
39
|
|
|
'class' => ActiveDataFilter::class, |
40
|
|
|
'searchModel' => DataFilterForm::class, |
41
|
|
|
]); |
42
|
|
|
if ($dataFilter->load($params)) { |
43
|
|
|
$filter = $dataFilter->build(); |
44
|
|
|
if ($filter === false) { |
45
|
|
|
return $dataFilter; |
46
|
|
|
} |
47
|
|
|
$query->andWhere($filter); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$dataProvider = new ActiveDataProvider([ |
51
|
|
|
'query' => $query, |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
$dataProvider->sort->attributes['followed_by'] = [ |
55
|
|
|
'asc' => ['account_stats.followed_by' => SORT_ASC], |
56
|
|
|
'desc' => ['account_stats.followed_by' => SORT_DESC], |
57
|
|
|
]; |
58
|
|
|
$dataProvider->sort->attributes['follows'] = [ |
59
|
|
|
'asc' => ['account_stats.follows' => SORT_ASC], |
60
|
|
|
'desc' => ['account_stats.follows' => SORT_DESC], |
61
|
|
|
]; |
62
|
|
|
$dataProvider->sort->attributes['media'] = [ |
63
|
|
|
'asc' => ['account_stats.media' => SORT_ASC], |
64
|
|
|
'desc' => ['account_stats.media' => SORT_DESC], |
65
|
|
|
]; |
66
|
|
|
$dataProvider->sort->attributes['er'] = [ |
67
|
|
|
'asc' => ['account_stats.er' => SORT_ASC], |
68
|
|
|
'desc' => ['account_stats.er' => SORT_DESC], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
return $dataProvider; |
72
|
|
|
} |
73
|
|
|
} |
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: