CakePHP 2.3.x database transaction

末鹿安然 提交于 2020-01-01 10:49:27

问题


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


回答1:


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();
}


来源:https://stackoverflow.com/questions/18032532/cakephp-2-3-x-database-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!