About::getTechnologies()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 1
nop 0
dl 0
loc 6
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
7
/**
8
 * This is the model class for table "about".
9
 *
10
 * @property int $id
11
 * @property int $default
12
 * @property string $title
13
 * @property string $description
14
 * @property string $content
15
 * @property string $metaKeys
16
 * @property string $metaDescription
17
 * @property string $created_at
18
 * @property string $updated_at
19
 *
20
 * @property AboutTechnology[] $aboutTechnologies
21
 * @property Technology[] $technologies
22
 * @property AboutQuality[] $aboutQualities
23
 * @property Quality[] $qualities
24
 *
25
 * @package app\models
26
 */
27
class About extends ActiveRecord
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public static function tableName()
33
    {
34
        return 'about';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function rules()
41
    {
42
        return [
43
            [
44
                [
45
                    'title',
46
                    'content',
47
                ],
48
                'required'
49
            ],
50
            [
51
                [
52
                    'description',
53
                    'content',
54
                ],
55
                'string'
56
            ],
57
            [
58
                [
59
                    'title',
60
                    'metaKeys',
61
                ],
62
                'string',
63
                'max' => 128
64
            ],
65
            [
66
                [
67
                    'metaDescription',
68
                ],
69
                'string',
70
                'max' => 255
71
            ],
72
            [
73
                [
74
                    'default'
75
                ],
76
                'integer'
77
            ],
78
            [
79
                'title',
80
                'unique',
81
                'skipOnError'     => true,
82
                'targetClass'     => static::class,
83
                'filter' => $this->getScenario() == self::SCENARIO_UPDATE ? 'id != '.$this->id : ''
84
            ],
85
            [
86
                [
87
                    'created_at',
88
                    'updated_at'
89
                ],
90
                'safe'
91
            ],
92
        ];
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function attributes(): array
99
    {
100
        return [
101
            'id',
102
            'default',
103
            'title',
104
            'description',
105
            'content',
106
            'metaKeys',
107
            'metaDescription',
108
            'created_at',
109
            'updated_at'
110
        ];
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function attributeLabels()
117
    {
118
        return [
119
            'id' => 'ID',
120
            'default' => Yii::t('app', 'Default'),
121
            'title' => Yii::t('app', 'Title'),
122
            'description' => Yii::t('app', 'Description'),
123
            'content' => Yii::t('app', 'Content'),
124
            'metaKeys' => Yii::t('app', 'Meta keys'),
125
            'metaDescription' => Yii::t('app', 'Meta description'),
126
            'created_at' => Yii::t('app', 'Created date'),
127
            'updated_at' => Yii::t('app', 'Updated date'),
128
        ];
129
    }
130
131
    /**
132
     * Reset the default about record.
133
     *
134
     * @param boolean $insert
135
     *
136
     * @return mixed
137
     */
138
    public function beforeSave($insert)
139
    {
140
        if ($this->default == 1) {
141
142
            $default = static::findOne([
143
                'default' => 1,
144
            ]);
145
146
            if (null !== $default) {
147
                $default->default = 0;
148
                $default->save();
149
            }
150
        }
151
152
        return parent::beforeSave($insert);
153
    }
154
155
    /**
156
     * Returns the default about record.
157
     *
158
     * @return null|\yii\db\ActiveRecord
159
     */
160
    public static function getDefaultAbout()
161
    {
162
        return static::find()
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::find()->w...'default' => 1))->one() also could return the type array which is incompatible with the documented return type null|yii\db\ActiveRecord.
Loading history...
163
            ->where([
164
                'default' => 1
165
            ])->one();
166
    }
167
168
    /**
169
     * @return \yii\db\ActiveQuery
170
     */
171
    public function getAboutTechnologies()
172
    {
173
        return $this->hasMany(AboutTechnology::class, [
174
            'about_id' => 'id'
175
        ]);
176
    }
177
178
    /**
179
     * @return \yii\db\ActiveQuery
180
     */
181
    public function getTechnologies()
182
    {
183
        return $this->hasMany(Technology::class, [
184
            'id' => 'technologies_id'
185
        ])->viaTable('about_technologies', [
186
            'about_id' => 'id'
187
        ]);
188
    }
189
190
    /**
191
     * @return \yii\db\ActiveQuery
192
     */
193
    public function getAboutQualities()
194
    {
195
        return $this->hasMany(AboutQuality::class, [
196
            'about_id' => 'id'
197
        ]);
198
    }
199
200
    /**
201
     * @return \yii\db\ActiveQuery
202
     */
203
    public function getQualities()
204
    {
205
        return $this->hasMany(Quality::class, [
206
            'id' => 'qualities_id'
207
        ])->viaTable('about_qualities', [
208
            'about_id' => 'id'
209
        ]);
210
    }
211
}
212