Skip to content

Commit e171a0d

Browse files
authored
Merge pull request #1663 from codeigniter4/binds
Fix bind issue that occurred when using whereIn or orWhereIn with a c…
2 parents c3cf7b1 + 929f215 commit e171a0d

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,8 @@ protected function _whereIn($key = null, $values = null, $not = false, $type = '
813813

814814
$not = ($not) ? ' NOT' : '';
815815

816-
$where_in = array_values($values);
817-
$this->binds[$ok] = $where_in;
816+
$where_in = array_values($values);
817+
$ok = $this->setBind($ok, $where_in);
818818

819819
$prefix = empty($this->QBWhere) ? $this->groupGetType('') : $this->groupGetType($type);
820820

@@ -1444,6 +1444,7 @@ public function get(int $limit = null, int $offset = 0, $returnSQL = false, $res
14441444
{
14451445
$this->limit($limit, $offset);
14461446
}
1447+
14471448
$result = $returnSQL ? $this->getCompiledSelect() : $this->db->query($this->compileSelect(), $this->binds);
14481449

14491450
if ($reset === true)
@@ -2958,7 +2959,7 @@ protected function setBind(string $key, $value = null)
29582959

29592960
while (array_key_exists($key . $count, $this->binds))
29602961
{
2961-
++ $count;
2962+
++$count;
29622963
}
29632964

29642965
$this->binds[$key . $count] = $value;

tests/system/Database/Live/SelectTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,42 @@ public function testSelectDistinctCanBeTurnedOff()
135135
}
136136

137137
//--------------------------------------------------------------------
138+
139+
/**
140+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1226
141+
*/
142+
public function testSelectWithMultipleWheresOnSameColumn()
143+
{
144+
$users = $this->db->table('user')
145+
->where('id', 1)
146+
->orWhereIn('id', [2, 3])
147+
->get()
148+
->getResultArray();
149+
150+
$this->assertCount(3, $users);
151+
152+
foreach ($users as $user)
153+
{
154+
$this->assertTrue(in_array($user['id'], [1, 2, 3]));
155+
}
156+
}
157+
158+
/**
159+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/1226
160+
*/
161+
public function testSelectWithMultipleWheresOnSameColumnAgain()
162+
{
163+
$users = $this->db->table('user')
164+
->whereIn('id', [1, 2])
165+
->orWhere('id', 3)
166+
->get()
167+
->getResultArray();
168+
169+
$this->assertCount(3, $users);
170+
171+
foreach ($users as $user)
172+
{
173+
$this->assertTrue(in_array($user['id'], [1, 2, 3]));
174+
}
175+
}
138176
}

0 commit comments

Comments
 (0)