From 7eea14aa9539d780f32de9ae37a3b4212b0135bc Mon Sep 17 00:00:00 2001 From: Will Taylor-Jackson Date: Mon, 18 Nov 2024 14:40:05 +0000 Subject: [PATCH 1/3] feat: use model to qualify column --- src/Illuminate/Database/Eloquent/Relations/BelongsTo.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 387ccc7ab33f..faab67e34734 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -96,9 +96,9 @@ public function addConstraints() // For belongs to relationships, which are essentially the inverse of has one // or has many relationships, we need to actually query on the primary key // of the related models matching on the foreign key that's on a parent. - $table = $this->related->getTable(); + $key = $this->related->qualifyColumn($this->ownerKey); - $this->query->where($table.'.'.$this->ownerKey, '=', $this->getForeignKeyFrom($this->child)); + $this->query->where($key, '=', $this->getForeignKeyFrom($this->child)); } } @@ -108,7 +108,7 @@ public function addEagerConstraints(array $models) // We'll grab the primary key name of the related models since it could be set to // a non-standard name and not "id". We will then construct the constraint for // our eagerly loading query so it returns the proper models from execution. - $key = $this->related->getTable().'.'.$this->ownerKey; + $key = $this->related->qualifyColumn($this->ownerKey); $whereIn = $this->whereInMethod($this->related, $this->ownerKey); From 16b55d7a03e45109e0d74f7ada5d9bf135089854 Mon Sep 17 00:00:00 2001 From: Will Taylor-Jackson Date: Mon, 18 Nov 2024 14:40:36 +0000 Subject: [PATCH 2/3] tests: update mock --- tests/Database/DatabaseEloquentBelongsToTest.php | 1 + tests/Database/DatabaseEloquentMorphToTest.php | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentBelongsToTest.php b/tests/Database/DatabaseEloquentBelongsToTest.php index 1ace437a4346..750a1f0237fd 100755 --- a/tests/Database/DatabaseEloquentBelongsToTest.php +++ b/tests/Database/DatabaseEloquentBelongsToTest.php @@ -404,6 +404,7 @@ protected function getRelation($parent = null, $keyType = 'int') $this->related->shouldReceive('getKeyType')->andReturn($keyType); $this->related->shouldReceive('getKeyName')->andReturn('id'); $this->related->shouldReceive('getTable')->andReturn('relation'); + $this->related->shouldReceive('qualifyColumn')->andReturnUsing(fn (string $column) => "relation.{$column}"); $this->builder->shouldReceive('getModel')->andReturn($this->related); $parent = $parent ?: new EloquentBelongsToModelStub; diff --git a/tests/Database/DatabaseEloquentMorphToTest.php b/tests/Database/DatabaseEloquentMorphToTest.php index 0366126f450c..d85b4bf15b06 100644 --- a/tests/Database/DatabaseEloquentMorphToTest.php +++ b/tests/Database/DatabaseEloquentMorphToTest.php @@ -368,6 +368,7 @@ protected function getRelationAssociate($parent) $related = m::mock(Model::class); $related->shouldReceive('getKey')->andReturn(1); $related->shouldReceive('getTable')->andReturn('relation'); + $related->shouldReceive('qualifyColumn')->andReturnUsing(fn (string $column) => "relation.{$column}"); $builder->shouldReceive('getModel')->andReturn($related); return new MorphTo($builder, $parent, 'foreign_key', 'id', 'morph_type', 'relation'); @@ -378,8 +379,9 @@ public function getRelation($parent = null, $builder = null) $this->builder = $builder ?: m::mock(Builder::class); $this->builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value'); $this->related = m::mock(Model::class); - $this->related->shouldReceive('getKeyName')->andReturn('id'); + $this->related->shouldReceive('getcolumn')->andReturn('id'); $this->related->shouldReceive('getTable')->andReturn('relation'); + $this->related->shouldReceive('qualifyColumn')->andReturnUsing(fn (string $column) => "relation.{$column}"); $this->builder->shouldReceive('getModel')->andReturn($this->related); $parent = $parent ?: new EloquentMorphToModelStub; From 392408b3170404e8acf58d7a0f6515e8212030ff Mon Sep 17 00:00:00 2001 From: Will Taylor-Jackson Date: Mon, 18 Nov 2024 15:08:26 +0000 Subject: [PATCH 3/3] refactor: use existing method --- src/Illuminate/Database/Eloquent/Relations/BelongsTo.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index faab67e34734..7822de3c2ad0 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -96,7 +96,7 @@ public function addConstraints() // For belongs to relationships, which are essentially the inverse of has one // or has many relationships, we need to actually query on the primary key // of the related models matching on the foreign key that's on a parent. - $key = $this->related->qualifyColumn($this->ownerKey); + $key = $this->getQualifiedOwnerKeyName(); $this->query->where($key, '=', $this->getForeignKeyFrom($this->child)); } @@ -108,7 +108,7 @@ public function addEagerConstraints(array $models) // We'll grab the primary key name of the related models since it could be set to // a non-standard name and not "id". We will then construct the constraint for // our eagerly loading query so it returns the proper models from execution. - $key = $this->related->qualifyColumn($this->ownerKey); + $key = $this->getQualifiedOwnerKeyName(); $whereIn = $this->whereInMethod($this->related, $this->ownerKey);