CakePHP 2.3.x数据库事务
我需要您在CakePHP中使用事务的帮助 我有一个产品模型,其中包含许多价格和属性模型(关键产品id) 在我的产品模型中,我添加了CakePHP 2.3.x数据库事务,cakephp,transactions,commit,Cakephp,Transactions,Commit,我需要您在CakePHP中使用事务的帮助 我有一个产品模型,其中包含许多价格和属性模型(关键产品id) 在我的产品模型中,我添加了 function begin() { $db =& ConnectionManager::getDataSource($this->useDbConfig); $db->begin($this); } function commit() { $db =& ConnectionManager::getDataS
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)
{
//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();
}
问题是,如果在“保存价格”或“属性”行中出现错误,则仍会添加产品。我会认为,当我有任何错误时,不会添加任何行(即回滚将删除它)
我使用的是CakePHP 2.3.8表必须采用InnoDb格式。MyISAM格式的表不支持事务 不需要在模型中插入额外的代码 产品控制器:
$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();
}
您好,但是mysql支持InnoDb表和事务@MarceloAymone:在这种情况下,他指的是MyISAM。这必须称为“引擎”,而不是“格式”!事务是否不会自动在InnoDb表中使用?