[eluser]jvicab[/eluser]
I created two functions using a code I found on the web to overcome this issue, one for like statements and another for where, but it should be avoid using something like $this->db_query("SELECT * FROM table WHERE user = 1 AND (status = 1 OR status = 2)";
$this->db->get();
Here is the function I added to CI_DB_active_record class on DB_active_rec.php on system/database/: (another similar for where)
function like_brackets()
{
// add a opening bracket
reset($this->ar_like);
$key = key($this->ar_like);
$this->ar_like[$key] = '('.$this->ar_like[$key];
// add a closing bracket
end($this->ar_like);
$key = key($this->ar_like);
$this->ar_like[$key] .= ')';
// update the AR cache clauses as well
if ($this->ar_caching === TRUE)
$this->ar_cache_like[$key] = $this->ar_like[$key];
return $this;
}
it can be used on a model like:
$this-db->from('mytable');
$this->db->where('id', '10');
$this->db->or_like('feature', 'oceanview');
$this->db->like('feature', 'waterfront');
$this->db->like_brackets();
$this->db->get();
This would produce a query like:
SELECT * FROM `mytable` WHERE `id` = '10'AND (`feature` LIKE '%oceanview%' OR `feature` LIKE '%waterfront%')