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

Fix bind issue that occurred when using whereIn or orWhereIn with a c… #1663

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions tests/system/Database/Live/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}
}