Completed
Push — master ( 1bc3c7...02ce33 )
by Tõnis
02:52
created

BaseSurvey::getLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace dameter\abstracts\models;
4
5
use dameter\abstracts\DActiveRecord;
6
use yii\helpers\ArrayHelper;
7
8
/**
9
 * Class BaseSurvey
10
 *
11
 * @property int $survey_id
12
 * @property string $name Survey name. Primarily meant for back-end usage.
13
 * @property int $language_id base language id
14
 *
15
 * @property BaseQuestion[] $questions
16
 * @property Language[] $languages
17
 * @property Language $language
18
 *
19
 * @package dameter\abstracts\models
20
 * @author Tõnis Ormisson <[email protected]>
21
 */
22
abstract class BaseSurvey extends DActiveRecord
23
{
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function rules()
29
    {
30
        return [
31
            [['name'], 'string', 'max' => 254],
32
            [['language_id'], 'integer'],
33
            [['language_id'], 'exist', 'skipOnError' => true, 'targetClass' => Language::class, 'targetAttribute' => ['language_id' => 'language_id']],
34
        ];
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getLanguage()
41
    {
42
        return $this->hasMany(Language::class);
43
    }
44
45
    /**
46
     * @return \yii\db\ActiveQuery
47
     * @throws \yii\base\NotSupportedException
48
     */
49
    public function getLanguages()
50
    {
51
        $relations = $this->surveyLanguages();
52
        $ids = ArrayHelper::getColumn($relations, SurveyLanguage::primaryKeySingle());
53
        return Language::find()->andWhere(['in', Language::primaryKeySingle(), $ids]);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getQuestions()
60
    {
61
        return $this->hasMany(BaseQuestion::class);
62
    }
63
64
65
    /**
66
     * @return SurveyLanguage[]
67
     * @throws \yii\base\NotSupportedException
68
     */
69
    private function surveyLanguages()
70
    {
71
        return SurveyLanguage::getChildren($this);
72
    }
73
74
}