【问题标题】:How to make the php codeIgniter active record group?如何使php codeIgniter 活动记录组?
【发布时间】:2011-09-13 14:33:04
【问题描述】:

首先,我的模型中有一些东西:

    $this->db->where('status', $aStatus);     
    $this->db->limit($aLimit, $aOffset);   

    $this->db->or_like('title', $aString);    
    $this->db->or_like('description', $aString);    

    $this->db->select('id,description,title,fee');

    $query = $this->db->get($this->table_name); 

我得到了这个查询:

选择id, description,title, fee FROM (events) status = 0 AND title LIKE '%space%' 或 description LIKE '%space%' LIMIT 5

但我想让它生成这个查询

选择id, description,title, fee FROM (events) status = 0 AND (title LIKE '%space%' 或 description LIKE '%space%') LIMIT 5

我该如何修改才能做到这一点?谢谢你。

【问题讨论】:

    标签: php codeigniter activerecord


    【解决方案1】:

    想想你能做到这一点的唯一方法是按照以下方式做一些事情:

    $this->db->where('status', $aStatus);
    $this->db->where("(title LIKE '%space%' OR description LIKE '%space%')");
    $this->db->limit($aLimit, $aOffset);   
    
    $this->db->select('id,description,title,fee');
    
    $query = $this->db->get($this->table_name); 
    

    尚未对其进行测试,但它应该可以帮助您。除此之外,我认为不可能以任何其他方式包含分组/括号查询。

    【讨论】:

    • 如果我使用sql语句,需要转义这个词还是会自动转义?
    • 快速测试表明搜索词没有转义。
    【解决方案2】:

    分组 LIKE 子句(用括号括起来)

    $this->db->group_start();
    $this->db->like('name', $query_array['q']);
    $this->db->or_like('description', $query_array['q']);
    $this->db->group_end();
    

    会产生这样的圆形后备箱

     (
       `name` LIKE '%test%' ESCAPE '!' 
        OR  `description` LIKE '%test%' ESCAPE '!' 
     )
    

    【讨论】:

      【解决方案3】:

      我会做类似的事情

      $this->db->where('status', $aStatus);     
      $this->db->limit($aLimit, $aOffset);   
      
      $this->db->where(sprintf(
          '(title LIKE %s OR description LIKE %s)',
          $this->db->escape_like_str($aString),    
          $this->db->escape_like_str($aString)
      ), NULL);    
      
      $this->db->select('id,description,title,fee');
      
      $query = $this->db->get($this->table_name);
      

      【讨论】:

        猜你喜欢
        • 2012-06-14
        • 2014-04-05
        • 1970-01-01
        • 2014-02-22
        • 1970-01-01
        • 2014-12-15
        • 2013-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多