0
我有一個大的數據集,需要在發佈到服務器時寫入數據庫,但可能會在客戶端編輯器中添加一條額外的記錄,觸發「完整性約束違規」。當拋出數據庫錯誤異常時,我可以回滾事務嗎?
我的問題是,當我到達錯誤觸發的數據點時,很多以前的數據已經在數據庫中更新。我需要回滾並拒絕發佈的數據。
這是我的控制器的操作。
/**
* Handles saving data from the network editor.
*/
public function json_save()
{
if($this->request->is('post'))
{
$result = array();
$data = $this->request->input('json_decode');
if(isset($data->network_id) && !empty($data->network_id))
{
$dataSource = $this->Node->getDataSource();
$dataSource->begin();
$result['nodes'] = $this->updateModel($data->network_id, $this->Node, 'nodes', $data, array(
'ParamValue'
));
$result['connections'] = $this->updateModel($data->network_id, $this->Connection, 'connections', $data);
$dataSource->commit();
$this->viewClass = 'Json';
$this->set('result', $result);
$this->set('_serialize', array(
'result'
));
return;
}
}
throw new ErrorException('Posted data missing.');
}
我控制器的updateModel
功能進行一些刪除和更新機型$this->Node
和$this->Connection
。
如何在更新$this->Connection
模型期間通常拋出的「完整性約束違規」中回滾。
我不確定它是否是一個PHP異常,我可以捕捉然後做回滾,或者如果有不同的方法來捕捉它。