[5.0] Fix More Bugs With "push" In Eloquent Models#6778
Merged
Conversation
taylorotwell
added a commit
that referenced
this pull request
Dec 23, 2014
[5.0] Fix More Bugs With "push" In Eloquent Models
Member
|
Thanks for your work on this ❤️ |
Contributor
Author
|
Not a problem. Glad I could help! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For extra reference, bugs with the
push()method have been mentioned in issues #6139, #6066, and PR #6067. Issues still remain.Illuminate\Database\Eloquent\Model::push()has the following code snippet:As far as I can tell, a relationship can either be a Collection (with 0+ items), a Model, or NULL. There are a couple issues with the current
push()implementation:Relationship is a Collection
If the relationship is a Collection, the
is_array($models)check returns false, so the Collection is wrapped in an array. Therefore,$modelsis now an array containing one entry: the Collection. The code then iterates the array, and callspush()on the Collection, which is not what was intended. This causes the following error:Missing argument 1 for Illuminate\Support\Collection::push().Relationship is NULL
If a
hasOnerelationship is loaded but the related object doesn't exist, the relationship will be set to NULL. In this case, in thepush()method,is_array(NULL)returns false, and the NULL value is wrapped in an array. Therefore,$modelsis now an array containing one entry: NULL. The code then iterates the array, and callspush()on the NULL value, generating the following error:PHP Fatal error: Call to a member function push() on null.Relationship is a Model
This looks to work fine.
This Pull Request attempts to correctly convert
$modelsinto an array of Models, filtering out NULL values as well. Tests have been included as well, which seem to work, but I don't have much experience writing tests, so they should be scrutinized.