Skip to content
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

[11.x] Fully qualify morph columns when using WHERE clauses #52227

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public function whereMorphedTo($relation, $model, $boolean = 'and')
}

if (is_null($model)) {
return $this->whereNull($relation->getMorphType(), $boolean);
return $this->whereNull($relation->qualifyColumn($relation->getMorphType()), $boolean);
}

if (is_string($model)) {
Expand All @@ -476,12 +476,12 @@ public function whereMorphedTo($relation, $model, $boolean = 'and')
$model = array_search($model, $morphMap, true);
}

return $this->where($relation->getMorphType(), $model, null, $boolean);
return $this->where($relation->qualifyColumn($relation->getMorphType()), $model, null, $boolean);
}

return $this->where(function ($query) use ($relation, $model) {
$query->where($relation->getMorphType(), $model->getMorphClass())
->where($relation->getForeignKeyName(), $model->getKey());
$query->where($relation->qualifyColumn($relation->getMorphType()), $model->getMorphClass())
->where($relation->qualifyColumn($relation->getForeignKeyName()), $model->getKey());
}, null, null, $boolean);
}

Expand All @@ -505,12 +505,12 @@ public function whereNotMorphedTo($relation, $model, $boolean = 'and')
$model = array_search($model, $morphMap, true);
}

return $this->whereNot($relation->getMorphType(), '<=>', $model, $boolean);
return $this->whereNot($relation->qualifyColumn($relation->getMorphType()), '<=>', $model, $boolean);
}

return $this->whereNot(function ($query) use ($relation, $model) {
$query->where($relation->getMorphType(), '<=>', $model->getMorphClass())
->where($relation->getForeignKeyName(), '<=>', $model->getKey());
$query->where($relation->qualifyColumn($relation->getMorphType()), '<=>', $model->getMorphClass())
->where($relation->qualifyColumn($relation->getForeignKeyName()), '<=>', $model->getKey());
}, null, null, $boolean);
}

Expand Down
22 changes: 11 additions & 11 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@ public function testWhereMorphedTo()

$builder = $model->whereMorphedTo('morph', $relatedModel);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where ("morph_type" = ? and "morph_id" = ?)', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where ("eloquent_builder_test_model_parent_stubs"."morph_type" = ? and "eloquent_builder_test_model_parent_stubs"."morph_id" = ?)', $builder->toSql());
$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

Expand All @@ -1784,7 +1784,7 @@ public function testWhereMorphedToNull()
$this->mockConnectionForModel($model, '');

$builder = $model->whereMorphedTo('morph', null);
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "morph_type" is null', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "eloquent_builder_test_model_parent_stubs"."morph_type" is null', $builder->toSql());
}

public function testWhereNotMorphedTo()
Expand All @@ -1797,7 +1797,7 @@ public function testWhereNotMorphedTo()

$builder = $model->whereNotMorphedTo('morph', $relatedModel);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not ("morph_type" <=> ? and "morph_id" <=> ?)', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not ("eloquent_builder_test_model_parent_stubs"."morph_type" <=> ? and "eloquent_builder_test_model_parent_stubs"."morph_id" <=> ?)', $builder->toSql());
$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

Expand All @@ -1811,7 +1811,7 @@ public function testOrWhereMorphedTo()

$builder = $model->where('bar', 'baz')->orWhereMorphedTo('morph', $relatedModel);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or ("morph_type" = ? and "morph_id" = ?)', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or ("eloquent_builder_test_model_parent_stubs"."morph_type" = ? and "eloquent_builder_test_model_parent_stubs"."morph_id" = ?)', $builder->toSql());
$this->assertEquals(['baz', $relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

Expand All @@ -1822,7 +1822,7 @@ public function testOrWhereMorphedToNull()

$builder = $model->where('bar', 'baz')->orWhereMorphedTo('morph', null);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or "morph_type" is null', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or "eloquent_builder_test_model_parent_stubs"."morph_type" is null', $builder->toSql());
$this->assertEquals(['baz'], $builder->getBindings());
}

Expand All @@ -1836,7 +1836,7 @@ public function testOrWhereNotMorphedTo()

$builder = $model->where('bar', 'baz')->orWhereNotMorphedTo('morph', $relatedModel);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not ("morph_type" <=> ? and "morph_id" <=> ?)', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not ("eloquent_builder_test_model_parent_stubs"."morph_type" <=> ? and "eloquent_builder_test_model_parent_stubs"."morph_id" <=> ?)', $builder->toSql());
$this->assertEquals(['baz', $relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());
}

Expand All @@ -1847,7 +1847,7 @@ public function testWhereMorphedToClass()

$builder = $model->whereMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "morph_type" = ?', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "eloquent_builder_test_model_parent_stubs"."morph_type" = ?', $builder->toSql());
$this->assertEquals([EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
}

Expand All @@ -1858,7 +1858,7 @@ public function testWhereNotMorphedToClass()

$builder = $model->whereNotMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not "morph_type" <=> ?', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not "eloquent_builder_test_model_parent_stubs"."morph_type" <=> ?', $builder->toSql());
$this->assertEquals([EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
}

Expand All @@ -1869,7 +1869,7 @@ public function testOrWhereMorphedToClass()

$builder = $model->where('bar', 'baz')->orWhereMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or "morph_type" = ?', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or "eloquent_builder_test_model_parent_stubs"."morph_type" = ?', $builder->toSql());
$this->assertEquals(['baz', EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
}

Expand All @@ -1880,7 +1880,7 @@ public function testOrWhereNotMorphedToClass()

$builder = $model->where('bar', 'baz')->orWhereNotMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not "morph_type" <=> ?', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "bar" = ? or not "eloquent_builder_test_model_parent_stubs"."morph_type" <=> ?', $builder->toSql());
$this->assertEquals(['baz', EloquentBuilderTestModelCloseRelatedStub::class], $builder->getBindings());
}

Expand All @@ -1895,7 +1895,7 @@ public function testWhereMorphedToAlias()

$builder = $model->whereMorphedTo('morph', EloquentBuilderTestModelCloseRelatedStub::class);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "morph_type" = ?', $builder->toSql());
$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "eloquent_builder_test_model_parent_stubs"."morph_type" = ?', $builder->toSql());
$this->assertEquals(['alias'], $builder->getBindings());

Relation::morphMap([], false);
Expand Down