如何在调用同一模型两次时释放codeigniter结果

时间:2014-05-26 17:14:38

标签: php codeigniter

我有一个情况,我在codeigniter中编写了一个基础模型,所有模型都从它扩展,基础模型有一个函数

public function load_all_by_keys($array, $limit = 0, $offset = 0) {
  if ($limit) {
    $query = $this->database->get_where($this::DB_TABLE, $array, $limit, $offset);
  } else {
    $query = $this->database->get_where($this::DB_TABLE, $array);
  }
  $ret_val = array();
  $class = get_class($this);
  foreach ($query->result() as $row) {
    $model = new $class;
    $model->populate($row);
    $ret_val[$row->{$this::DB_TABLE_PK}] = $model;
  }
  return $ret_val;
}

在这个函数中,我可以说,例如,属于Abet类的学校

$abet = new School_Model();
// this query will get all schools with by column category whose value is Abet, thats fine
$abetSchools = $abet-?load_all_by_keys(array('category'=>'Abet'));
$primary = new School_Model();
// This second query fails with the error Fatal error: Call to a member function result() on a non-object
$primarySchools = $primary-?load_all_by_keys(array('category'=>'Primary'))

有人可以提供帮助

1 个答案:

答案 0 :(得分:0)

$this->db->flush_cache()

此功能删除Active Record缓存中的所有项目(参考http://ellislab.com/codeigniter/user-guide/database/active_record.html#caching