Codeigniter Active Record Query无效

时间:2014-06-12 05:19:02

标签: php mysql codeigniter activerecord codeigniter-2

我有以下模型方法:

 public function product_count_where($where,$countwhat){
    $fields='count('.$countwhat.') as totalcount';
    try{                                    
    $this->db->select($fields);
    $this->db->from('product as p');        
    $this->db->join('shop_product_m2m as m2m','p.id = m2m.product','left'); 
    $this->db->join(
            'price_source_product_m2m as psp','p.id = psp.product','left');         
    $this->db->where($where);   
    $this->db->group_by('p.id');
        $query=$this->db->get();

    if(!$query) { 
    $errno =mysqli_errno($this->db->conn_id);
    $errmsg=mysqli_error($this->db->conn_id);
    throw new Exception('Error'.$errno." says:".$errmsg, 1);

    }else{return $query->row_array();}

    }catch(Exception $e){return $e->getMessage();}
}

我从一个视图中调用此函数(当然,我已经定义了一个帮助函数来从视图中调用模型函数) as:

<?php 
     $where='p.category = '.$item['category'];

     $where=$where.' AND ( m2m.price BETWEEN '. round($item['lowerprice']) .
     ' AND '.   round($item['upperprice']) .  
     ' OR psp.price BETWEEN '. round($item['lowerprice']) .
     ' AND '. round($item['upperprice']) .'  )';

     $countprodbybudget=product_count_where($where,'p.id');

     echo $countprodbybudget; 
?>

编辑&gt; $这 - &GT; DB-&GT; last_query();结果:

   SELECT count(p.id) as totalcount FROM (`product` as p) LEFT JOIN 
  `shop_product_m2m`  as m2m ON `p`.`id` = `m2m`.`product` LEFT JOIN
  `price_source_product_m2m` as psp ON `p`.`id` = `psp`.`product` 
   WHERE `p`.`category` = 1 AND ( m2m.price BETWEEN 10000 AND 15000 OR 
   psp.price BETWEEN 10000 AND 15000 ) GROUP BY `p`.`id`

现在令人惊讶的是,我的愚蠢顽固方法(或我是)对任何值都返回1,即使我排除了betwwen子句部分它仍然返回1

我浪费了2个小时调查此事。有人可以选择我在这里制作的错误/错误吗?

0 个答案:

没有答案
相关问题