Skip to content

Commit ff20d99

Browse files
committed
Fixed infinite recursion when trying to access a parent value without having instantiated a parent yet
1 parent e20302a commit ff20d99

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/ActiveRecordInheritanceTrait.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public function getParent($recalculate = false)
9292
throw new BaseException('Classes that use the \jlorente\db\ActiveRecordInheritanceTrait must implement \jlorente\db\ActiveRecordInheritanceInterface');
9393
}
9494
$pClass = static::extendsFrom();
95-
$parent = $this->_parent()->one();
96-
if ($this->getIsNewRecord() === false || $parent !== null) {
95+
if ($this->getParentAttributeValue() && $this->getIsNewRecord() === false && ($parent = $this->_parent()->one())) {
9796
$this->_parent = $parent;
9897
} else {
9998
$this->_parent = new $pClass();
@@ -464,4 +463,15 @@ public function loadDefaultValues($skipIfSet = true)
464463
parent::loadDefaultValues($skipIfSet);
465464
}
466465

466+
/**
467+
* Gets the parent attribute value.
468+
*
469+
* @return mixed|null
470+
*/
471+
public function getParentAttributeValue()
472+
{
473+
$ownAttributes = $this->attributes();
474+
$parentAttribute = $this->parentAttribute();
475+
return isset($ownAttributes[$parentAttribute]) ? $ownAttributes[$parentAttribute] : null;
476+
}
467477
}

0 commit comments

Comments
 (0)