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の例外なのか、それとも別の方法でキャッチできるのかはわかりません。

4

1 に答える 1

3

次のことができます。

$dataSource->begin();

try {
  // The queries here.
} catch (Exception $e) {
  $dataSource->rollback();
  throw $e;
}

$dataSource->commit();
于 2013-04-08T16:33:55.680 に答える