Replies: 1 comment 2 replies
-
Not sure this is an idea @crynobone; anyone updating their codebases to use named parameters while omitting the operator of a where() will have reasonably severe data integrity issues with no perceptible complaints from the framework. Your call though! |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Laravel Version
11.35.0
PHP Version
8.4.1
Database Driver & Version
Sqlite 3.43.2 2023-10-10 13:08:14 1b37c146ee9ebb7acd0160c0ab1fd11017a419fa8a3187386ed8cb32b709aapl (64-bit)
Description
When using named arguments in a where(), failing to supply an
operator
causes null results. Running a where() with no named arguments works as expected, and running a where() with all (of the first 3) arguments works. Leaving theoperator
argument out, when using named arguments, causes the value to be null.Simple examples:
I thought it might be something to do with the way
func_get_args()
works under the hood, - I've put together a couple of tests in the repo nominated in the steps to reproduce thatfunc_get_args
faithfully returns the operator even when not supplied with the function call. But then I sawfunc_num_args()
does the same, and there's a bit in the framework in Illuminate/Database/Query/Builder.php that might be getting tripped up by this behaviourIn the test class
QueriesUsingPositionalArgumentsNullWithoutOperatorTest
I've put in tests for the followingI'm unable to find previous issues that mention this behaviour - feel free to call me a dumdum and close if this has been covered / fixed / is generally not advised - thanks all. Might be a doco thing too?
I think it might have something to do with this line and the fact that func_num_args() acts differently based on named arguments:
In our case,
From https://www.php.net/manual/en/function.func-num-args.php
As of PHP 8.0.0, the func*() family of functions is intended to be mostly transparent with regard to named arguments, by treating the arguments as if they were all passed positionally, and missing arguments are replaced with their defaults. This function ignores the collection of unknown named variadic arguments. Unknown named arguments which are collected can only be accessed through the variadic parameter._
In the evaluation of $useDefault, when using only two named arguments to try to shortcut the operator - func_num_args() comes out at 3, causing the value to come through as null, and causing the query to be incorrect.
Hope this makes sense - cheers all and thanks so much for such an awesome framework 🔥 - PG
Steps To Reproduce
touch database/database.sqlite
composer install && php artisan migrate:fresh --seed
./vendor/bin/phpunit --filter=QueriesUsingPositionalArgumentsNullWithoutOperatorTest
2 tests will fail, both expecting to be able to omit the
operator
named argument in a where() queryBeta Was this translation helpful? Give feedback.
All reactions