Codeigniter Active Record'where_or'分组

kil*_*zzy 8 mysql activerecord codeigniter

是否有可能 and_where/or_where/or_like ...语句组合在一起,以免与其他和/,语句混合.

会导致的东西

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

小智 16

我知道这有点晚了,但我在调查时找到了这篇文章,并找到了比肖恩提出的更好的解决方案.

$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();
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案适用于Codeigniter 3+,但不适用于Codeigniter 2 (3认同)

bir*_*ric 12

是. $this->db->where()允许您手动编写子句.你可以这样做:

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

将第三个参数设置为false可防止CodeIgniter尝试向该子句添加反引号.

查看Active Record文档.标题为$ this-> db-> where()的部分; 解释了可用于设置WHERE子句的四种方法.