Passed
Push — master ( a90c1b...36c150 )
by Razon
02:16
created

ArticleCategoryController::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace App\Http\Api\Backend\Controller;
3
4
use App\Http\Api\Backend\Model\ArticleCategory;
5
use yii\base\DynamicModel;
6
7
class ArticleCategoryController extends ActiveController
8
{
9
    public $modelClass = ArticleCategory::class;
10
    
11
    public function getQuery($action)
12
    {
13
        return parent::getQuery($action)
14
            ->alias('c')
15
            ->orderBy([
16
                'c.name' => SORT_ASC
17
            ]);
18
    }
19
20
    public function searchModel()
21
    {
22
        return (new DynamicModel(['id' => '', 'name' => '']))
23
            ->addRule(['name'], 'string')
24
            ->addRule(['id'], 'number');
25
    }
26
27
    protected function applyFilter($query, $model, $filter)
28
    {
29
        foreach (['name'] as $name) {
30
            if (!empty($model->$name)) {
31
                $query->andWhere(['LIKE', 'c.' . $name, $model->$name]);
32
            }
33
        }
34
        foreach (['id'] as $name) {
35
            if (is_numeric($model->$name)) {
36
                $query->andWhere(['c.' . $name => $model->$name]);
37
            }
38
        }
39
    }
40
}
41