Skip to content

Commit

Permalink
FIX Protect against loading incorrect eager-loaded relations.
Browse files Browse the repository at this point in the history
Don't fetch eager-loaded has_many or many_many unless we've validated
that the relation IS actually a has_many or many_many relation.
Also clear eager-loaded relations when flushing the record.
  • Loading branch information
GuySartorelli committed Jul 17, 2023
1 parent acad946 commit 3bf845a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/ORM/DataObject.php
Expand Up @@ -1954,12 +1954,6 @@ public function setEagerLoadedData(string $eagerLoadRelation, mixed $eagerLoaded
*/
public function getComponents($componentName, $id = null)
{
if (isset($this->eagerLoadedData[$componentName])) {
return $this->eagerLoadedData[$componentName];
}
if (!isset($id)) {
$id = $this->ID;
}
$result = null;

$schema = $this->getSchema();
Expand All @@ -1972,7 +1966,14 @@ public function getComponents($componentName, $id = null)
));
}

if (isset($this->eagerLoadedData[$componentName])) {
return $this->eagerLoadedData[$componentName];
}

// If we haven't been written yet, we can't save these relations, so use a list that handles this case
if (!isset($id)) {
$id = $this->ID;
}
if (!$id) {
if (!isset($this->unsavedRelations[$componentName])) {
$this->unsavedRelations[$componentName] =
Expand Down Expand Up @@ -2174,12 +2175,6 @@ public function inferReciprocalComponent($remoteClass, $remoteRelation)
*/
public function getManyManyComponents($componentName, $id = null)
{
if (isset($this->eagerLoadedData[$componentName])) {
return $this->eagerLoadedData[$componentName];
}
if (!isset($id)) {
$id = $this->ID;
}
$schema = static::getSchema();
$manyManyComponent = $schema->manyManyComponent(static::class, $componentName);
if (!$manyManyComponent) {
Expand All @@ -2190,7 +2185,14 @@ public function getManyManyComponents($componentName, $id = null)
));
}

if (isset($this->eagerLoadedData[$componentName])) {
return $this->eagerLoadedData[$componentName];
}

// If we haven't been written yet, we can't save these relations, so use a list that handles this case
if (!isset($id)) {
$id = $this->ID;
}
if (!$id) {
if (!isset($this->unsavedRelations[$componentName])) {
$this->unsavedRelations[$componentName] = new UnsavedRelationList(
Expand Down Expand Up @@ -3447,6 +3449,7 @@ public function flushCache($persistent = true)
$this->extend('flushCache');

$this->components = [];
$this->eagerLoadedData = [];
return $this;
}

Expand Down

0 comments on commit 3bf845a

Please sign in to comment.