繁体   English   中英

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

[英]CodeIgniter active record limit with join

我的模型中有以下功能:

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->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 ,当我执行$this->db->order_by('item_id', 'asc')->get('item', $limit, $offset)->result()时,不会发生这种情况。 没有LIMIT 0偶数限制,偏移量为null 那么,如何解决这个问题呢? 当limit为null时,我已添加if条件。

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

试试这个

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 这将解决问题

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

我通过调整代码来解决这个问题:

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

非常感谢。

暂无
暂无

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

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