多个模型的事务管理使用单个事务提交和回滚 [英] Transaction management with multiple models using single transaction commit and rollback

查看:280
本文介绍了多个模型的事务管理使用单个事务提交和回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是cakephp的新人。
我想知道是否可能在cakephp处理多个模型提交和回滚与单个事务。
我想做这样的事情

 <?php 
function add(){
$ transaction = begintransaction;
if(model1> save()){
if(model2-> save()){
if(model3-> save(){
}
else {
$ errorFlag ['model3'] =在模型3中出错;
}
}
else {
$ errorFlag ['model2' ] =模型2中的错误;
}
}
else {
$ errorFlag ['model3'] =模型3中出错;
} b $ b if(empty($ errorFlag)){//保存模型没有错误
$ transaction-> commit();
$ this-> Session-> setFlash '多个模型的表单数据被保存',true));
}
else {//保存模型错误
$ transaction-> rollback();
$ this-> Session-> setFlash(__('保存多个模型的表单数据',true));
}
}
?>


解决方案

可以。

  $ this-> Model-> begin(); //开始事务
$ this-> Model-> commit //提交事务
$ this-> Model-> rollback(); //回滚事务

另请参阅手动


I am new to cakephp. I want to know if it is possible in cakephp to to handle multiple model commit and rollback with single transaction. I want to do some thing like this

<?php
function add(){
    $transaction = begintransaction;
    if(model1->save()){
        if(model2->save()){
            if(model3->save(){
            }
            else{
                $errorFlag['model3'] = "Error in model 3"; 
            }
        }
        else{
            $errorFlag['model2'] = "Error in model 2";
        }
    }
    else{
        $errorFlag['model3'] = "Error in model 3";
    }
    if(empty($errorFlag)){ //no error in saving the model
        $transaction->commit();
        $this->Session->setFlash(__('The form data with multiple model is saved', true)); 
    }
    else{   //error in saving the model
        $transaction->rollback();
        $this->Session->setFlash(__('The form data with multiple model is saved', true));
    }
}
?>

解决方案

Yes, you can.

$this->Model->begin(); // Start transaction
$this->Model->commit(); // Commit transaction
$this->Model->rollback(); // Rollback transaction

Also take a look at the manual.

这篇关于多个模型的事务管理使用单个事务提交和回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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