-
Notifications
You must be signed in to change notification settings - Fork 108
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
Count broken if select columns contain parameters #190
Comments
Not sure if its the same problem, but I had some problems with duplicated binding values, so I solved simply changing the last 3 lines of the function: $bindings = $myQuery->getBindings();
$this->$count = DB::connection($connection)
->table(DB::raw('('.$myQuery->toSql().') AS count_row_table'))
->setBindings($bindings)->count(); |
Actually this clone thing is messing up some things... I have a complex query with union and it's throwing invalid parameters number exception when I updated the packages from Laravel... This solved: $myQuery = $query;
if( !preg_match( '/UNION/i', $myQuery->toSql() ) ){
$myQuery = clone $query; I'm only cloning if its necessary... I don't think the default clone will work just fine, It was messing up the bindings. |
Please, check this latest solution, @patrickcarlohickman ... I think the problem is that the parent object, $query (from Eloquent\Builder) is being cloned, but not the bindings array from this query... It explains why the 'true' value appears two times. |
Actually, I think the clone won't work correctly because the Builder object is not overriding the __clone, and, since it's a complex object, the PHP superficial clone won't work correctly, leading to unexpected results like these... |
Given the following query and parameter bindings:
This generates the following count query:
The issue is that the columns for the original select had a parameter, but the binding for that parameter was not removed when the select was replaced for the count. Therefore, there are still two values being bound to the new count statement which only has one parameter now. This causes the count statement to fail.
From the PHP docs here: "You cannot bind more values than specified; if more keys exist in input_parameters than in the SQL specified in the PDO::prepare(), then the statement will fail and an error is emitted."
Current solution working for me so far (I can make a pull request, if this looks reasonable):
The text was updated successfully, but these errors were encountered: