Skip to content
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 BelongsToMany relationship bugs when using custom keys #2695

Merged

Conversation

hans-thomas
Copy link
Contributor

Hi there,
This PR fixes bugs in BlongsToMany relationship when custom keys defined.
I tested custom parentKey and relatedKey in #2667, but that was not enough. In this PR foreignPivotKey and relatedPivotKey keys have been tested and previous tests are improved.

Comment on lines 306 to 310
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey() . 's';
$foreignPivotKey = $foreignPivotKey ?: Str::plural($this->getForeignKey());

$instance = new $related();

$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey() . 's';
$relatedPivotKey = $relatedPivotKey ?: Str::plural($instance->getForeignKey());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pluralizing foreignPivotKey and relatedPivotKey keys in the same way in MorphToMany

$relatedPivotKey = $relatedPivotKey ?: Str::plural($instance->getForeignKey());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid it's a breaking change. This (incorrect) naming convention is here since the beginning and it will break silently for applications that don't use schema validation (what I think is the majority). By changing this rule, they will start having 2 lists (childs and children) and that will be hard to fix.

Let's add a deprecation notice instead, if the pluralization is different.

Please revert in this PR and open an new one with this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. this can be done in 5.x version. devs need to override their foreignPivotKey and relatedPivotKey keys for just existing relationships to upgrade to 5.x version.

@hans-thomas hans-thomas marked this pull request as ready for review December 12, 2023 11:26
Comment on lines 306 to 310
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey() . 's';
$foreignPivotKey = $foreignPivotKey ?: Str::plural($this->getForeignKey());

$instance = new $related();

$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey() . 's';
$relatedPivotKey = $relatedPivotKey ?: Str::plural($instance->getForeignKey());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid it's a breaking change. This (incorrect) naming convention is here since the beginning and it will break silently for applications that don't use schema validation (what I think is the majority). By changing this rule, they will start having 2 lists (childs and children) and that will be hard to fix.

Let's add a deprecation notice instead, if the pluralization is different.

Please revert in this PR and open an new one with this change.

@GromNaN GromNaN merged commit 99e1028 into mongodb:4.1 Dec 13, 2023
15 checks passed
@GromNaN
Copy link
Member

GromNaN commented Dec 13, 2023

Thank you @hans-thomas for all your work on relationships.

@hans-thomas hans-thomas deleted the belongs-to-many-relation-with-custom-keys branch December 13, 2023 11:40
@hans-thomas
Copy link
Contributor Author

@GromNaN You're welcome buddy. Thank you for sharing your knowledge with me❤️ I learned a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants