You can cleanly omit the WHERE IN expression by falling the value parameter back to null when the array is empty.
$this->db->where_in('p.brand_id', $brands ?: null);
Some snippets of source code:
where_in() calls _where_in().
public function where_in($key = NULL, $values = NULL, $escape = NULL)
{
return $this->_where_in($key, $values, FALSE, 'AND ', $escape);
}
In _where_in() there is a conditional early return which avoids adding to the cache of WHERE conditions.
protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
{
if ($key === NULL OR $values === NULL)
{
return $this;
}
// ... more native code ...
These CI2 and CI1 repos contain a flaw in that the early return doesn't return $this. These functions serve to allow the omission of the WHERE IN statement, but that feature also breaks query builder chainability.
https://github.com/rhivent/master_codeigniter2/blob/master/system/database/DB_active_rec.php
protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ')
{
if ($key === NULL OR $values === NULL)
{
return;
}
// ... more native code ...
https://github.com/kidino/CodeIgniter-1/blob/develop/system/database/DB_query_builder.php
protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ', $escape = NULL)
{
if ($key === NULL OR $values === NULL)
{
return $this;
}
// ... more native code ...