Skip to content
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

Datatables is broken with complex query after updating because of PR 1730 #1983

Closed
royduin opened this issue Feb 8, 2019 · 4 comments
Closed

Comments

@royduin
Copy link
Contributor

royduin commented Feb 8, 2019

Just updated Datatables because I updated Laravel and now it's broken:

Exception Message:

SQLSTATE[HY093]: Invalid parameter number (SQL: select count(*) as aggregate from (select '1' as `row_count` from (select `customers`.*, (select COUNT(`id`) from `orders` where `orders`.`customer_id` = `customers`.`id` and `ordered_at` between Alkmaar and 4) as `order_count_2017` from `customers`) as t where `city` = ? and `order_count_2017` = ?) count_row_table)

After some digging this is causing it: #1730. It removes my select bindings. As you can see in the error my "where bindings" are used for the "select bindings".

The query I'm using:

$subQuery = DB::table('orders')
    ->selectRaw('COUNT(`id`)')
    ->whereColumn('orders.customer_id', 'customers.id')
    ->whereBetween('ordered_at', ['2017-01-01 00:00:00', '2017-12-31 23:59:59']);

$query = DB::table('customers')
    ->select('customers.*')
    ->selectSub($subQuery, 'order_count_2017');

$wrapperQuery = DB::table(DB::raw("({$query->toSql()}) as t"))
    ->mergeBindings($query)
    ->where('city', 'Alkmaar')
    ->where('order_count_2017', 4);

All of this is needed so I can do a "where" on my subselect. This is needed because "having" isn't supported. Can we revert #1730 and find another solution for that? Just removing all select bindings isn't the solution @fschalkwijk and @yajra...

@yajra
Copy link
Owner

yajra commented Feb 12, 2019

Thanks for reporting, will try to dig this further. Reverting the PR might break the other one that was fixed #1600.

@yajra
Copy link
Owner

yajra commented Feb 12, 2019

BTW, please do not hesitate to submit a PR if you can. Thanks!

@royduin
Copy link
Contributor Author

royduin commented Feb 12, 2019

Do you think we can somehow detect if it should be removed or give it a parameter?

@royduin
Copy link
Contributor Author

royduin commented Feb 12, 2019

Tried to auto detect it, for now just a flag which can be set.

@github-actions github-actions bot locked and limited conversation to collaborators Oct 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants