CodeIgniter ActiveRecord查询

时间:2011-02-03 21:49:51

标签: codeigniter

我有以下代码:

$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); 

生成以下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

有没有办法让它像这样生成:

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

因为我只想获得user_id = 1的记录而不是所有用户的记录。

1 个答案:

答案 0 :(得分:2)

这似乎是CI ActiveRecord中的错误/限制。您可以尝试Ignited Query,它能够处理嵌套的WHERE子句,并且可能以您喜欢的方式处理嵌套的WHERE/LIKE

Check out this thread also for more info.