avenirer / CodeIgniter-MY_Model

Base model (MY_Model) for the Codeigniter framework.
324 stars 203 forks source link

fields() leaving uninitialized values #286

Open esteban-turnocheck opened 5 years ago

esteban-turnocheck commented 5 years ago

Hi all! I've detected what could be a bug. When you use the fields() function, it sets the _select property of the MY_Model class (declared in line 182). Then, when it fetches data and goes to _prep_after_read, in line 391 it sets _select back to the default "*". Problem is that _prep_after_read is only called when the query brings back 1 or more rows. Not otherwise. Meaning that _select remains with the previous value. So if you do two querys, one after the other, and the first one doesn't return any row, the next one will try to fetch the same fields as the first, potentially causing errors (if query one and query two, for instance, use different tables). Here's how lines from 983, on function get(), reads.

if ($query->num_rows() == 1)
  {
    $row = $query->row_array();
    $row = $this->trigger('after_get', $row);
    $row =  $this->_prep_after_read(array($row),FALSE);
    $row = $row[0];
    $this->_write_to_cache($row);
    return $row;
  }
else
  {
    return FALSE;
}

And this is the patch I've made to solve this issue.

if ($query->num_rows() == 1)
  {
    $row = $query->row_array();
    $row = $this->trigger('after_get', $row);
    $row =  $this->_prep_after_read(array($row),FALSE);
    $row = $row[0];
    $this->_write_to_cache($row);
    return $row;
  }
else
  {
    if(isset($this->_select))
      {
        $this->_select = '*';
      }
    return FALSE;
}

And the same goes for function get()