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

Conversation

Projects
None yet
3 participants
@iceheat

iceheat commented May 3, 2018

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

1 of 2 checks passed

continuous-integration/styleci/pr Issues have been identified with 2 files
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@sylouuu

This comment has been minimized.

Show comment
Hide comment
@sylouuu

sylouuu Jul 24, 2018

@iceheat is this PR fixes #22144 ?

Bests

sylouuu commented Jul 24, 2018

@iceheat is this PR fixes #22144 ?

Bests

@iceheat

This comment has been minimized.

Show comment
Hide comment
@iceheat

iceheat Jul 24, 2018

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.

iceheat commented Jul 24, 2018

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.

@iceheat

This comment has been minimized.

Show comment
Hide comment
@iceheat

iceheat Jul 30, 2018

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

iceheat commented Jul 30, 2018

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

@sylouuu

This comment has been minimized.

Show comment
Hide comment
@sylouuu

sylouuu Jul 30, 2018

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

sylouuu commented Jul 30, 2018

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

@iceheat

This comment has been minimized.

Show comment
Hide comment
@iceheat

iceheat Jul 30, 2018

@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.

iceheat commented Jul 30, 2018

@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