Skip to content

Commit

Permalink
Fixed infinite recursion when trying to access a parent value without…
Browse files Browse the repository at this point in the history
… having instantiated a parent yet
  • Loading branch information
jlorente committed Nov 9, 2023
1 parent e20302a commit ff20d99
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/ActiveRecordInheritanceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public function getParent($recalculate = false)
throw new BaseException('Classes that use the \jlorente\db\ActiveRecordInheritanceTrait must implement \jlorente\db\ActiveRecordInheritanceInterface');
}
$pClass = static::extendsFrom();
$parent = $this->_parent()->one();
if ($this->getIsNewRecord() === false || $parent !== null) {
if ($this->getParentAttributeValue() && $this->getIsNewRecord() === false && ($parent = $this->_parent()->one())) {
$this->_parent = $parent;
} else {
$this->_parent = new $pClass();
Expand Down Expand Up @@ -464,4 +463,15 @@ public function loadDefaultValues($skipIfSet = true)
parent::loadDefaultValues($skipIfSet);
}

/**
* Gets the parent attribute value.
*
* @return mixed|null
*/
public function getParentAttributeValue()
{
$ownAttributes = $this->attributes();
$parentAttribute = $this->parentAttribute();
return isset($ownAttributes[$parentAttribute]) ? $ownAttributes[$parentAttribute] : null;
}
}

0 comments on commit ff20d99

Please sign in to comment.