Skip to content

Commit

Permalink
FIX: Save repeated database queries to fetch elemental area name (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kinglozzer committed Aug 23, 2023
1 parent 4b19889 commit e17dbf1
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,16 +894,19 @@ public function getAreaRelationName()
$result = 'ElementalArea';

if ($page) {
$has_one = $page->config()->get('has_one');
$area = $this->Parent();

foreach ($has_one as $relationName => $relationClass) {
if ($page instanceof BaseElement && $relationName === 'Parent') {
continue;
}
if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) {
$result = $relationName;
break;
$class = DataObject::getSchema()->hasOneComponent($this, 'Parent');
$area = $this->ParentID ? DataObject::get_by_id($class, $this->ParentID) : null;

if ($area) {
$has_one = $page->config()->get('has_one');
foreach ($has_one as $relationName => $relationClass) {
if ($page instanceof BaseElement && $relationName === 'Parent') {
continue;
}
if ($relationClass === $area->ClassName && $page->{$relationName}()->ID === $area->ID) {
$result = $relationName;
break;
}
}
}
}
Expand Down

0 comments on commit e17dbf1

Please sign in to comment.