Codeigniter 交易 [英] Codeigniter Transactions
问题描述
我正在使用 Codeigniter 交易
$this->db->trans_start();$this->db->query('AN SQL QUERY...');$this->db->trans_complete();
这很好用,我遇到的问题是在 trans_start
和 trans_complete
内部我正在调用其他函数,这些函数处理数据库,因此它们包含插入和更新和一些删除...例如:
$this->db->trans_start();$this->utils->insert_function($data);$this->utils->update_function2($test);$this->db->trans_complete();
现在,如果这些函数被执行并且发生了一些错误,CodeIgniter 将不会进行回滚.
处理此类问题的最佳方法是什么?
我想到的唯一解决方案是从这些函数返回一个错误,并在这些函数中添加(trans_stat
和 trans_complete
)如果它返回一个错误测试一个 do $this->db->trans_rollback
例如:
$this->db->trans_start();$result = $this->utils->insert_function($data);if($result === false){$this->db->trans_rollback();}$this->db->trans_complete();
有没有更好的方法来做到这一点?
更新 1:
根据要求提供我正在调用的外部函数示例:
//insert_function 包含$rec = 数组('数字' =>$数字,'transaction_id' =>$id,'借记' =>$product_taxes['amount_without_taxes'],'日期' =>$data['date_transaction'],);$this->addExerciceAccountingRecords($rec);并 addExerciceAccountingRecords 包含函数 addExerciceAccountingRecords($records) {$this->db->insert('transactions_exercices', $records);}
<块引用>使用
transactions
表示支持数据库安全插入数据.因此,在 Codeigniter 中,我们在 Model 中而不是在 Controller 中编写了每个数据库相关的函数..在您的第二个代码(不起作用)中,您在那里指出了模型.(utils
).如此简单,我确定这行不通.因为它不是模型和控制器并行的插入数据.交易应该在模型中编码(我会在我的回答中写在模型中).
<小时>
也加载这些东西
- 数据库库
- 模型类
- 网址助手
- 会话
<小时>
假设
在您的代码中,您使用了 $data
和 $test
作为数组.所以我假设有两个数组用于插入和更新数据.
您的数据集
$data = 数组('标题' =>'我的头衔' ,'名称' =>'我的名字' ,'日期' =>'我的约会');$id = 007;$test = 数组('标题' =>$title,'名称' =>$姓名,'日期' =>$日期);
您的代码
$this->db->trans_start();# 开始交易$this->db->trans_strict(FALSE);# 见注 01.如果你愿意也可以删除$this->db->insert('table_name', $data);# 插入数据# 更新数据$this->db->where('id', $id);$this->db->update('table_name', $test);$this->db->trans_complete();# 完成交易/*可选的*/如果 ($this->db->trans_status() === FALSE) {# 出了些问题.$this->db->trans_rollback();返回假;}别的 {#一切都是完美的.# 提交数据到数据库.$this->db->trans_commit();返回真;}
<小时>
注意事项
- 默认情况下,Codeigniter 以严格模式运行所有事务.什么时候严格模式启用,如果您正在运行多组事务,如果一组事务失败,所有组都将回滚.如果严格模式被禁用,每个组都被处理独立,意味着一组失败不会影响任何其他.
I'm using Codeigniter transactions
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->trans_complete();
This works fine , the problem I have is that inside the trans_start
and trans_complete
I'm calling other functions and those functions deals with database so they contains inserts and update and some deletes ... ex:
$this->db->trans_start();
$this->utils->insert_function($data);
$this->utils->update_function2($test);
$this->db->trans_complete();
Now if those functions are executed and some errors occur CodeIgniter won't do a rollback.
What is the best way to deal with such issue?
The only solution, I have in mind, is to return an error from those functions and inside those function add (trans_stat
and trans_complete
) And if it returns an error test an do $this->db->trans_rollback
ex:
$this->db->trans_start();
$result = $this->utils->insert_function($data);
if($result === false){
$this->db->trans_rollback();
}
$this->db->trans_complete();
Is there a better way of doing this?
Update 1:
As requested a sample of the external function i'm calling :
// insert_function contains
$rec = array(
'numero' => $numero,
'transaction_id' => $id,
'debit' => $product_taxes['amount_without_taxes'],
'date' => $data['date_transaction'],
);
$this->addExerciceAccountingRecords($rec);
and addExerciceAccountingRecords contains
function addExerciceAccountingRecords($records) {
$this->db->insert('transactions_exercices', $records);
}
Using
transactions
means support databases to insert data safely. So in Codeigniter we write every database related functions in the Model not in Controller.. And in your second code(which is not working)you have pointed model on there.(utils
). So simple I'm sure this will not work. Because its not a insert data with model and Controller parallel. Transaction should be coded in the Model(I will write in Model in my answer).
Load this stuffs as well
- Database Library
- Model Class
- URL helper
- Session
Assumptions
In your code you have used $data
and $test
as array. So i assume there is two array for inserting and updating data.
Your data sets
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$id = 007;
$test = array(
'title' => $title,
'name' => $name,
'date' => $date
);
Your Code
$this->db->trans_start(); # Starting Transaction
$this->db->trans_strict(FALSE); # See Note 01. If you wish can remove as well
$this->db->insert('table_name', $data); # Inserting data
# Updating data
$this->db->where('id', $id);
$this->db->update('table_name', $test);
$this->db->trans_complete(); # Completing transaction
/*Optional*/
if ($this->db->trans_status() === FALSE) {
# Something went wrong.
$this->db->trans_rollback();
return FALSE;
}
else {
# Everything is Perfect.
# Committing data to the database.
$this->db->trans_commit();
return TRUE;
}
Notes
- 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.
这篇关于Codeigniter 交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!