Skip to content

Commit

Permalink
[8.x] Make use of specified ownerKey in MorphTo::associate() (attempt #2
Browse files Browse the repository at this point in the history
) (#36303)

* Check ownerKey first, before defaulting to getKeyName.

* Fix tests for fetching ownerKey.
  • Loading branch information
garbetjie authored Feb 17, 2021
1 parent 918db60 commit 16ab323
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ protected function matchToMorphParents($type, Collection $results)
public function associate($model)
{
$this->parent->setAttribute(
$this->foreignKey, $model instanceof Model ? $model->getKey() : null
$this->foreignKey, $model instanceof Model ? $model->{$this->ownerKey ?: $model->getKeyName()} : null
);

$this->parent->setAttribute(
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentMorphToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function testAssociateMethodSetsForeignKeyAndTypeOnModel()
$relation = $this->getRelationAssociate($parent);

$associate = m::mock(Model::class);
$associate->shouldReceive('getKey')->once()->andReturn(1);
$associate->shouldReceive('getAttribute')->once()->andReturn(1);
$associate->shouldReceive('getMorphClass')->once()->andReturn('Model');

$parent->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
Expand Down

0 comments on commit 16ab323

Please sign in to comment.