-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Attributes collision using BelongsToMany + cursor() #22144
Comments
Related: |
A dirty workaround: foreach ($john->groups()->cursor() as $group) {
$group->id = $group->attributesToArray()['group_id'];
dump($group->getAttributes());
} @taylorotwell Is it possible to consider a fix for a future version? |
@sylouuu My apologies for referencing this issue on PR that does not fix it. I blindly assumed that the issue you were complaining about was about I had a quick look at the code for BelongsToMany. I believe I have a grasp of what is causing the issue. I will try to dedicate some time over the evenings/weekend to write a few integration tests before introducing changes because this can easily introduce breaking changes. Cheers, |
@PGBI Can you resubmit your PR for Laravel 5.8? |
@staudenmeir Feel free to cherry-pick my commits and resubmit the PR. Given how my PR was rejected without the slightest start of explanation, I'm not willing to submit it again. |
This is exactly the same type of issue and has been already merged. So the cherry-picked PR must be going to be merged. |
We just ran into this issue and fortunately in our case, we didn't modify related models' attributes whilst using a cursor. But should one do so, it could very well update nothing or completely unrelated records. I don't understand why the original PR was rejected by @taylorotwell, because while it is technically a BC breaking change, the current behaviour is not only broken, it is outright dangerous and this should be considered a critical issue. I've set up a quick and dirty example of this in a PHPUnit test case; the test case actually tests the current, broken behaviour on purpose. See https://github.com/Werelds/laravel-broken-cursor-demo I'm going to create a fresh PR for this, because this has to be fixed. In its current implementation, one can not use cursors on at least |
Description:
When looping through a belongsToMany association using cursor(), the models aren't correctly hydrated: they get filled with values of the pivot table. If the pivot table have columns with the same name as columns in the related model table, pivot values are used to hydrate the related model.
Steps To Reproduce:
DB setup
users
table: id, name, timestampsgroups
table: id, name, timestampsgroup_user
table: id, name, timestamps, user_id, group_id, approved (boolean)Relationship defined as such:
Actual code showing bug:
Outputs:
I had actually proposed a PR to fix this (#22126), but it was rejected because: "breaking change". So instead of attempting to fix it myself, I figured I would post a bug report :)
The text was updated successfully, but these errors were encountered: