【问题标题】:Codeigniter database reset cache query after one deletionCodeigniter 数据库在一次删除后重置缓存查询
【发布时间】:2017-03-13 10:17:11
【问题描述】:

我有一个函数可以让我删除表格上的一行。但问题是,为了数据库完整性,我必须删除另一个表上与我要删除的记录相关的另一行。这是我的功能:

public function delete($tab)
    {
        $this->db->where('id', $tab['id']);
        $this->db->delete('agent', $tab); 
       // Here the query is "DELETE from agent WHERE id = $tab['id']"

        $this->db->where('id_agent', $tab['id']);
        $this->db->delete('agent_vendeuse', $tab);
// Here the query gives "Delete from agent_vendeuse WHERE id_agent= $tab['id'] AND id=$tab['id'];" Which is where the error comes from
    }

我的函数出现错误数据库错误:

DELETE FROM `agent_vendeuse` WHERE `id_agent` = '2' AND `id` = '2'

这意味着在我影响 where 子句的新索引 'id_agent' 后,前一个 'id' 仍在缓存中。

$tab 变量来自我的表单的 $_POST。我只是在函数中更改了它的名称。

我想我必须在第一次删除后清理缓存但是如何写呢?

【问题讨论】:

  • 你能发布你得到的实际错误信息吗?
  • avenir.ro/… -- 有点偏离轨道,但这可能有助于在 CI 中建立关系
  • @Pacio 我在第二个查询中遇到了 codeigniter 错误,因为我的 where 子句 id 和 id_agent 有两个条件,它们位于不同的表上
  • 好的,现在明白了。但是你传递给 delete() 的 $tab 变量是什么?第二个参数应该是 where 子句,但您已经使用 where() 调用完成了。

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

delete 函数的第二个参数是 where 子句,所以我猜可能发生的情况是 $this->db->where('id', $tab['id']); 行在第一次 delete() 调用时被忽略,并且由于某种原因在第二次 delete( ) 调用您在参数中定义的“where 子句”,因为 $tab 被忽略,而使用两个 where() 调用。

只需从传递给 delete() 的参数中删除 $tab 就可以了:

public function delete($tab)
{
    $this->db->where('id', $tab['id']);
    $this->db->delete('agent'); 

    $this->db->where('id_agent', $tab['id']);
    $this->db->delete('agent_vendeuse');
}

【讨论】:

  • agent 和 agent_vendeuse 是两个不同的表,agent_vendeuse 在其字段中没有 id。这就是第二次删除会引发数据库错误的原因。 Codeigniter 在其查询中保留了这两个位置
  • 我知道它们是不同的表,并且它保留了两个 where(),但我很确定这不是正常行为,它发生是因为第一个 delete() 有不适当的参数。你试过这个代码吗?您也可以尝试仅将所有四行替换为 $this->db->delete('agent', array('id'=>$tab['id']);$this->db->delete('agent_vendeuse', array('id'=>$tab['id']);
  • 是的,它有效!谢谢。我没用好codeigniter的delete功能!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 2011-08-17
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多