-
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
Conversation
This reverts commit 54c9a85
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.
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.
LGTM. It fixes the issue reported in #2657.
Thank you @hans-thomas! |
@@ -221,7 +221,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null | |||
$this->newQuery(), | |||
$this, | |||
$id, | |||
$ownerKey, | |||
$ownerKey ?: $this->getKeyName(), |
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the $this->getKeyName()
fallback in HybridRelations::morphTo
and allowing MorphTo to be constructed with a null $ownerKey
would result in an exception here when null
is used in place of a string field name.
The relevant fix in #3011 is to fall back to $this->getForeignKeyName()
here, which respects a custom primary key name on parent.
|
||
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@hans-thomas: Was there a particular reason you used Photo::query()->create()
above but Client::create()
here? Best I could tell, they resolve to the same Builder::create()
method.
Client::create()
just ends up forwarding the call through Model::__call()
.
Hi, it fixes #2657.