Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function addConstraints()
if (static::$constraints) {
$this->getRelationQuery()->where($this->morphType, $this->morphClass);

$this->getRelationQuery()->where(
$this->foreignKey, '=', (string) $this->getParentKey()
);

$this->getRelationQuery()->whereNotNull($this->foreignKey);
Comment on lines 58 to +64
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you just do this?

Suggested change
$this->getRelationQuery()->where($this->morphType, $this->morphClass);
parent::addConstraints();
$this->getRelationQuery()->where(
$this->foreignKey, '=', (string) $this->getParentKey()
);
$this->getRelationQuery()->whereNotNull($this->foreignKey);
$this->getRelationQuery()
->where($this->morphType, $this->morphClass)
->where($this->foreignKey, '=', (string) $this->getParentKey())
->whereNotNull($this->foreignKey);

Copy link
Author

Choose a reason for hiding this comment

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

yeah it is a better implementation but don't we want to to keep the parent::addConstraints(); line at the begining ?

Copy link
Contributor

Choose a reason for hiding this comment

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

we don't and we don't keep it

parent::addConstraints();
}
}
Expand All @@ -65,6 +70,12 @@ public function addConstraints()
public function addEagerConstraints(array $models)
{
parent::addEagerConstraints($models);
$this->whereInEager(
'whereIn',
$this->foreignKey,
array_map(fn ($value) => (string) $value, $this->getKeys($models, $this->localKey)),
$this->getRelationQuery()
);

$this->getRelationQuery()->where($this->morphType, $this->morphClass);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentMorphOneOfManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function testEagerLoadingAppliesConstraintsToInnerJoinSubQuery()
$relation = $product->current_state();
$relation->addEagerConstraints([$product]);
$this->assertSame('select MAX("states"."id") as "id_aggregate", "states"."stateful_id", "states"."stateful_type" from "states" where "states"."stateful_type" = ? and "states"."stateful_id" = ? and "states"."stateful_id" is not null and "states"."stateful_id" in (1) and "states"."stateful_type" = ? group by "states"."stateful_id", "states"."stateful_type"', $relation->getOneOfManySubQuery()->toSql());

}

public function testReceivingModel()
Expand Down
22 changes: 16 additions & 6 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ protected function tearDown(): void
public function testMorphOneSetsProperConstraints()
{
$this->getOneRelation();

$this->assertTrue(true);
}

public function testMorphOneEagerConstraintsAreProperlyAdded()
{
$relation = $this->getOneRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('string');
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', [1, 2]);
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', ['1', '2']);
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));

$model1 = new EloquentMorphResetModelStub;
$model1->id = 1;
$model2 = new EloquentMorphResetModelStub;
$model2->id = 2;
$relation->addEagerConstraints([$model1, $model2]);

$this->assertTrue(true);
}

/**
Expand All @@ -50,21 +54,25 @@ public function testMorphOneEagerConstraintsAreProperlyAdded()
public function testMorphManySetsProperConstraints()
{
$this->getManyRelation();

$this->assertTrue(true);
}

public function testMorphManyEagerConstraintsAreProperlyAdded()
{
$relation = $this->getManyRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('table.morph_id', [1, 2]);
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', ['1', '2']);
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));

$model1 = new EloquentMorphResetModelStub;
$model1->id = 1;
$model2 = new EloquentMorphResetModelStub;
$model2->id = 2;
$relation->addEagerConstraints([$model1, $model2]);

$this->assertTrue(true);
}

public function testMorphRelationUpsertFillsForeignKey()
Expand Down Expand Up @@ -102,6 +110,8 @@ public function testMorphRelationUpsertFillsForeignKey()
['email'],
['name']
);

$this->assertTrue(true);
}

public function testMakeFunctionOnMorph()
Expand Down Expand Up @@ -484,7 +494,7 @@ protected function getOneRelation()
$queryBuilder = m::mock(QueryBuilder::class);
$builder = m::mock(Builder::class, [$queryBuilder]);
$builder->shouldReceive('whereNotNull')->once()->with('table.morph_id');
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', '1');
$related = m::mock(Model::class);
$builder->shouldReceive('getModel')->andReturn($related);
$parent = m::mock(Model::class);
Expand All @@ -499,7 +509,7 @@ protected function getManyRelation()
{
$builder = m::mock(Builder::class);
$builder->shouldReceive('whereNotNull')->once()->with('table.morph_id');
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', '1');
$related = m::mock(Model::class);
$builder->shouldReceive('getModel')->andReturn($related);
$parent = m::mock(Model::class);
Expand All @@ -520,7 +530,7 @@ protected function getNamespacedRelation($alias)

$builder = m::mock(Builder::class);
$builder->shouldReceive('whereNotNull')->once()->with('table.morph_id');
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
$builder->shouldReceive('where')->once()->with('table.morph_id', '=', '1');
$related = m::mock(Model::class);
$builder->shouldReceive('getModel')->andReturn($related);
$parent = m::mock(EloquentModelNamespacedStub::class);
Expand Down
Loading