samedi 12 novembre 2016

Problems with mixed(whereRaw, whereNotIn) query bindings in eloquent scope

I have an issue with query bindings in eloquent at the moment.

I have the following query:

    $orderTypes = [6, 7, 8, 10];

    $accountOrders = DB::table('account_orders')
        ->selectRaw('account_orders.user_id, sum(orders.total) as account_total, count(orders.id) as account_count')
        ->leftJoin('orders', 'orders.id', '=', 'account_orders.order_id')
        ->whereNotIn('orders.order_type_id', $orderTypes)
        ->groupBy('account_orders.user_id');

    $query->selectRaw('users.*, sum(orders.total) + IFNULL(accountOrders.account_total, 0) total_orders_amount, count(orders.id) + IFNULL(accountOrders.account_count, 0) total_orders')
        ->leftJoin(DB::raw("({$accountOrders->toSql()}) as accountOrders"), 'accountOrders.user_id', '=', 'users.id')
        ->leftJoin('orders', 'orders.user_id', '=', 'users.id')
        ->where('users.user_role_id', 4)
        ->whereRaw('orders.payment_status = "fully paid" ')
        ->whereNotIn('orders.order_type_id', $orderTypes)
        ->groupBy('orders.user_id')
        ->limit(300)
        ->orderBy(DB::raw('sum(orders.total)'), 'DESC');

When I call get() on the model that owns the query scope I receive the following error:

QueryException in Connection.php line 624 : SQLSTATE[HY093]: Invalid parameter number (SQL: select users.*, sum(orders.total) + IFNULL(accountOrders.account_total, 0) total_orders_amount, count(orders.id) + IFNULL(accountOrders.account_count, 0) total_orders from `users` left join (select account_orders.user_id, sum(orders.total) as account_total, count(orders.id) as account_count from `account_orders` left join `orders` on `orders`.`id` = `account_orders`.`order_id` where `orders`.`order_type_id` not in (4, 6, 7, 8) group by `account_orders`.`user_id`) as accountOrders on `accountOrders`.`user_id` = `users`.`id` left join `orders` on `orders`.`user_id` = `users`.`id` where `users`.`user_role_id` = 10 and orders.payment_status = "fully paid" and `orders`.`order_type_id` not in (?, ?, ?, ?) group by `orders`.`user_id` order by sum(orders.total) desc limit 300)

If I copy and past the query to Sequel Pro and run it it works fine. But the problem is that the query builder is putting the bindings wrong, or perhaps I'm doing something wrong.

If you look at this parte of the query builder:

        ->where('users.user_role_id', 4)

Its clear that I'm querying user_role_id 4 but from the error the binding for that clause is then, which is the last element of the $orderTypes array. And also its not binding the ->whereNotIn('orders.order_type_id', $orderTypes) clause correctly.

When I dump the query bindings dd($query->getBindings(); I get:

array:5 [
  0 => 4
  1 => 6
  2 => 7
  3 => 8
  4 => 10
]

So the bindings are there, but the assignment is going wrong. I've never had problems like this in the past, the only thing I'm doing different now is mixing some raw stuff with eloquent.

I tried using named bindings but no luck.

Anyone with deep experience with bindings have any idea ?

Thanks.

I'm using Laravel Framework version 5.0.28 by the way.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire