Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:5.6from
AmmarRahman:5.6
May 3, 2018
Merged

[5.6] Fix for HasManyThrough returning incorrect results with chunk()…#24096
taylorotwell merged 1 commit into
laravel:5.6from
AmmarRahman:5.6

Conversation

@AmmarRahman
Copy link
Copy Markdown

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
Copy Markdown

sylouuu commented Jul 24, 2018

@iceheat is this PR fixes #22144 ?

Bests

@AmmarRahman
Copy link
Copy Markdown
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
Copy Markdown
Author

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

@sylouuu
Copy link
Copy Markdown

sylouuu commented Jul 30, 2018

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

@AmmarRahman
Copy link
Copy Markdown
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