Skip to content

Commit

Permalink
Prevent triggering belongs to query if there are no relations to load #…
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed May 30, 2022
1 parent acddea7 commit c3253ab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
21 changes: 20 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class BelongsTo extends Relation
InteractsWithDictionary,
SupportsDefaultModels;

protected bool $hasValidConstrains = true;

/**
* The child model instance of the relation.
*
Expand Down Expand Up @@ -113,7 +115,15 @@ public function addEagerConstraints(array $models)

$whereIn = $this->whereInMethod($this->related, $this->ownerKey);

$this->query->{$whereIn}($key, $this->getEagerModelKeys($models));
// Do trigger query if there is nothing to load
$eagerModelKeys = $this->getEagerModelKeys($models);

if ($eagerModelKeys === []) {
$this->hasValidConstrains = false;
return;
}

$this->query->{$whereIn}($key, $eagerModelKeys);
}

/**
Expand All @@ -140,6 +150,15 @@ protected function getEagerModelKeys(array $models)
return array_values(array_unique($keys));
}

public function getEager()
{
if ($this->hasValidConstrains) {
return parent::getEager();
}

return new Collection();
}

/**
* Initialize the relation on a set of models.
*
Expand Down
19 changes: 16 additions & 3 deletions tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public function testIdsInEagerConstraintsCanBeZero()
$relation->addEagerConstraints($models);
}

public function testIdsInEagerConstraintsWithMixedRelationExistOrNonExists()
{
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', [0, 'foreign.value']);
$models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStubWithZeroId, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}

public function testRelationIsProperlyInitialized()
{
$relation = $this->getRelation();
Expand Down Expand Up @@ -175,15 +185,17 @@ public function testDefaultEagerConstraintsWhenIncrementing()
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', m::mustBe([]));
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->never();
$relation->getQuery()->shouldReceive('whereIn')->never();
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}

public function testDefaultEagerConstraintsWhenIncrementingAndNonIntKeyType()
{
$relation = $this->getRelation(null, 'string');
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([]));
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->never();
$relation->getQuery()->shouldReceive('whereIn')->never();
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand All @@ -193,7 +205,8 @@ public function testDefaultEagerConstraintsWhenNotIncrementing()
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', m::mustBe([]));
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->never();
$relation->getQuery()->shouldReceive('whereIn')->never();
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand Down

1 comment on commit c3253ab

@Rijen
Copy link

@Rijen Rijen commented on c3253ab Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, a useful fix.
Waiting for it to be accepted....

Please sign in to comment.