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

[5.6] Fix for HasManyThrough returning incorrect results with chunk()… #24096

Merged
merged 1 commit into from
May 3, 2018
Merged

Conversation

AmmarRahman
Copy link

Laravel Version: 5.6.17
PHP Version: 7.1.3 (MacOS 10.13.4)

Description:

Summary:
This is pull request addresses the issues reported on #21774. Using the chunk() method on a HasManyThrough relation object returns incorrect results. This is due to to the fact that Eloquent QueryBuilder attempts to hydrate the the related model with intermediate model attributes.

When querying models through a hasManyThrough relationship and using chunk() on the query, all the retrieved models have the same id (and probably a non-existent id for the model), even though their other attributes have the correct values. It creates a problem when eager loading other relations, since they all have the same id.
The aforementioned issue is a resulted because the intermediate model id is inserted on the query instead of the related model.

With the current codebase get() method has fixes the issue by getting an instance of the builder with the correct table and columns selected to overcome the issue. This can be demonstrated by the different model structure that would be retrieved when running
$country->posts()->get() vs $country->posts()->getQuery()->get()

The same issue is manifested when using each() method. This is because each() uses the underlying chunk() method during its runtime.

Proposed fix

chunk() and each() methods were added to HasManyThrough class to correct the selection of columns before they operate. This would contain the behaviour change to HasManyThrough without affecting any other behaviours. Hence, there are no breaking changes introduced.

Tests

Added two extra tests to DatabaseEloquentHasManyThroughIntegrationTest to test chunk() and each(), where the models are checked to have the correct columns defined.

@taylorotwell taylorotwell merged commit 48f07be into laravel:5.6 May 3, 2018
@sylouuu
Copy link

sylouuu commented Jul 24, 2018

@iceheat is this PR fixes #22144 ?

Bests

@AmmarRahman
Copy link
Author

No, I have not tackled the cursor() issue. I will attempt to do a pull request to fix it without breaking anything within the next couple of days.

@AmmarRahman
Copy link
Author

@sylouuu A fix for cursor has been merged into 5.6 branch

@sylouuu
Copy link

sylouuu commented Jul 30, 2018

@iceheat Awesome, thanks! Is the merged PR you talk about also works for BelongsToMany relationships?

@AmmarRahman
Copy link
Author

@sylouuu The fix was for HasManyThrough. I have no knowledge of BelongsToMany causing issue. It would be helpful if you submit a test highlighting the issue.

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.

3 participants