Skip to content

Commit

Permalink
Merge pull request #1085 from kinglozzer/928-repeated-queries
Browse files Browse the repository at this point in the history
FIX: Save repeated database queries to fetch elemental area name (fixes #928)
  • Loading branch information
GuySartorelli authored Aug 23, 2023
2 parents 4b19889 + e17dbf1 commit d8a76f3
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 d8a76f3

Please sign in to comment.