Skip to content

Commit e20302a

Browse files
committed
* Fixed bug in __isset magic method.
1 parent 18c4dfb commit e20302a

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

src/ActiveRecordInheritanceTrait.php

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
*
7474
* @author José Lorente <jose.lorente.martin@gmail.com>
7575
*/
76-
trait ActiveRecordInheritanceTrait {
76+
trait ActiveRecordInheritanceTrait
77+
{
7778

7879
/**
7980
*
@@ -84,7 +85,8 @@ trait ActiveRecordInheritanceTrait {
8485
/**
8586
* @return \yii\db\ActiveRecordInterface
8687
*/
87-
public function getParent($recalculate = false) {
88+
public function getParent($recalculate = false)
89+
{
8890
if ($this->_parent === null || $recalculate === true) {
8991
if (($this instanceof ActiveRecordInheritanceInterface) === false) {
9092
throw new BaseException('Classes that use the \jlorente\db\ActiveRecordInheritanceTrait must implement \jlorente\db\ActiveRecordInheritanceInterface');
@@ -105,23 +107,26 @@ public function getParent($recalculate = false) {
105107
*
106108
* @return \yii\db\ActiveQueryInterface
107109
*/
108-
private function _parent() {
110+
private function _parent()
111+
{
109112
$class = static::extendsFrom();
110113
return $this->hasOne($class::className(), [$this->parentPrimaryKey() => $this->parentAttribute()]);
111114
}
112115

113116
/**
114117
* @inheritdoc
115118
*/
116-
public static function populateRecord($record, $row) {
119+
public static function populateRecord($record, $row)
120+
{
117121
parent::populateRecord($record, $row);
118122
$record->_parent = $record->_parent()->one();
119123
}
120124

121125
/**
122126
* @inheritdoc
123127
*/
124-
public function __get($name) {
128+
public function __get($name)
129+
{
125130
try {
126131
return parent::__get($name);
127132
} catch (UnknownPropertyException $e) {
@@ -132,7 +137,8 @@ public function __get($name) {
132137
/**
133138
* @inheritdoc
134139
*/
135-
public function __set($name, $value) {
140+
public function __set($name, $value)
141+
{
136142
try {
137143
parent::__set($name, $value);
138144
} catch (UnknownPropertyException $e) {
@@ -147,21 +153,23 @@ public function __set($name, $value) {
147153
/**
148154
* @inheritdoc
149155
*/
150-
public function setAttributes($values, $safeOnly = true) {
156+
public function setAttributes($values, $safeOnly = true)
157+
{
151158
$this->getParent()->setAttributes($values, $safeOnly);
152159
parent::setAttributes($values, $safeOnly);
153160
}
154161

155162
/**
156163
* @inheritdoc
157164
*/
158-
public function __isset($name) {
165+
public function __isset($name)
166+
{
159167
try {
160168
if (parent::__get($name) !== null) {
161169
return true;
162170
}
163171
} catch (UnknownPropertyException $e) {
164-
return $this->getParent()->{$name};
172+
return isset($this->getParent()->{$name});
165173
}
166174

167175
if (parent::__isset($name) === false) {
@@ -174,7 +182,8 @@ public function __isset($name) {
174182
/**
175183
* @inheritdoc
176184
*/
177-
public function __unset($name) {
185+
public function __unset($name)
186+
{
178187
try {
179188
if (parent::__get($name) !== null) {
180189
parent::__unset($name);
@@ -187,7 +196,8 @@ public function __unset($name) {
187196
/**
188197
* @inheritdoc
189198
*/
190-
public function __call($name, $params) {
199+
public function __call($name, $params)
200+
{
191201
try {
192202
return parent::__call($name, $params);
193203
} catch (UnknownMethodException $e) {
@@ -210,7 +220,8 @@ public function __call($name, $params) {
210220
*
211221
* ```
212222
*/
213-
public function attributeLabels() {
223+
public function attributeLabels()
224+
{
214225
return array_merge($this->getParent()->attributeLabels(), parent::attributeLabels());
215226
}
216227

@@ -223,7 +234,8 @@ public function attributeLabels() {
223234
* @param array $except list of attributes whose value should NOT be returned.
224235
* @return array attribute values (name => value).
225236
*/
226-
public function getAttributes($names = null, $except = array()) {
237+
public function getAttributes($names = null, $except = array())
238+
{
227239
if ($names === null) {
228240
$names = array_merge($this->getParent()->attributes(), $this->attributes());
229241
}
@@ -238,7 +250,8 @@ public function getAttributes($names = null, $except = array()) {
238250
* @return boolean
239251
* @throws \Exception
240252
*/
241-
public function save($runValidation = true, $attributeNames = null) {
253+
public function save($runValidation = true, $attributeNames = null)
254+
{
242255
if ($runValidation === true && $this->validate($attributeNames) === false) {
243256
Yii::info('Model not inserted due to validation error.', __METHOD__);
244257
return false;
@@ -281,7 +294,8 @@ public function save($runValidation = true, $attributeNames = null) {
281294
* being deleted is outdated.
282295
* @throws \Exception in case delete failed.
283296
*/
284-
public function delete() {
297+
public function delete()
298+
{
285299
$trans = static::getDb()->beginTransaction();
286300
try {
287301
if (parent::delete() === false) {
@@ -307,10 +321,11 @@ public function delete() {
307321
* @return boolean
308322
* @throws Exception
309323
*/
310-
public function validate($attributeNames = null, $clearErrors = true) {
324+
public function validate($attributeNames = null, $clearErrors = true)
325+
{
311326
$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);
314329
return $this->getParent()->validate($attributeNames, $clearErrors) && $r;
315330
}
316331

@@ -322,7 +337,8 @@ public function validate($attributeNames = null, $clearErrors = true) {
322337
* @param string|null $attribute attribute name. Use null to check all attributes.
323338
* @return boolean whether there is any error.
324339
*/
325-
public function hasErrors($attribute = null) {
340+
public function hasErrors($attribute = null)
341+
{
326342
return $this->getParent()->hasErrors($attribute) || parent::hasErrors($attribute);
327343
}
328344

@@ -352,7 +368,8 @@ public function hasErrors($attribute = null) {
352368
* @see getFirstErrors()
353369
* @see getFirstError()
354370
*/
355-
public function getErrors($attribute = null) {
371+
public function getErrors($attribute = null)
372+
{
356373
return array_merge($this->getParent()->getErrors($attribute), parent::getErrors($attribute));
357374
}
358375

@@ -366,7 +383,8 @@ public function getErrors($attribute = null) {
366383
* @see getErrors()
367384
* @see getFirstError()
368385
*/
369-
public function getFirstErrors() {
386+
public function getFirstErrors()
387+
{
370388
$errs = $this->getErrors();
371389
if (empty($errs)) {
372390
return [];
@@ -392,15 +410,17 @@ public function getFirstErrors() {
392410
* @see getErrors()
393411
* @see getFirstErrors()
394412
*/
395-
public function getFirstError($attribute) {
413+
public function getFirstError($attribute)
414+
{
396415
$errors = $this->getErrors($attribute);
397416
return count($errors) ? $errors[0] : null;
398417
}
399418

400419
/**
401420
* @inheritdoc
402421
*/
403-
public function refresh() {
422+
public function refresh()
423+
{
404424
$r = parent::refresh();
405425
return $this->getParent()->refresh() && $r;
406426
}
@@ -411,7 +431,8 @@ public function refresh() {
411431
*
412432
* @return string
413433
*/
414-
public function parentAttribute() {
434+
public function parentAttribute()
435+
{
415436
return static::primaryKey()[0];
416437
}
417438

@@ -420,22 +441,25 @@ public function parentAttribute() {
420441
*
421442
* @return string
422443
*/
423-
public function parentPrimaryKey() {
444+
public function parentPrimaryKey()
445+
{
424446
$pClass = static::extendsFrom();
425447
return $pClass::primaryKey()[0];
426448
}
427449

428450
/**
429451
* @inheritdoc
430452
*/
431-
public function fields() {
453+
public function fields()
454+
{
432455
return ArrayHelper::merge($this->parent->fields(), parent::fields());
433456
}
434457

435458
/**
436459
* @inheritdoc
437460
*/
438-
public function loadDefaultValues($skipIfSet = true) {
461+
public function loadDefaultValues($skipIfSet = true)
462+
{
439463
$this->parent->loadDefaultValues($skipIfSet);
440464
parent::loadDefaultValues($skipIfSet);
441465
}

0 commit comments

Comments
 (0)