diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index 9dbba3fa8db0..10f2e64b535b 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -813,8 +813,8 @@ protected function _whereIn($key = null, $values = null, $not = false, $type = ' $not = ($not) ? ' NOT' : ''; - $where_in = array_values($values); - $this->binds[$ok] = $where_in; + $where_in = array_values($values); + $ok = $this->setBind($ok, $where_in); $prefix = empty($this->QBWhere) ? $this->groupGetType('') : $this->groupGetType($type); @@ -1444,6 +1444,7 @@ public function get(int $limit = null, int $offset = 0, $returnSQL = false, $res { $this->limit($limit, $offset); } + $result = $returnSQL ? $this->getCompiledSelect() : $this->db->query($this->compileSelect(), $this->binds); if ($reset === true) @@ -2958,7 +2959,7 @@ protected function setBind(string $key, $value = null) while (array_key_exists($key . $count, $this->binds)) { - ++ $count; + ++$count; } $this->binds[$key . $count] = $value; diff --git a/tests/system/Database/Live/SelectTest.php b/tests/system/Database/Live/SelectTest.php index d4c6331eb056..d6d45cb22e18 100644 --- a/tests/system/Database/Live/SelectTest.php +++ b/tests/system/Database/Live/SelectTest.php @@ -135,4 +135,42 @@ public function testSelectDistinctCanBeTurnedOff() } //-------------------------------------------------------------------- + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/1226 + */ + public function testSelectWithMultipleWheresOnSameColumn() + { + $users = $this->db->table('user') + ->where('id', 1) + ->orWhereIn('id', [2, 3]) + ->get() + ->getResultArray(); + + $this->assertCount(3, $users); + + foreach ($users as $user) + { + $this->assertTrue(in_array($user['id'], [1, 2, 3])); + } + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/1226 + */ + public function testSelectWithMultipleWheresOnSameColumnAgain() + { + $users = $this->db->table('user') + ->whereIn('id', [1, 2]) + ->orWhere('id', 3) + ->get() + ->getResultArray(); + + $this->assertCount(3, $users); + + foreach ($users as $user) + { + $this->assertTrue(in_array($user['id'], [1, 2, 3])); + } + } }