2015-11-02 92 views
3

我已经在我的模型以下功能:导致下面的查询$this->the_model->getLocations()笨活动记录限制与加盟

SELECT * 
FROM "location" 
JOIN "process" ON "process"."process_id"="location"."process_id" 
JOIN "line" ON "line"."line_id"="process"."line_id" 
ORDER BY "location_id" asc LIMIT 0 

通知LIMIT 0,这确实

function getLocations($limit = null, $offset = null){ 
    $this->db->select('*'); 
    $this->db->from('location'); 
    $this->db->join('process', 'process.process_id=location.process_id'); 
    $this->db->join('line', 'line.line_id=process.line_id'); 
    $this->db->limit($limit, $offset); 
    $this->db->order_by('location_id', 'asc'); 
    return $this->db->get()->result(); 
} 

它然后使用执行当我执行$this->db->order_by('item_id', 'asc')->get('item', $limit, $offset)->result()时不会发生。没有LIMIT 0甚至限制和抵消是null。那么,如何解决这个问题呢?限制为空时,我已经添加了if条件。

+0

嗨,null并不意味着是= 0,你可以尝试做功能getLocations($ limit = 0,$ offset = 0) –

回答

1

0是一个值。这并不意味着0NULL。而NULL根本没有价值。

对于你的情况,

function getLocations($limit = 1, $offset = 0){ 
          ^  ^
    $this->db->select('*'); 
    $this->db->from('location'); 
    $this->db->join('process', 'process.process_id=location.process_id'); 
    $this->db->join('line', 'line.line_id=process.line_id'); 
    $this->db->limit($limit, $offset); 
    $this->db->order_by('location_id', 'asc'); 
    return $this->db->get()->result(); 
} 

limit至少1和偏移量是0为默认值。

0

感谢那些回答我的问题的人。

我解决了这个由代码调整到:

function getLocations($limit = null, $offset = null){ 
    $this->db->select('*'); 
    $this->db->join('process', 'process.process_id=location.process_id'); 
    $this->db->join('line', 'line.line_id=process.line_id'); 
    $this->db->order_by('location_id', 'asc'); 
    return $this->db->get('location', $limit, $offset)->result(); 
} 

这种方式存在,甚至生成的查询没有LIMIT 0$limitnull

非常感谢。

0

试试这个

function getLocations($limit , $offset){ 
    $query = $this->db->query(
     "SELECT * 
     FROM location 
     JOIN process ON process.process_id=location.process_id 
     JOIN line ON line.line_id=process.line_id 
     ORDER BY location_id asc LIMIT $limit, $offset"); 
    $result = $query->result_array(); 
    return $result; 
} 

不要设置$limit = NULL。这将解决问题

+0

谢谢,但有时我需要限制和抵消,有时不需要。 –

+0

然后使用另一个功能。如果您使用功能,它不会对您的项目性能产生任何不同 –