73
73
*
74
74
* @author José Lorente <jose.lorente.martin@gmail.com>
75
75
*/
76
- trait ActiveRecordInheritanceTrait {
76
+ trait ActiveRecordInheritanceTrait
77
+ {
77
78
78
79
/**
79
80
*
@@ -84,7 +85,8 @@ trait ActiveRecordInheritanceTrait {
84
85
/**
85
86
* @return \yii\db\ActiveRecordInterface
86
87
*/
87
- public function getParent ($ recalculate = false ) {
88
+ public function getParent ($ recalculate = false )
89
+ {
88
90
if ($ this ->_parent === null || $ recalculate === true ) {
89
91
if (($ this instanceof ActiveRecordInheritanceInterface) === false ) {
90
92
throw new BaseException ('Classes that use the \jlorente\db\ActiveRecordInheritanceTrait must implement \jlorente\db\ActiveRecordInheritanceInterface ' );
@@ -105,23 +107,26 @@ public function getParent($recalculate = false) {
105
107
*
106
108
* @return \yii\db\ActiveQueryInterface
107
109
*/
108
- private function _parent () {
110
+ private function _parent ()
111
+ {
109
112
$ class = static ::extendsFrom ();
110
113
return $ this ->hasOne ($ class ::className (), [$ this ->parentPrimaryKey () => $ this ->parentAttribute ()]);
111
114
}
112
115
113
116
/**
114
117
* @inheritdoc
115
118
*/
116
- public static function populateRecord ($ record , $ row ) {
119
+ public static function populateRecord ($ record , $ row )
120
+ {
117
121
parent ::populateRecord ($ record , $ row );
118
122
$ record ->_parent = $ record ->_parent ()->one ();
119
123
}
120
124
121
125
/**
122
126
* @inheritdoc
123
127
*/
124
- public function __get ($ name ) {
128
+ public function __get ($ name )
129
+ {
125
130
try {
126
131
return parent ::__get ($ name );
127
132
} catch (UnknownPropertyException $ e ) {
@@ -132,7 +137,8 @@ public function __get($name) {
132
137
/**
133
138
* @inheritdoc
134
139
*/
135
- public function __set ($ name , $ value ) {
140
+ public function __set ($ name , $ value )
141
+ {
136
142
try {
137
143
parent ::__set ($ name , $ value );
138
144
} catch (UnknownPropertyException $ e ) {
@@ -147,21 +153,23 @@ public function __set($name, $value) {
147
153
/**
148
154
* @inheritdoc
149
155
*/
150
- public function setAttributes ($ values , $ safeOnly = true ) {
156
+ public function setAttributes ($ values , $ safeOnly = true )
157
+ {
151
158
$ this ->getParent ()->setAttributes ($ values , $ safeOnly );
152
159
parent ::setAttributes ($ values , $ safeOnly );
153
160
}
154
161
155
162
/**
156
163
* @inheritdoc
157
164
*/
158
- public function __isset ($ name ) {
165
+ public function __isset ($ name )
166
+ {
159
167
try {
160
168
if (parent ::__get ($ name ) !== null ) {
161
169
return true ;
162
170
}
163
171
} catch (UnknownPropertyException $ e ) {
164
- return $ this ->getParent ()->{$ name };
172
+ return isset ( $ this ->getParent ()->{$ name }) ;
165
173
}
166
174
167
175
if (parent ::__isset ($ name ) === false ) {
@@ -174,7 +182,8 @@ public function __isset($name) {
174
182
/**
175
183
* @inheritdoc
176
184
*/
177
- public function __unset ($ name ) {
185
+ public function __unset ($ name )
186
+ {
178
187
try {
179
188
if (parent ::__get ($ name ) !== null ) {
180
189
parent ::__unset ($ name );
@@ -187,7 +196,8 @@ public function __unset($name) {
187
196
/**
188
197
* @inheritdoc
189
198
*/
190
- public function __call ($ name , $ params ) {
199
+ public function __call ($ name , $ params )
200
+ {
191
201
try {
192
202
return parent ::__call ($ name , $ params );
193
203
} catch (UnknownMethodException $ e ) {
@@ -210,7 +220,8 @@ public function __call($name, $params) {
210
220
*
211
221
* ```
212
222
*/
213
- public function attributeLabels () {
223
+ public function attributeLabels ()
224
+ {
214
225
return array_merge ($ this ->getParent ()->attributeLabels (), parent ::attributeLabels ());
215
226
}
216
227
@@ -223,7 +234,8 @@ public function attributeLabels() {
223
234
* @param array $except list of attributes whose value should NOT be returned.
224
235
* @return array attribute values (name => value).
225
236
*/
226
- public function getAttributes ($ names = null , $ except = array ()) {
237
+ public function getAttributes ($ names = null , $ except = array ())
238
+ {
227
239
if ($ names === null ) {
228
240
$ names = array_merge ($ this ->getParent ()->attributes (), $ this ->attributes ());
229
241
}
@@ -238,7 +250,8 @@ public function getAttributes($names = null, $except = array()) {
238
250
* @return boolean
239
251
* @throws \Exception
240
252
*/
241
- public function save ($ runValidation = true , $ attributeNames = null ) {
253
+ public function save ($ runValidation = true , $ attributeNames = null )
254
+ {
242
255
if ($ runValidation === true && $ this ->validate ($ attributeNames ) === false ) {
243
256
Yii::info ('Model not inserted due to validation error. ' , __METHOD__ );
244
257
return false ;
@@ -281,7 +294,8 @@ public function save($runValidation = true, $attributeNames = null) {
281
294
* being deleted is outdated.
282
295
* @throws \Exception in case delete failed.
283
296
*/
284
- public function delete () {
297
+ public function delete ()
298
+ {
285
299
$ trans = static ::getDb ()->beginTransaction ();
286
300
try {
287
301
if (parent ::delete () === false ) {
@@ -307,10 +321,11 @@ public function delete() {
307
321
* @return boolean
308
322
* @throws Exception
309
323
*/
310
- public function validate ($ attributeNames = null , $ clearErrors = true ) {
324
+ public function validate ($ attributeNames = null , $ clearErrors = true )
325
+ {
311
326
$ r = parent ::validate ($ attributeNames === null ?
312
- array_diff ($ this ->activeAttributes (), $ this ->getParent ()->activeAttributes (), [$ this ->parentAttribute ()]) :
313
- $ attributeNames , $ clearErrors );
327
+ array_diff ($ this ->activeAttributes (), $ this ->getParent ()->activeAttributes (), [$ this ->parentAttribute ()]) :
328
+ $ attributeNames , $ clearErrors );
314
329
return $ this ->getParent ()->validate ($ attributeNames , $ clearErrors ) && $ r ;
315
330
}
316
331
@@ -322,7 +337,8 @@ public function validate($attributeNames = null, $clearErrors = true) {
322
337
* @param string|null $attribute attribute name. Use null to check all attributes.
323
338
* @return boolean whether there is any error.
324
339
*/
325
- public function hasErrors ($ attribute = null ) {
340
+ public function hasErrors ($ attribute = null )
341
+ {
326
342
return $ this ->getParent ()->hasErrors ($ attribute ) || parent ::hasErrors ($ attribute );
327
343
}
328
344
@@ -352,7 +368,8 @@ public function hasErrors($attribute = null) {
352
368
* @see getFirstErrors()
353
369
* @see getFirstError()
354
370
*/
355
- public function getErrors ($ attribute = null ) {
371
+ public function getErrors ($ attribute = null )
372
+ {
356
373
return array_merge ($ this ->getParent ()->getErrors ($ attribute ), parent ::getErrors ($ attribute ));
357
374
}
358
375
@@ -366,7 +383,8 @@ public function getErrors($attribute = null) {
366
383
* @see getErrors()
367
384
* @see getFirstError()
368
385
*/
369
- public function getFirstErrors () {
386
+ public function getFirstErrors ()
387
+ {
370
388
$ errs = $ this ->getErrors ();
371
389
if (empty ($ errs )) {
372
390
return [];
@@ -392,15 +410,17 @@ public function getFirstErrors() {
392
410
* @see getErrors()
393
411
* @see getFirstErrors()
394
412
*/
395
- public function getFirstError ($ attribute ) {
413
+ public function getFirstError ($ attribute )
414
+ {
396
415
$ errors = $ this ->getErrors ($ attribute );
397
416
return count ($ errors ) ? $ errors [0 ] : null ;
398
417
}
399
418
400
419
/**
401
420
* @inheritdoc
402
421
*/
403
- public function refresh () {
422
+ public function refresh ()
423
+ {
404
424
$ r = parent ::refresh ();
405
425
return $ this ->getParent ()->refresh () && $ r ;
406
426
}
@@ -411,7 +431,8 @@ public function refresh() {
411
431
*
412
432
* @return string
413
433
*/
414
- public function parentAttribute () {
434
+ public function parentAttribute ()
435
+ {
415
436
return static ::primaryKey ()[0 ];
416
437
}
417
438
@@ -420,22 +441,25 @@ public function parentAttribute() {
420
441
*
421
442
* @return string
422
443
*/
423
- public function parentPrimaryKey () {
444
+ public function parentPrimaryKey ()
445
+ {
424
446
$ pClass = static ::extendsFrom ();
425
447
return $ pClass ::primaryKey ()[0 ];
426
448
}
427
449
428
450
/**
429
451
* @inheritdoc
430
452
*/
431
- public function fields () {
453
+ public function fields ()
454
+ {
432
455
return ArrayHelper::merge ($ this ->parent ->fields (), parent ::fields ());
433
456
}
434
457
435
458
/**
436
459
* @inheritdoc
437
460
*/
438
- public function loadDefaultValues ($ skipIfSet = true ) {
461
+ public function loadDefaultValues ($ skipIfSet = true )
462
+ {
439
463
$ this ->parent ->loadDefaultValues ($ skipIfSet );
440
464
parent ::loadDefaultValues ($ skipIfSet );
441
465
}
0 commit comments