Passed
Push — master ( dd4450...9a9614 )
by Andrey
05:55
created

ArticleSearch::scenarios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\{ActiveDataProvider, Pagination};
8
9
/**
10
 * ArticleSearch represents the model behind the search form of `app\models\Article`.
11
 *
12
 * @package app\models
13
 */
14
class ArticleSearch extends Article
15
{
16
    /**
17
     * @var string
18
     */
19
    public $title;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function rules()
25
    {
26
        return [
27
            [
28
                [
29
                    'pageId',
30
                    'active',
31
                ],
32
                'integer',
33
            ],
34
            [
35
                'title',
36
                'string',
37
            ],
38
        ];
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function scenarios()
45
    {
46
        // bypass scenarios() implementation in the parent class
47
        return Model::scenarios();
48
    }
49
50
    /**
51
     * Creates data provider instance with search query applied
52
     *
53
     * @param array $params
54
     *
55
     * @return ActiveDataProvider
56
     */
57
    public function search($params)
58
    {
59
        $query = Article::find()->joinWith(['articlesLanguages']);
60
61
        // add conditions that should always apply here
62
63
        $dataProvider = new ActiveDataProvider([
64
            'query' => $query,
65
        ]);
66
67
        $this->load($params);
68
69
        if (!$this->validate()) {
70
            // uncomment the following line if you do not want to return any records when validation fails
71
            // $query->where('0=1');
72
            return $dataProvider;
73
        }
74
75
        // grid filtering conditions
76
        $query->andFilterWhere([
77
            'pageId' => $this->pageId,
78
            'active' => $this->active,
79
        ]);
80
81
        $query->andFilterWhere(['like', 'articles_language.title', $this->title]);
82
83
        $pagination = new Pagination([
84
            'defaultPageSize' => Yii::$app->params['defaultPageSize'],
85
            'totalCount' => $query->count(),
86
        ]);
87
88
        $dataProvider->setPagination($pagination);
89
90
        return $dataProvider;
91
    }
92
}
93