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

fix: use qualifyColumn rather than assuming format #53559

Merged
merged 3 commits into from
Nov 18, 2024
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
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->getQualifiedOwnerKeyName();

$this->query->where($table.'.'.$this->ownerKey, '=', $this->getForeignKeyFrom($this->child));
$this->query->where($key, '=', $this->getForeignKeyFrom($this->child));
}
}

Expand All @@ -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->getQualifiedOwnerKeyName();

$whereIn = $this->whereInMethod($this->related, $this->ownerKey);

Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion tests/Database/DatabaseEloquentMorphToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');

Choose a reason for hiding this comment

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

Is this supposed to be getColumn?

Copy link
Member

Choose a reason for hiding this comment

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

doesn't really matter. method names are not case sensitive.

$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;

Expand Down