When I add new category to my database it insert fine. But the Codeigniter Database Query Cache does not update when add or remove
Question: Once I add a new category or delete a category how to make sure the codeigniter database cache query gets updated correct
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'community',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => TRUE,
'cachedir' => APPPATH . 'cache/database/',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Forum Model
public function getcategories() {
$this->db->cache_on(); // Cache On
$query = $this->db->select('*')
->order_by('name', 'asc')
->from('forum')
->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return array();
}
}
public function add() {
$this->db->trans_start();
$this->db->trans_strict(FALSE);
$insert = array(
'name' => $this->input->post('title'),
'type' => $this->input->post('type'),
'pid' => $this->input->post('pid')
);
$this->db->insert($this->db->dbprefix . 'forum', $insert);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
return FALSE;
} else {
$this->db->trans_commit();
return TRUE;
}
}
public function categories_for_deletion($fid) {
$this->db->where('fid', $fid);
$this->db->or_where('pid', $fid);
$query = $this->db->delete('forum');
$this->db->cache_delete('admin', 'category');
if ($query) {
return TRUE;
} else {
return FALSE;
}
}
Controller
<?php
class Category extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('forum');
$this->load->model('admin/forum/forum_model');
}
public function add() {
$this->form_validation->set_rules('title', 'title', 'required');
if ($this->form_validation->run() == TRUE) {
$this->forum_model->add();
redirect('admin/category');
}
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$data['categories'] = $this->forum->generate_select();
$this->load->view('template/forums/category_add_view', $data);
}
public function edit($fid) {
$this->form_validation->set_rules('title', 'title', 'required');
if ($this->form_validation->run() == TRUE) {
}
$data['header'] = Modules::run('admin/common/header/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('template/forums/category_edit_view', $data);
}
public function delete($fid) {
$delete = $this->forum_model->categories_for_deletion($fid);
if ($delete == TRUE) {
redirect('admin/category');
}
$this->index();
}
public function index() {
$data['categories'] = '';
if ($this->forum_model->getcategories()) {
$data['categories'] = $this->forum->set_categories_list($this->forum_model->getcategories());
}
$data['header'] = Modules::run('admin/common/header/index');
$data['navbar'] = Modules::run('admin/common/navbar/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$this->load->view('template/forums/category_view', $data);
}
}