Skip to content

Commit bfbe055

Browse files
authored
[Fix] morphTo relationship (#2669)
* KeyName as default value for ownerKey; * Add test for inverse MorphTo; * Add test for inverse MorphTo with custom ownerKey; * Remove comment; * Fix phpcs; * keyName of queried model as default ownerKey; * Update test; * Revert "KeyName as default value for ownerKey;" This reverts commit 54c9a85 * Fix phpcs; * Update MorphTo.php * Default value for ownerKey; * Check if the related model is original; * Fix phpcs;
1 parent 5387d54 commit bfbe055

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/Eloquent/HybridRelations.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
221221
$this->newQuery(),
222222
$this,
223223
$id,
224-
$ownerKey,
224+
$ownerKey ?: $this->getKeyName(),
225225
$type,
226226
$name,
227227
);
@@ -236,6 +236,11 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
236236

237237
$ownerKey ??= $instance->getKeyName();
238238

239+
// Check if it is a relation with an original model.
240+
if (! is_subclass_of($instance, MongoDBModel::class)) {
241+
return parent::morphTo($name, $type, $id, $ownerKey);
242+
}
243+
239244
return new MorphTo(
240245
$instance->newQuery(),
241246
$this,

src/Relations/MorphTo.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public function addConstraints()
1616
// For belongs to relationships, which are essentially the inverse of has one
1717
// or has many relationships, we need to actually query on the primary key
1818
// of the related models matching on the foreign key that's on a parent.
19-
$this->query->where($this->ownerKey, '=', $this->parent->{$this->foreignKey});
19+
$this->query->where(
20+
$this->ownerKey,
21+
'=',
22+
$this->getForeignKeyFrom($this->parent),
23+
);
2024
}
2125
}
2226

tests/Models/Photo.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public function hasImage(): MorphTo
1717
{
1818
return $this->morphTo();
1919
}
20+
21+
public function hasImageWithCustomOwnerKey(): MorphTo
22+
{
23+
return $this->morphTo(ownerKey: 'cclient_id');
24+
}
2025
}

tests/RelationsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,25 @@ public function testMorph(): void
470470
$relations = $photos[1]->getRelations();
471471
$this->assertArrayHasKey('hasImage', $relations);
472472
$this->assertInstanceOf(Client::class, $photos[1]->hasImage);
473+
474+
// inverse
475+
$photo = Photo::query()->create(['url' => 'https://graph.facebook.com/hans.thomas/picture']);
476+
$client = Client::create(['name' => 'Hans Thomas']);
477+
$photo->hasImage()->associate($client)->save();
478+
479+
$this->assertCount(1, $photo->hasImage()->get());
480+
$this->assertInstanceOf(Client::class, $photo->hasImage);
481+
$this->assertEquals($client->_id, $photo->hasImage->_id);
482+
483+
// inverse with custom ownerKey
484+
$photo = Photo::query()->create(['url' => 'https://graph.facebook.com/young.gerald/picture']);
485+
$client = Client::create(['cclient_id' => (string) (new ObjectId()), 'name' => 'Young Gerald']);
486+
$photo->hasImageWithCustomOwnerKey()->associate($client)->save();
487+
488+
$this->assertCount(1, $photo->hasImageWithCustomOwnerKey()->get());
489+
$this->assertInstanceOf(Client::class, $photo->hasImageWithCustomOwnerKey);
490+
$this->assertEquals($client->cclient_id, $photo->has_image_with_custom_owner_key_id);
491+
$this->assertEquals($client->_id, $photo->hasImageWithCustomOwnerKey->_id);
473492
}
474493

475494
public function testHasManyHas(): void

0 commit comments

Comments
 (0)