CodeIgniter ActiveRecord Request

I have the following code:

$this->db->from('addresses'); $this->db->where('user_id', $this->session->userdata('user.id')); $this->db->like('country', $pSearch); $this->db->or_like('state', $pSearch); $this->db->or_like('city', $pSearch); $this->db->or_like('zip', $pSearch); 

which generates the following SQL

 SELECT * FROM (`addresses`) WHERE `user_id` = '1' AND `country` LIKE '%d%' OR `state` LIKE '%d%' OR `city` LIKE '%d%' OR `zip` LIKE '%d%' ORDER BY `country` asc LIMIT 15 

Is there a way to get it to generate it like this:

 SELECT * FROM (`addresses`) WHERE `user_id` = '1' AND (`country` LIKE '%d%' OR `state` LIKE '%d%' OR `city` LIKE '%d%' OR `zip` LIKE '%d%') ORDER BY `country` asc LIMIT 15 

As I want to get only entries for user_id = 1, not for all users.

+4
source share
1 answer

This is similar to an error / limitation in CI ActiveRecord. You can try Ignited Query , which has the ability to process nested WHERE clauses and can probably handle the nested WHERE/LIKE way you like,

See also this topic.

+2
source

Source: https://habr.com/ru/post/1338325/


All Articles