Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 交易可以';代码点火器中的t回滚_Php_Mysql_Codeigniter_Transactions_Codeigniter 3 - Fatal编程技术网

Php 交易可以';代码点火器中的t回滚

Php 交易可以';代码点火器中的t回滚,php,mysql,codeigniter,transactions,codeigniter-3,Php,Mysql,Codeigniter,Transactions,Codeigniter 3,我正在处理事务,在上面提到的代码中,我遇到了一些问题。我没有提交事务,但它将数据插入了我的数据库 $this->db->trans_begin(); $this->db->insert('tblorder',$data); $orderid=$this->db->insert_id(); foreach ($orderItemList as $orderItemList) { $orderitem = array('orderid' =>$ord

我正在处理事务,在上面提到的代码中,我遇到了一些问题。我没有提交事务,但它将数据插入了我的数据库

$this->db->trans_begin();
$this->db->insert('tblorder',$data);
$orderid=$this->db->insert_id();
foreach ($orderItemList as $orderItemList) {
    $orderitem = array('orderid' =>$orderid ,'productid' =>$orderItemList->productid ,'amount' =>$orderItemList->amount);
    $this->db->insert('tblorderitem',$orderitem);
}
$this->db->trans_complete();

if ($this->db->trans_status() == 1) {
    $this->db->trans_rollback();
    return "true";
} else {
    $this->db->trans_commit();
    return "false";
}
我回滚了该事务,并且再次将所有数据插入到我的数据库中。有什么问题吗?我拿不到。

答案更新

我必须告诉你codeigniter配置的默认设置是自动提交所有事务

如果出于任何原因要禁用此功能,则必须使用此行:

$this->db->trans_off();    
之前

$this->db->begin();
说了,所以当你使用

$this->db->trans_complete();
您需要提交或回滚,如下所示:

$result = $this->db->trans_complete();

if($result === true){
    $this->db->trans_commit();
}else{
    $this->db->trans_rollback();
}
最后,您想要的是:

$this->db->trans_off();

$this->db->query("insert this...");
$this->db->query("insert that...");

$result = $this->db->trans_complete();

if($result === true){
    $this->db->trans_commit();
}else{
    $this->db->trans_rollback();
}

正如我所知,对于CI版本3.0.1,您唯一应该担心的是在哪里放置
$this->db->trans_start()
$this->db->trans_complete()

因此,对于以下情况:

$this->db->trans_start();
//any code goes here
$this->db->trans_complete();
如果出现问题,事务将回滚,或者在调用时提交到
$this->db->trans_complete()

因为如果您在trans\u start方法的引擎盖下查看,它包含trans\u start。而trans\u complete方法包含对trans\u状态的检查,并相应地调用trans\u committrans\u rollback方法

这同样适用于嵌套事务:

$this->db->trans_start();
//any code goes here
$this->db->trans_start();
//any code goes here
$this->db->trans_complete();
//any code goes here
$this->db->trans_complete();
默认情况下,Codeigniter以严格模式运行所有事务,因此最后一次trans_complete将提交所有事务,或者如果任何事务失败,则所有回滚都将提交

为什么要
回滚
?是不是因为错误的
传输状态以外的原因?如果是,请在调用
trans\u complete
trans\u commit
之前调用
trans\u rollback

听起来这对您的情况来说是最佳选择(对语法错误表示歉意):


不要使用
$this->db->trans_complete()

$this->db->trans_begin();

$this->db->insert('tblorder',$data);
$orderid=$this->db->insert_id();

$orderitem = array();

foreach ($orderItemList as $orderItemList) 
{
    $orderitem[] = array('orderid' =>$orderid ,'productid' =>$orderItemList->productid ,'amount' =>$orderItemList->amount);
}

$this->db->insert_batch('tblorderitem', $orderitem); 

$this->db->trans_complete();

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

像这样做如果您想了解有关交易的更多信息。这有助于您对交易有一个清晰的认识。


您编写的代码与编写自动事务的方式几乎相同——只是不尽相同。我的建议?不要试图胜过CodeIgniter,使用“自动”模式进行事务处理。然后,如果需要,将为您处理回滚

我用自动事务模式重做了你的代码。我还从
$orderItemList
收集数据,因此可以使用批处理插入数据,而不是在循环中重复调用
db->insert

$this->db->trans_start();
$this->db->insert('tblorder', $data);
$orderid = $this->db->insert_id();
foreach($orderItemList as $orderItem)
{
    $orderedItems[] = [
      'orderid' => $orderid,
      'productid' => $orderItem->productid,
      'amount' => $orderItem->amount
    ];
}
if(isset($orderedItems))
{
    $this->db->insert_batch('tblorderitem', $orderedItems);
}
$this->db->trans_complete();
$this->db->trans_status();

我想要回滚事务,而不是执行什么操作?您使用
trans\u off()
是错误的。一旦运行该方法,其他
trans
方法都不会执行。好。。。从技术上讲,它们执行,但一旦看到调用了
trans\u off()
,它们立即返回FALSE。
$this->db->trans_begin();

$this->db->insert('tblorder',$data);
$orderid=$this->db->insert_id();

$orderitem = array();

foreach ($orderItemList as $orderItemList) 
{
    $orderitem[] = array('orderid' =>$orderid ,'productid' =>$orderItemList->productid ,'amount' =>$orderItemList->amount);
}

$this->db->insert_batch('tblorderitem', $orderitem); 

$this->db->trans_complete();

if ($this->db->trans_status() === FALSE) 
{
    $this->db->trans_rollback();
    return FALSE;
} 
else 
{
    $this->db->trans_commit();
    return TRUE;
}
$this->db->trans_start();
$this->db->insert('tblorder', $data);
$orderid = $this->db->insert_id();
foreach($orderItemList as $orderItem)
{
    $orderedItems[] = [
      'orderid' => $orderid,
      'productid' => $orderItem->productid,
      'amount' => $orderItem->amount
    ];
}
if(isset($orderedItems))
{
    $this->db->insert_batch('tblorderitem', $orderedItems);
}
$this->db->trans_complete();
$this->db->trans_status();