php 如何为连续查询“重置”CodeIgniter 活动记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6246023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:46:38  来源:igfitidea点击:

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

phpcodeigniter

提问by zee

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:

我正在使用 CodeIgniter 并且有一个案例,其中两个表(项目和任务)需要一个接一个地更新一个值(活动列需要设置为“n”)。我正在使用的代码是:

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.

使用此代码,项目表会更新,但任务表不会。如果我注释掉,$this->db->update('projects', array('active' => 'n'));则任务表会更新。

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

我认为这与缓存有关,但我flush_cache在任务db->update调用之前尝试过,但没有任何效果。

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

有人可以解释如何使用 CodeIgniter 执行连续更新查询吗?

回答by Prasanna Shetye

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.

停止缓存后。

回答by Chris Adams

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:

如果你不执行 get() 或类似的 CI 并不总是清除缓存。最终代码如下所示:

$this->db->from('table');
$this->db->where('field', $field);
$count = $this->db->count_all_results();
$this->db->flush_cache();

回答by Femi

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

尝试$this->db->reset();在第一次update呼叫后呼叫。

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

编辑:嗯,尝试$this->db->_reset_write();刷新查询的所有痕迹。

回答by Antony

For version 3 of Codeigniter the correct way is:

对于 Codeigniter 的第 3 版,正确的方法是:

$this->db->reset_query()

$this->db->reset_query()

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

在这里找到:http: //www.codeigniter.com/userguide3/database/query_builder.html#resetting-query-builder

回答by Katsuke

In the second update call, do you need the url conditional? if so, after you call the first update that data is no longer available for the second one. You will need to set again:

在第二次更新调用中,您是否需要 url 条件?如果是这样,在您调用第一个更新后,第二个更新不再可用。您将需要再次设置:

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

// Alternatively
function update($url, $id)
{
    $where_bit = array(
        'url' => $url,
    );
    $this->db->update('projects', array('active' => 'n'), $where_bit);
    $where_bit['event_id'] = $id;
    $this->db->update('tasks', array('active' => 'n'), $where_bit);
}

CI active record update supports the where condition to be passed in as an array of key => value as the 3rd parameter (also as a string but id recommend using the array instead)

CI 活动记录更新支持将 where 条件作为 key => value 的数组作为第三个参数传入(也作为字符串,但 id 建议使用数组代替)

回答by mariofertc

try

尝试

$this->db->reconnect();

after your query.

在您查询之后。

Good day!

再会!