diff --git a/src/ActiveRecordInheritanceTrait.php b/src/ActiveRecordInheritanceTrait.php index 32f347a..d79948e 100644 --- a/src/ActiveRecordInheritanceTrait.php +++ b/src/ActiveRecordInheritanceTrait.php @@ -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(); @@ -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; + } }