CodeIgniter ActiveRecord one row at a time

纵然是瞬间 提交于 2019-12-12 03:44:54

问题


I'm running a query that selects many rows. When I loop over the results, I want to fetch a single row at a time (since it's too much memory to fetch them all at once).

Problem is, CodeIgniter's Active Record $query->row for all it's variations will fetch all the records from the database into the memory (and return a single row). I looked into the source of DB_result.php and found:

public function row_object($n = 0)
{
    $result = $this->result_object();
    if (count($result) == 0)
    {
        return $result;
    }

    if ($n != $this->current_row AND isset($result[$n]))
    {
        $this->current_row = $n;
    }

    return $result[$this->current_row];
}

and inside result_object():

...
while ($row = $this->_fetch_assoc())
{
    $this->result_array[] = $row;
}

This fetches ALL of the records from the database into the memory.

Is there a workaround that will allow me to fetch a single row without loading the entire resultset into the memory?


回答1:


SOLUTION

$query = $this->db->get();
while($r = $query->_fetch_object()){
    ...
}

probably the reason they left it public...



来源:https://stackoverflow.com/questions/14500110/codeigniter-activerecord-one-row-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!