36

我正在使用 Codeigniter 事务

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

这很好用,我遇到的问题是在内部trans_starttrans_complete我正在调用其他函数,这些函数处理数据库,因此它们包含插入和更新以及一些删除......例如:

$this->db->trans_start();
 $this->utils->insert_function($data);
 $this->utils->update_function2($test);
$this->db->trans_complete();

现在,如果执行了这些函数并且发生了一些错误,CodeIgniter 将不会进行回滚。

处理此类问题的最佳方法是什么?

我想到的唯一解决方案是从这些函数中返回一个错误,并在这些函数内部添加(trans_stattrans_complete)如果它返回一个错误,则进行测试$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 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);
    }
4

6 回答 6

45

使用transactions方法支持数据库安全插入数据。所以在 Codeigniter 中,我们在模型中而不是在控制器中编写每个与数据库相关的函数。. 在您的第二个代码(不起作用)中,您已经在那里指出了模型。(utils)。如此简单,我相信这行不通。因为它不是模型和控制器并行的插入数据。交易应该在模型中编码(我将在我的回答中写在模型中)。


也加载这些东西

  1. 数据库库
  2. 模型类
  3. 网址助手
  4. 会议

假设

在您的代码中,您使用了$data$test作为数组。所以我假设有两个数组用于插入和更新数据。


您的数据集

$data = array(
   'title' => 'My title' ,
   'name' => 'My Name' ,
   'date' => 'My date'
);

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

你的代码

$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;
}

笔记

  1. 默认情况下,Codeigniter 以严格模式运行所有事务。启用严格模式后,如果 您正在运行多组事务,如果一组失败,所有组都将回滚。如果禁用严格模式,则每个组都被独立处理这意味着一个组的失败不会影响其他任何组
于 2016-01-09T15:51:41.320 回答
8

我尝试的更多的是一种技巧,但它对我有用。

$this->db->trans_begin();
  $rst1=  $this->utils->insert_function($data);
  $rst2 =  $this->utils->update_function2($test);
if($this->db->trans_status() === FALSE || !isset($rst1) || !isset($rst2)){
   $this->db->trans_rollback();
}else{
   $this->db->trans_commit();
}
于 2016-08-08T23:22:39.410 回答
4

我怀疑问题与 CodeIgniter 处理对象的方式有关。

如果您访问“创建库”部分下的 CI 文档,网址为:
http
://ellislab.com/codeigniter/user-guide/general/creating_libraries.html 并查看与以下相关的部分:

$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');

在您的主控制器中,您已经使用自动加载或显式加载类加载/实例化了数据库类。

然后继续打开事务,然后通过 utils 库访问数据库函数。

但是,一旦您$this-db在库中使用,您实际上是在访问数据库实例的另一个副本,而不是与您的事务关联的副本。

要访问同一个实例,您需要使用 get_instance() 函数。

我认为这应该可以解决您的问题。您将功能分成各种模块的原始编码风格非常出色。您只需要了解这个额外的细节。

请尝试并确认回滚是否按预期进行。

代码的核心由以下控制器组成:

$this->db->trans_start();
$this->User_profile_m->create_new_user_profile();
$this->User_profile_m->create_new_user();
$this->db->trans_complete(); 

user_profile_m以及一个处理数据持久性的简单模型:

function create_new_user()
{
    $data['user_name_usr'] = $this->input->post('user_name');
    $data['create_date_usr'] = NULL;

    $this->db->insert('user_usr', $data);  
}

function create_new_user_profile()
{
    $data['user_name_pro'] = $this->input->post('user_name');
    $data['user_description_pro'] = $this->input->post('user_description');
    $data['create_date_pro'] = NULL;

    $this->db->insert('user_profile_pro', $data);  
}

本质上,该演示尝试进行两次插入(两个表中的每一个)。如果一个插入失败,则回滚另一个。

我在 CodeIgniter 2.1.3 中构建了它,我可以通过 GitHub 提供应用程序文件,或者将它们压缩并发送给你。

于 2013-03-05T18:31:22.710 回答
3


注意:确保$this->db->trans_begin()在运行手动事务时使用,而不是$this->db->trans_start().

$this -> db -> trans_begin(); 
$this -> utils -> insert_function ( $data );
$this -> utils -> update_function2 ( $test ); 
$this -> db -> trans_complete ();

认证以防使用 MySql,以 InnoDb 格式使用

于 2014-01-30T12:04:26.630 回答
3

试试这个过程。它真的对我有用:)

$this->db->trans_start();
   $this->utils->insert_function($data);
   $this->utils->update_function2($test);
if($this->db->trans_status() === FALSE){
   $this->db->trans_rollback();
}else{
   $this->db->trans_complete();
}
于 2014-07-21T03:39:49.680 回答
0

对于单个插入或更新记录,您可以使用affected_rows 函数

$this->db->insert('table_name', xss_clean($data));


//Check if there is a record affected
if($this->db->affected_rows() > 0)
              {
                  return true;
              }
              else
              { 
                  // if not succeeded
                  // check your last query 
                  die($this->db->last_query());
              }
于 2019-03-01T07:23:12.527 回答