数据库事务不能在控制器编码器中工作


Database transaction not working in controller codeigniter

我在控制器和所有模型调用多个模型做数据库查询。我做了这样的事情

public function InsertSale()
    {
            $this->db->trans_start(TRUE);
            // all logic part and models calling which do insert/update/delete
            $this->db->trans_complete();
}

上面的代码不工作,即使某些查询失败后,他们不回滚。

true置于$this->db->trans_start(true);将使事务进入测试模式,这意味着无论发生什么,查询都将回滚。

如果你想看看查询是否有效,你可以使用:

$this->db->trans_status();

根据结果返回true/false

所以你必须像这样跟随

$this->db->trans_start(); # Starting Transaction
$this->db->trans_strict(FALSE); 
$this->db->insert('table_name', $someDataArray); # Inserting data
# Updating data
$this->db->where('id', $id);
$this->db->update('table_name', $someDataArray); 
$this->db->trans_complete();

这是工作很好。