简体   繁体   English

如何在codeigniter中使用事务进行基于数据更改的提交和回滚?

[英]how to use transactions in codeigniter for commit and rollback based on data change?

I want to learn how I could use MYSQL TRANSACTIONS . 我想学习如何使用MYSQL TRANSACTIONS

I have use case where I have two tables. 我有用例,我有两张桌子。 Let say Table1 and Table2. 让我们说Table1和Table2。 Now I insert some data in Table1 and the insert id I get from that table I want to insert it into table 2. 现在我在Table1中插入一些数据,并从该表中获取insert id ,我想将其插入表2中。

If values are successfully inserted in Table1, and during the insertion of Table2 values if any error occurs I want to delete the values from Table1 as well, as the query for my Table2 was not successful. 如果在Table1中成功插入值,并且在插入Table2值期间发生任何错误,我也想删除Table1中的值,因为我的Table2的查询不成功。

I just want to understand how this could be done using Codeigniter. 我只想了解如何使用Codeigniter完成此操作。 How to COMMIT or ROLLBACK as per need. 如何根据需要进行COMMIT或ROLLBACK。

Please help me with some sample code to understand. 请帮我一些示例代码来理解。

Update 更新

I also referred the Codeigniter UserGuide. 我还提到了Codeigniter UserGuide。 But I did not understood the concept of Running Transactions manually What does this mean ? 但我没有理解Running Transactions manually的概念这是什么意思? As mentioned above I want to do something like trigger which is automatic, I mean if my query fail I want it to ROLLBACK whatever it did, using Codeigniter. 如上所述,我想做一些像触发器这样是自动的,我的意思是如果我的查询失败,我希望它使用Codeigniter进行ROLLBACK。

Code: 码:

$this->db->trans_begin();
$data = $this->Product_m->array_from_post(array('name','description'));
$this->Product_m->save($data,$id);
$pid = $this->db->insert_id();

$num_of_license = $_POST['license'];

$this->Product_m->create_product($pid,$num_of_license);
    if ($this->db->trans_status() === FALSE)
    {
            $this->db->trans_rollback();
    }
    else
    {
            $this->db->trans_commit();
    }

Now in this case I tried doing this: 现在在这种情况下我尝试这样做:

This statement $this->Product_m->create_product($pid,$num_of_license); 此语句$this->Product_m->create_product($pid,$num_of_license); inserts data based on the previous save() method, Now suppose some error occurs during create_product() method. 基于先前的save()方法插入数据,现在假设在create_product()方法期间发生了一些错误。 Then I want to rollback. 然后我想回滚。 I want to delete the record that save() method did. 我想删除save()方法所做的记录。

Use the following steps. 使用以下步骤。

1.Begin your transaction using $this->db->trans_begin(); 1.使用$this->db->trans_begin();开始你的交易$this->db->trans_begin(); .

2.Performs queries. 2.执行查询。

3.Check Transaction status using $this->db->trans_status() . 3.使用$this->db->trans_status()检查交易状态。

4.If status is true commit transaction using $this->db->trans_commit(); 4.如果status是真正的commit使用事务$this->db->trans_commit(); .

5.If status is false rollback transaction using $this->db->trans_rollback(); 5.如果status为false,则rollback事务使用$this->db->trans_rollback(); .

$this->db->trans_begin();

$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');

if ($this->db->trans_status() === FALSE)
{
        $this->db->trans_rollback();
}
else
{
        $this->db->trans_commit();
}

For more see docs Codeigniter Transaction 有关更多信息,请参阅文档Codeigniter Transaction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM