Codeigniter Active Record 'where_or' grouping

Is it possible to group and_where / or_where / or_like ... commands together so as not to mix with other and / or other operators.

Something that will lead to

WHERE city = 'My City' AND state = 'My State' AND (name LIKE% name1% OR name LIKE% name2%)

+8
mysql activerecord codeigniter
source share
3 answers

Yes. $this->db->where() allows you to write articles manually. You can do it:

 $this->db->where('city', 'My City'); $this->db->where('state', 'My State'); $this->db->where('(name LIKE %name1% OR name LIKE %name2%)', null, false); 

Setting the third parameter to false prevents CodeIgniter from trying to add backticks to the sentence.

View Active Record Documentation . Section $ this-> db-> where (); explains four methods you can use to set WHERE clauses.

+11
source share

I know this is a little late, but I found this post, studying it and found a better solution than the one that Sean suggested.

 $name = 'Bob'; $this->db->where('city', 'My City'); $this->db->where('state', 'My State'); $this->db->group_start(); $this->db->like('name', $name); $this->db->or_like('name', $name); $this->db->group_end(); 
+14
source share

These two functions group_start() and group_end() implemented in CodeIgniter 3.0.3 . Here a little better implemented in accordance with the original question.

 $name1 = 'Bob'; $name2 = 'John'; $this->db->where('city', 'My City'); $this->db->where('state', 'My State'); $this->db->group_start(); $this->db->like('name', $name1); $this->db->or_like(('name', $name2); $this->db->group_end(); 
+1
source share

All Articles