1 | <?php |
||
2 | |||
3 | namespace app\models; |
||
4 | |||
5 | use Yii; |
||
6 | use Itstructure\AdminModule\models\{MultilanguageTrait, Language, ActiveRecord}; |
||
7 | use Itstructure\AdminModule\interfaces\ModelInterface; |
||
8 | |||
9 | /** |
||
10 | * This is the model class for table "qualities". |
||
11 | * |
||
12 | * @property int $id |
||
13 | * @property string $icon |
||
14 | * @property string $created_at |
||
15 | * @property string $updated_at |
||
16 | * |
||
17 | * @property AboutQuality[] $aboutQualities |
||
18 | * @property About[] $about |
||
19 | * @property QualityLanguage[] $qualitiesLanguages |
||
20 | * @property Language[] $languages |
||
21 | */ |
||
22 | class QualityBase extends ActiveRecord |
||
23 | { |
||
24 | use MultilanguageTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public static function tableName() |
||
30 | { |
||
31 | return 'qualities'; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function rules() |
||
38 | { |
||
39 | return [ |
||
40 | [ |
||
41 | [ |
||
42 | 'icon', |
||
43 | 'about' |
||
44 | ], |
||
45 | 'required' |
||
46 | ], |
||
47 | [ |
||
48 | [ |
||
49 | 'created_at', |
||
50 | 'updated_at' |
||
51 | ], |
||
52 | 'safe' |
||
53 | ], |
||
54 | [ |
||
55 | [ |
||
56 | 'icon' |
||
57 | ], |
||
58 | 'string', |
||
59 | 'max' => 128 |
||
60 | ] |
||
61 | ]; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * List if attributes. |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | public function attributes() |
||
70 | { |
||
71 | return [ |
||
72 | 'id', |
||
73 | 'icon', |
||
74 | 'created_at', |
||
75 | 'updated_at', |
||
76 | ]; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | public function mainModelAttributes() |
||
83 | { |
||
84 | return [ |
||
85 | 'id', |
||
86 | 'icon', |
||
87 | 'about', |
||
88 | 'created_at', |
||
89 | 'updated_at', |
||
90 | ]; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | public function scenarios() |
||
97 | { |
||
98 | $scenarios = parent::scenarios(); |
||
99 | |||
100 | $scenarios[ModelInterface::SCENARIO_CREATE][] = 'about'; |
||
101 | $scenarios[ModelInterface::SCENARIO_UPDATE][] = 'about'; |
||
102 | |||
103 | return $scenarios; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function attributeLabels() |
||
110 | { |
||
111 | return [ |
||
112 | 'id' => 'ID', |
||
113 | 'icon' => Yii::t('app', 'Icon'), |
||
114 | 'created_at' => Yii::t('app', 'Created date'), |
||
115 | 'updated_at' => Yii::t('app', 'Updated date'), |
||
116 | ]; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return \yii\db\ActiveQuery |
||
121 | */ |
||
122 | public function getAboutQualities() |
||
123 | { |
||
124 | return $this->hasMany(AboutQuality::class, [ |
||
125 | 'qualities_id' => 'id' |
||
126 | ]); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return \yii\db\ActiveQuery |
||
131 | */ |
||
132 | public function getAbout() |
||
133 | { |
||
134 | return $this->hasMany(About::class, [ |
||
135 | 'id' => 'about_id' |
||
136 | ])->viaTable('about_qualities', [ |
||
137 | 'qualities_id' => 'id' |
||
138 | ]); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param $about |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | public function setAbout($about): void |
||
147 | { |
||
148 | $this->about = $about; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return \yii\db\ActiveQuery |
||
153 | */ |
||
154 | public function getQualitiesLanguages() |
||
155 | { |
||
156 | return $this->hasMany(QualityLanguage::class, [ |
||
157 | 'qualities_id' => 'id' |
||
158 | ]); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return \yii\db\ActiveQuery |
||
163 | */ |
||
164 | public function getLanguages() |
||
165 | { |
||
166 | return $this->hasMany(Language::class, [ |
||
167 | 'id' => 'language_id' |
||
168 | ])->viaTable('qualities_language', [ |
||
169 | 'qualities_id' => 'id' |
||
170 | ]); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Link with about entity. |
||
175 | * |
||
176 | * @param array $aboutList |
||
177 | */ |
||
178 | protected function linkWithAbout(array $aboutList): void |
||
179 | { |
||
180 | AboutQuality::deleteAll([ |
||
181 | 'qualities_id' => $this->id |
||
182 | ]); |
||
183 | |||
184 | foreach ($aboutList as $aboutId) { |
||
185 | $aboutQuality = new AboutQuality(); |
||
186 | $aboutQuality->qualities_id = $this->id; |
||
187 | $aboutQuality->about_id = $aboutId; |
||
188 | $aboutQuality->save(); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 |