簡體   English   中英

在CodeIgniter中,我們可以這樣使用數據庫事務嗎?

[英]In CodeIgniter, Can we use Database Transactions like this?

在CodeIgniter中,是否也可以使用這樣的“活動記錄類”?

<?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 
}
?>

請注意,我對兩個查詢都使用$ this-> db->,所以我不知道第一個查詢的成功結果是否被清除以檢查第二個查詢?

這行得通嗎? 我可以相信這將使查詢要么成功要么都不成功(我不希望一個查詢成功而一個查詢失敗)

文檔

默認情況下,CodeIgniter在嚴格模式下運行所有​​事務。 啟用嚴格模式后,如果您正在運行多個事務組,則如果一個組失敗,則所有組都將回滾。 如果禁用嚴格模式,則每個組將被獨立對待,這意味着一個組的故障不會影響其他任何組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM