Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "reset" CodeIgniter active record for consecutive queries?

I'm using CodeIgniter and have a case where two tables (projects and tasks) need to be updated with a value right after one another (active column needs to be set to "n"). The code I am using is:

function update($url, $id)
{
    $this->db->where('url', $url);
    $this->db->update('projects', array('active' => 'n'));
    $this->db->where('eventid', $id);
    $this->db->update('tasks', array('active' => 'n'));
}

With this code, the projects table gets updated but the tasks table does not. If I comment out $this->db->update('projects', array('active' => 'n')); then the tasks table gets updated.

I reckon this has something to do with caching but I have tried flush_cache before the tasks db->update call but that didn't have any effect.

Can someone explain how consecutive update queries can be executed using CodeIgniter?

like image 231
zee Avatar asked Jun 05 '11 21:06

zee


4 Answers

Use

$this->db->start_cache();

Before starting query building and

$this->db->stop_cache();

After ending query building. Also, use

$this->db->flush_cache();

After stop cache.

like image 71
Prasanna Shetye Avatar answered Nov 18 '22 03:11

Prasanna Shetye


This works:

$this->db->flush_cache();

If you don't perform a get() or similar CI does not always clear the cache. The final code looks like this:

$this->db->from('table');
$this->db->where('field', $field);
$count = $this->db->count_all_results();
$this->db->flush_cache();
like image 22
Chris Adams Avatar answered Nov 18 '22 03:11

Chris Adams


Try calling $this->db->reset(); after the first update call.

EDIT: meh, try $this->db->_reset_write(); to flush all traces of the query.

like image 43
Femi Avatar answered Nov 18 '22 05:11

Femi


For version 3 of Codeigniter the correct way is:

$this->db->reset_query()

As found here: http://www.codeigniter.com/userguide3/database/query_builder.html#resetting-query-builder

And 2022 update for version 4:

$this->db->resetQuery();

As found here: https://codeigniter.com/user_guide/database/query_builder.html#resetting-query-builder

like image 20
Antony Avatar answered Nov 18 '22 04:11

Antony