简体   繁体   English

具有连接的CodeIgniter活动记录限制

[英]CodeIgniter active record limit with join

I have the following function in my model: 我的模型中有以下功能:

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();
}

It is then executed using: $this->the_model->getLocations() resulting in the following query: 然后使用: $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

Notice LIMIT 0 , this does not happen when I execute $this->db->order_by('item_id', 'asc')->get('item', $limit, $offset)->result() . 注意LIMIT 0 ,当我执行$this->db->order_by('item_id', 'asc')->get('item', $limit, $offset)->result()时,不会发生这种情况。 There is no LIMIT 0 even limit and offset is null . 没有LIMIT 0偶数限制,偏移量为null So, how to solve this? 那么,如何解决这个问题呢? I already add if condition when limit is null. 当limit为null时,我已添加if条件。

0 is a value. 0是一个值。 This does not mean 0 is NULL . 这并不意味着0NULL While NULL has no value at all. NULL根本没有任何价值。

For your case, 对于你的情况,

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();
}

Put the limit at least 1 and offset to be 0 for default values. limit至少设置为1 ,将默认值设置为0

Try this 试试这个

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;
}

Don't set $limit = NULL . 不要设置$limit = NULL this will fix the issue 这将解决问题

thanks for those who response to my question. 感谢那些回答我的问题的人。

I solved this by adjusting the code to: 我通过调整代码来解决这个问题:

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();
}

this way there is no LIMIT 0 in generated query even $limit is null . 这样生成的查询中没有LIMIT 0 ,即使$limitnull

thanks a lot. 非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM