Completed
Push — master ( da2fc1...7aff44 )
by Razon
02:48
created

ArticleCommentController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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