liveday 发表于 2015-6-27 20:47:47

this->db->where..算不算bug

场景:在QueryFCL()方法中,需要根据URL参数生成不同的查询条件,其中在方法中包含getDestPortFromCountry()方法的调用,getDestPortFromCountry方法同时也会根据查询条件返回相应的ID,代码如下:
class Model_Test extends CI_Model
{
    public function __construct()
    {
      parent::__construct();
      $this->load->database();
    }

    public function QueryFCL($country='us'){
      $this->db->where_in('fobport',array('szx01', 'szx02', 'szx03', 'szx04')); //未执行db->get,db->get_where,db->query等方法前
      $destports = $this->getDestPortFromCountry($country);   //会将上面查询条件带入此方法,造成污染   
          /*
*不论何时,正确的查询应该是select id from destport where country='us'
*污染后的查询变成了 SELECT `id` FROM `destport` WHERE `fobport` IN('szx01', 'szx02', 'szx03', 'szx04') AND `country` = 'us'
* 由于方法执行了db->get_where操作,下面的操作将取不到上面的查询条件;
*/
if ($destports) {
            $this->db->where_in('destport', $destports);
      }
      $this->db->get('fclclientprice');
       return$this->db->last_query();      
    }

    public function getDestPortFromCountry($country)
    {
      //查询包含某国家下的全部目的港ID
      // $tsql = "select id from destport where country=?";
      //$query = $this->db->query($tsql, array($country));

      $this->db->select('id');
      $query = $this->db->get_where('destport', array('country' => $country));

      //die($this->db->last_query());

      if ($query) {
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                  $id[] = $row->id;
                }

                return $id;
            }
      }

      return false;

    }

}
醉了,不知道大家碰到过没。。。

页: [1]
查看完整版本: this->db->where..算不算bug