Skip to content

Commit

Permalink
fix(database): fully qualify morph columns when using WHERE clauses (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenpaauw authored Jul 29, 2024
1 parent d267a53 commit c2eb318
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
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

0 comments on commit c2eb318

Please sign in to comment.