CakePHP 2.3.x数据库事务 [英] CakePHP 2.3.x database transaction

查看:182
本文介绍了CakePHP 2.3.x数据库事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您在CakePHP中使用交易的帮助。



我有一个产品模型,包含子句hasMany到价格和属性模型(关键product_id)。



我的产品模型,我添加

  function begin(){
$ db =& ConnectionManager :: getDataSource($ this-> useDbConfig);
$ db-> begin($ this);
}

function commit(){
$ db =& ConnectionManager :: getDataSource($ this-> useDbConfig);
$ db-> commit($ this);
}
function rollback()
{
$ db =& ConnectionManager :: getDataSource($ this-> useDbConfig);
$ db-> rollback($ this);
}

在ProductController中,我使用save()来保存我的产品,然后我的价格和财产。 (我只使用save(),而不是saveAll())。



我的代码是:

  $ this-> Product-> begin(); 
$ error = false;
if($ this-> Product-> save($ data)
{
//我的函数和计算
if(!$ this-> Price-> ; save($ data_one)
{
$ error = true;
}
//计算
if(!$ this> Property-> save )
{
$ error = true;
}
}
if($ error){
$ this-> Product-> rollback );
}
else
{
$ it-> Product-> commit();
}

问题是,如果我在保存价格或属性行中有错误,产品仍然添加。



我使用CakePHP 2.3.8

。表格的MyISAM格式不支持事务处理。
























无需向模型中插入其他代码。



ProductController:

  $ datasource = $ this-> Product-> getDataSource(); 
try {
$ datasource-> begin();
if(!$ this-> Product-> save($ data)
throw new Exception();

if(!$ this-> Price-> ; save($ data_one)
throw new Exception();

if(!$ this-> Property-> save($ my_data)
throw new Exception ;

$ datasource-> commit();
} catch(Exception $ e){
$ datasource-> rollback();
}


I need your help using transactions in CakePHP.

I have a Product model, with clause hasMany to Price and Property models (key product_id).

In my Product model, I add

function begin() {
    $db =& ConnectionManager::getDataSource($this->useDbConfig);
    $db->begin($this); 
} 

function commit() {
    $db =& ConnectionManager::getDataSource($this->useDbConfig);
    $db->commit($this); 
} 
function rollback() 
{
    $db =& ConnectionManager::getDataSource($this->useDbConfig);
    $db->rollback($this); 
}

And in ProductController I use save() to save my Product, and then my Price and Property. (I use only save(), not saveAll() ).

My code is:

$this->Product->begin(); 
$error = false; 
if($this->Product->save($data) 
{ 
    //my functions and calculations 
    if(!$this->Price->save($data_one) 
    { 
        $error = true; 
    }
    //calculations
    if(!$this>Property->save($my_data)
    { 
        $error = true; 
    }
} 
if($error) {
    $this->Product->rollback();
}
else
{
    $this->Product->commit(); 
}

The problem is that if I have an error inside the save Price or Property row, the Product is still added. I would have thought that when I have any errors, none of my rows would be added (i.e. a rollback would delete it).

I am using CakePHP 2.3.8

解决方案

Tables must be in InnoDb format. MyISAM format of tables doesn't support transactions.

No need to insert additional code into model.

ProductController:

$datasource = $this->Product->getDataSource();
try {
    $datasource->begin();
    if(!$this->Product->save($data)
        throw new Exception();

    if(!$this->Price->save($data_one)
        throw new Exception();

    if(!$this->Property->save($my_data)
        throw new Exception();

    $datasource->commit();
} catch(Exception $e) {
    $datasource->rollback();
}

这篇关于CakePHP 2.3.x数据库事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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