-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] morphTo relationship #2669
Changes from all commits
54c9a85
1a5fd7a
c728779
474f1b1
b44f591
3728b1f
6b35c88
a1d8927
0f00ab7
423a706
4c03ea8
73ecd41
6966abb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,11 @@ 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. | ||
$this->query->where($this->ownerKey, '=', $this->parent->{$this->foreignKey}); | ||
$this->query->where( | ||
$this->ownerKey, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the The relevant fix in #3011 is to fall back to |
||
'=', | ||
$this->getForeignKeyFrom($this->parent), | ||
); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -470,6 +470,25 @@ public function testMorph(): void | |
$relations = $photos[1]->getRelations(); | ||
$this->assertArrayHasKey('hasImage', $relations); | ||
$this->assertInstanceOf(Client::class, $photos[1]->hasImage); | ||
|
||
// inverse | ||
$photo = Photo::query()->create(['url' => 'https://graph.facebook.com/hans.thomas/picture']); | ||
$client = Client::create(['name' => 'Hans Thomas']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hans-thomas: Was there a particular reason you used
|
||
$photo->hasImage()->associate($client)->save(); | ||
|
||
$this->assertCount(1, $photo->hasImage()->get()); | ||
$this->assertInstanceOf(Client::class, $photo->hasImage); | ||
$this->assertEquals($client->_id, $photo->hasImage->_id); | ||
|
||
// inverse with custom ownerKey | ||
$photo = Photo::query()->create(['url' => 'https://graph.facebook.com/young.gerald/picture']); | ||
$client = Client::create(['cclient_id' => (string) (new ObjectId()), 'name' => 'Young Gerald']); | ||
$photo->hasImageWithCustomOwnerKey()->associate($client)->save(); | ||
|
||
$this->assertCount(1, $photo->hasImageWithCustomOwnerKey()->get()); | ||
$this->assertInstanceOf(Client::class, $photo->hasImageWithCustomOwnerKey); | ||
$this->assertEquals($client->cclient_id, $photo->has_image_with_custom_owner_key_id); | ||
$this->assertEquals($client->_id, $photo->hasImageWithCustomOwnerKey->_id); | ||
} | ||
|
||
public function testHasManyHas(): void | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2783 suggests that this broke MorphTo relationships when a model uses a custom primary key.
#3011 includes a proposed fix.