代码交易 [英] Codeigniter Transactions

查看:211
本文介绍了代码交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Codeigniter交易:

  $ this-> db-> trans_start 
$ this-> db-> query('AN SQL QUERY ...');
$ this-> db-> trans_complete();

这工作正常,我的问题是,在trans_start和trans_complete内我调用其他函数这些函数处理数据库,因此它们包含插入和更新以及一些删除...例如:

  $ this-& - > 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



ex:

  $ this-> db-> trans_start(); 
$ result = $ this-> utils-> insert_function($ data);
if($ result === false){
$ this-> db-> trans_rollback();
}
$ this-> db-> trans_complete();有没有更好的方法?


更新1:



根据要求,我调用的外部函数示例:

  // insert_function contains 

$ rec = array(
'numero'=> $ numero,
'transaction_id'=> $ id ,
'debit'=> $ product_taxes ['amount_without_taxes'],
'date'=> $ data ['date_transaction'],

$ this-> addExerciceAccountingRecords($ rec);

和addExerciceAccountingRecords包含

函数addExerciceAccountingRecords($ records){
$ this-> db-> insert('transactions_exercices',$ records);
}


解决方案


< >

使用 transactions 表示支持数据库安全插入数据。因此,在Codeigniter中,我们将每个数据库相关函数写入 不在控制器中。在你的第二个代码(这是不工作),你有指向的模型。( utils )。所以简单我相信这将不工作。因为它不是一个插入数据与模型和控制器并行。事务应该在模型(我将在我的答案中写入模型)中编码。




b $ b




加载这些东西


  1. 数据库库

  2. 模型类

  3. 网址帮助

  4. 会话






假设



您已使用 $ data $ test 作为数组的代码。因此,我假设有两个数组用于插入和更新数据。






您的数据集

  $ data = array(
'title'=>'My title',
'name' =>'My Name',
'date'=>'我的日期'
);

$ id = 007;
$ test = array(
'title'=> $ title,
'name'=> $ name,
'date'=> $ date
);

您的代码

  $ this-> db-> trans_start(); #Starting Transaction 
$ 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(); #完成事务

/ *可选* /

if($ this-> db-> trans_status()=== FALSE){
#Something出错。
$ this-> db-> trans_rollback();
return FALSE;
}
else {
#一切都是完美的。
#将数据提交到数据库。
$ this-> db-> trans_commit();
return TRUE;
}






/ strong>


  1. 默认情况下,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 occurs codeigniter wont do a rollback.

What is the best way to deal with such issue ?

The only solution if 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

  1. Database Library
  2. Model Class
  3. URL helper
  4. 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

  1. 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.

这篇关于代码交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆