dqsxsmi3704 2015-03-08 20:43
浏览 108

在CodeIgniter中,我们可以像这样使用数据库事务吗?

In CodeIgniter, Are we also allowed to use "Active Record Class" like this?

<?php
$data_balance = array(
 'balance' => $newbalance
);
$data_transaction = array(
 'amount' => $amount,
 'user' => $user
);

$this->db->trans_start();
 // This is an "Insert" query for a table
 $this->db->set('date', 'NOW()', FALSE);
 $this->db->insert('financial_transactions', $data_transaction);
 // This is an "Update" query for another table
 $this->db->where('user_id', $user_id);
 $this->db->update('financial_balances', $data_balance);
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE){
    // Do stuff if failed 
}
?>

Note that i use $this->db-> for both queries, so i don't know if the success result of first one is actually cleared to check the second one?

Is this going to work? can i trust that this will make either both queries to success or neither of them (i don't want one to success and one to fail)

  • 写回答

1条回答 默认 最新

  • duanfeiqu1989 2015-03-08 21:53
    关注

    From documentation:

    By default CodeIgniter runs all transactions in Strict Mode. When strict mode is enabled, if you are running multiple groups of transactions, if one group fails all groups will be rolled back. If strict mode is disabled, each group is treated independently, meaning a failure of one group will not affect any others.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制