Skip to content

Commit

Permalink
Merge pull request #1663 from codeigniter4/binds
Browse files Browse the repository at this point in the history
Fix bind issue that occurred when using whereIn or orWhereIn with a c…
  • Loading branch information
lonnieezell committed Jan 14, 2019
2 parents c3cf7b1 + 929f215 commit e171a0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
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]));
}
}
}

0 comments on commit e171a0d

Please sign in to comment.