サーバーに投稿するときにデータベースに書き込む必要がある大きなデータ セットがありますが、クライアント エディターのバグにより、「整合性制約違反」をトリガーする余分なレコードが追加される可能性があります。
私の問題は、エラーがトリガーされるデータのポイントに到達すると、以前のデータの多くがデータベースで既に更新されていることです。投稿されたデータをロールバックして拒否する必要があります。
これが私のコントローラーのアクションです。
/**
* 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の例外なのか、それとも別の方法でキャッチできるのかはわかりません。