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

[10.x] Use the dedicated key getters in BelongsTo #48509

Merged
merged 4 commits into from
Sep 26, 2023
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
8 changes: 2 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,13 @@ public function initRelation(array $models, $relation)
*/
public function match(array $models, Collection $results, $relation)
{
$foreign = $this->foreignKey;

$owner = $this->ownerKey;

// First we will get to build a dictionary of the child models by their primary
// key of the relationship, then we can easily match the children back onto
// the parents using that dictionary and the primary key of the children.
$dictionary = [];

foreach ($results as $result) {
$attribute = $this->getDictionaryKey($result->getAttribute($owner));
$attribute = $this->getDictionaryKey($this->getRelatedKeyFrom($result));

$dictionary[$attribute] = $result;
}
Expand All @@ -185,7 +181,7 @@ public function match(array $models, Collection $results, $relation)
// and match back onto their children using these keys of the dictionary and
// the primary key of the children to map them onto the correct instances.
foreach ($models as $model) {
$attribute = $this->getDictionaryKey($model->{$foreign});
$attribute = $this->getDictionaryKey($this->getForeignKeyFrom($model));

if (isset($dictionary[$attribute])) {
$model->setRelation($relation, $dictionary[$attribute]);
Expand Down
24 changes: 16 additions & 8 deletions tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;

class DatabaseEloquentBelongsToTest extends TestCase
{
Expand Down Expand Up @@ -99,18 +98,27 @@ public function testRelationIsProperlyInitialized()
public function testModelsAreProperlyMatchedToParents()
{
$relation = $this->getRelation();
$result1 = m::mock(stdClass::class);
$result1->shouldReceive('getAttribute')->with('id')->andReturn(1);
$result2 = m::mock(stdClass::class);
$result2->shouldReceive('getAttribute')->with('id')->andReturn(2);
$result3 = m::mock(stdClass::class);
$result3->shouldReceive('getAttribute')->with('id')->andReturn(new class

$result1 = new class extends Model
{
protected $attributes = ['id' => 1];
};

$result2 = new class extends Model
{
protected $attributes = ['id' => 2];
};

$result3 = new class extends Model
{
protected $attributes = ['id' => 3];

public function __toString()
{
return '3';
}
});
};

$model1 = new EloquentBelongsToModelStub;
$model1->foreign_key = 1;
$model2 = new EloquentBelongsToModelStub;
Expand Down