Fixed incomplete return type signature on insertGetId method. Updated Model class and added test. #54410
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.
Explanation
There can be unexpected consequences when calling methods like
$model->insertAndSetId()
or$table->insertGetId()
are called and the returned value isfalse
, which is not accounted for in the signatures of those methods. This can happen because both functions return the result ofIlluminate\Database\Query\Processors\Processor\processInsertGetId()
(also needing a PHPDoc update), which in turn returns the result ofPDO\lastInsertId()
, which correctly indicates it could return a value offalse
.Changes
Model\insertAndSetId()
method to not set the attribute if the return value is false. There must be a better way to handle this, but I wanted to be consistent with other database functions and not throw an error.Model
class to handle the case in which the attribute is not set. I'm not sure if I put my test in the right place. Improvements are probably needed anyway.Note
There are many other places where a
false
return value fromlastInsertId()
could be problematic. Further review and tests are encouraged.Beware
Model
class seems like a be a breaking change to me, so based on the guidelines, I'm hoping I selected the right branch. Again, sorry if I'm getting this wrong.