Skip to content

[5.7] Prevent breaking eager loading with string keys #26622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public function relatedUpdatedAt()
protected function whereInMethod(Model $model, $key)
{
return $model->getKeyName() === last(explode('.', $key))
&& in_array($model->getKeyType(), ['int', 'integer'])
&& $model->getIncrementing()
? 'whereIntegerInRaw'
: 'whereIn';
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ public function testDefaultEagerConstraintsWhenNotIncrementing()
{
$relation = $this->getRelation(null, false);
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', m::mustBe([null]));
$relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([null]));
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getParent()->shouldReceive('getIncrementing')->once()->andReturn(true);
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasManyModelStub;
$model1->id = 1;
Expand All @@ -214,7 +214,7 @@ public function testEagerConstraintsAreProperlyAddedWithStringKey()
{
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('string');
$relation->getParent()->shouldReceive('getIncrementing')->once()->andReturn(false);
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasManyModelStub;
$model1->id = 1;
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getParent()->shouldReceive('getIncrementing')->once()->andReturn(true);
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasOneModelStub;
$model1->id = 1;
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testMorphOneEagerConstraintsAreProperlyAdded()
{
$relation = $this->getOneRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('string');
$relation->getParent()->shouldReceive('getIncrementing')->once()->andReturn(false);
$relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', [1, 2]);
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));

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

Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentMorphToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->andReturn('int');
$relation->getParent()->shouldReceive('getIncrementing')->once()->andReturn(true);
$relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('taggables.taggable_id', [1, 2]);
$relation->getQuery()->shouldReceive('where')->once()->with('taggables.taggable_type', get_class($relation->getParent()));
$model1 = new EloquentMorphToManyModelStub;
Expand Down