-
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
PHPORM-127 Fix priority of embeds vs attributes #2584
base: 5.x
Are you sure you want to change the base?
Conversation
da4339c
to
81a0fc6
Compare
// First, we'll need to determine the foreign key and "other key" for the | ||
// relationship. Once we have determined the keys we'll make the query | ||
// instances as well as the relationship instances we need for this. | ||
$foreignKey = $foreignKey ?: $this->getForeignKey().'s'; | ||
|
||
$instance = new $related; | ||
|
||
if ($otherKey === $relation) { | ||
throw new \LogicException(sprintf('In %s::%s(), the key cannot be identical to the relation name "%s". The default key is "%s".', static::class, $relation, $relation, $instance->getForeignKey().'s')); |
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.
The solution was so easy, just don't name the foreign key the same as the relation; that is the default behavior if you don't pass any foreign keys to $this->belongsToMany(related , /* Don't pass anything else */) , and that is also the documented way to do it.
I propose to throw an exception in this case to avoid any error.
tests/Models/User.php
Outdated
|
||
public function otherGroups() | ||
{ | ||
return $this->belongsToMany(Group::class, 'groups', 'users', 'otherGroups', '_id', '_id', 'groups'); |
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.
I'm going to add a test for the exception.
171d8c2
to
68afe15
Compare
@@ -805,19 +805,23 @@ class User extends Model | |||
} | |||
} | |||
``` | |||
**Warning:** naming the foreign key same as the relation name will prevent the relation for being called on dynamic property, i.e. in the example above if you replaced `group_ids` with `groups` calling `$user->groups` will return the column instead of the relation. |
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.
I think "e.g." would be more appropriate because you're providing one possible example.
See: https://www.merriam-webster.com/grammar/ie-vs-eg-abbreviation-meaning-usage-difference
|
||
### EmbedsMany Relationship | ||
|
||
If you want to embed models, rather than referencing them, you can use the `embedsMany` relation. This relation is similar to the `hasMany` relation but embeds the models inside the parent object. | ||
|
||
**REMEMBER**: These relations return Eloquent collections, they don't return query builder objects! | ||
|
||
**Breaking changes** starting from v4.0 you need to define the return type of EmbedsOne and EmbedsMany relation for it to work |
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.
I see this PR targets 5.x. Should this documentation change be backported?
Also, is this heading better written as:
**Breaking Change:** starting from...
You're only noting a single breaking change, and it appears to be a label (vs. part of the sentence) so a colon could help differentiate. This applies below as well.
*/ | ||
private function hasEmbeddedRelation(string $key): bool | ||
{ | ||
if (! method_exists($this, $method = Str::camel($key))) { |
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.
I've never bothered to do this myself, but assumed CS for PHPLIB prohibited such inline assignments in the interest of readability. Is this common within the Laravel package?
Happy to defer to you.
@@ -272,6 +272,10 @@ public function belongsToMany( | |||
|
|||
$instance = new $related; | |||
|
|||
if ($relatedPivotKey === $relation) { | |||
throw new \LogicException(sprintf('In %s::%s(), the key cannot be identical to the relation name "%s". The default key is "%s".', static::class, $relation, $relation, $instance->getForeignKey().'s')); |
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.
You can optionally use argument numbering to remove the duplicate $relation
argument.
throw new \LogicException(sprintf('In %s::%s(), the key cannot be identical to the relation name "%s". The default key is "%s".', static::class, $relation, $relation, $instance->getForeignKey().'s')); | |
throw new \LogicException(sprintf('In %1$s::%2$s(), the key cannot be identical to the relation name "%2$s". The default key is "%3$s".', static::class, $relation, $instance->getForeignKey().'s')); |
if ( | ||
method_exists($this, $key) | ||
&& ! method_exists(self::class, $key) | ||
&& ! $this->hasAttributeGetMutator($key) |
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.
Not sure what this method does, but it seems the fix here is replace this with checking that the method returns either EmbedsOne or EmbedsMany (per your EmbedsRelations trait).
@@ -15,6 +15,6 @@ class Group extends Eloquent | |||
|
|||
public function users(): BelongsToMany | |||
{ | |||
return $this->belongsToMany(User::class, 'users', 'groups', 'users', '_id', '_id', 'users'); | |||
return $this->belongsToMany(User::class, 'users', 'groupIds', 'userIds', '_id', '_id', 'users'); |
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.
Is renaming to "userIds" the meaningful change here, so that it doesn't conflict with the relationship name? Is the renaming of "groupIds" just for consistency?
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.
Some optional suggestions and questions. If everything is easily resolved/answered, feel free to merge.
Fix #2577 (continuing)
Fix PHPORM-127
Moving the code from the
Model
class to theEmbedsRelations
trait.