Codeigniter4: Problem with Database BaseBuilder binds

Created on 18 Sep 2018  路  3Comments  路  Source: codeigniter4/CodeIgniter4

Imagine this query.

$model = new UserModel();
$user = $model
        ->where('id', 2)
        ->orWhereIn('id', [3, 4])
        ->find(); 

where('id', 2) will generate a bind in BaseBuilder with key _id_.
orWhereIn('id', [3, 4]) will do the same.

We will end up with one bind, when we should have two.
The final query will end up like this

SELECT *
FROM `users`
WHERE `users`.`id` = (3,4)
OR `users`.`id` IN (3,4)

I see that you have already tried to deal with this issue by adding a setBind-method in BaseBuilder. But whereIn() does not use this method. I've tried replacing
$this->binds[$ok] = $where_in;
With
$this->setBind($ok, $where_in);
But this does not solve the problem. I still end up with a query like this

SELECT * 
FROM `users` 
WHERE `users`.`id` = 2 
OR `users`.`id` IN 2

Unfortunately I do not have more time to look at this. I hope someone else with take a look :-)

bug database

Most helpful comment

I found a fix anyway. Look at this.
4spacesdarktemplar____phpstormprojects_4spacesdarktemplar__-_____system_database_basebuilder_php__4spacesdarktemplar_

All 3 comments

I found a fix anyway. Look at this.
4spacesdarktemplar____phpstormprojects_4spacesdarktemplar__-_____system_database_basebuilder_php__4spacesdarktemplar_

having the same issue. Your solution fixed mine as well

Will have to dig a little deeper when I have a little more time, but that's not a perfect fix. It needs to be fixed in the setBind method, since that helps to ensure that name collisions don't happen, etc.

Was this page helpful?
0 / 5 - 0 ratings