Skip to content

Commit

Permalink
[10.x] Use the dedicated key getters in BelongsTo (#48509)
Browse files Browse the repository at this point in the history
* [10.x] Use the dedicated key getters in BelongsTo

* fix method call

* Update tests

* fix style ci
  • Loading branch information
iamgergo committed Sep 26, 2023
1 parent ebcb151 commit 13dcd8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
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

0 comments on commit 13dcd8f

Please sign in to comment.