2016-07-25 33 views
0

我想用一个关键字,但只在business_type是制造商的行进行搜索,但它不工作,它让所有的行。这是在我的模型的方法:如何使用像,or_like和get_where一起笨3

public function search($keyword) { 
    $this->db->like('category',$keyword); 
    $this->db->or_like('keyword',$keyword); 
    $this->db->or_like('products_deals_with',$keyword); 
    $this->db->or_like('buisness_name',$keyword); 
    $params['conditions'] = array(
     'business_type' => 'manufacturer' 
    ); 
    $query = $this->db->get_where('business_listings', $params['conditions']); 
    return $query->result_array(); 
} 

生成的查询是:

SELECT * FROM `business_listings` 
WHERE `category` LIKE '%%' ESCAPE '!' 
OR `keyword` LIKE '%%' ESCAPE '!' 
OR `products_deals_with`LIKE '%%' ESCAPE '!' 
OR `buisness_name` LIKE '%%' ESCAPE '!' 
AND `business_type` = 'manufacturer' 
+0

也许只是使用'$这个 - > DB-> where'和'$这个 - > DB-> GET'如果他们互动糟糕呢?你也可以提供生成的SQL查询吗? – CollinD

+0

SELECT * FROM'business_listing'其中'category' LIKE '%%' ESCAPE '!'或'关键字'LIKE'%%'ESCAPE'!'或'products_deals_with' LIKE'%%'ESCAPE'!'或'buisness_name' LIKE'%%'ESCAPE'!'和'business_type' =“制造商” –

+0

来吧,使用此信息更新,而不是作为发布评论你的问题。 – CollinD

回答

3

我已经找到了解决办法。我必须使用$ this-> db-> group_start();和$ this-> db-> group_end();

public function search($keyword) { 

    $this->db->select('*'); 
    $this->db->where("business_type = 'manufacturer'"); 
    $this->db->group_start(); 
    $this->db->like('category',$keyword); 
    $this->db->or_like('keyword',$keyword); 
    $this->db->or_like('products_deals_with',$keyword); 
    $this->db->or_like('buisness_name',$keyword); 
    $this->db->group_end(); 
    $query = $this->db->get('business_listings'); 
    // echo $this->db->last_query(); 
    return $query->result_array(); 

} 

生成的查询:

SELECT * FROM `business_listings` 
WHERE `business_type` = 'manufacturer' 
AND (
`category` LIKE '%%' ESCAPE '!' 
OR `keyword` LIKE '%%' ESCAPE '!' 
OR `products_deals_with` LIKE '%%' ESCAPE '!' 
OR `buisness_name` LIKE '%%' ESCAPE '!')