Friday, February 5, 2010

Atomic transactions in Zend

If you have many database changes where all need to succeed or none at all do the following:

//Get a hold of the database adapter:
$model = new Model_DbTable_();
$adapter = $model->getAdapter();
try {
//Do inserts, updates, deletes etc.
//Commit the changes and do rollback if it fails:
catch (Exception $e) {
//Print errors
$echo $e->getMessage();

or similarly:

try {
$db->insert('server', array('key' => 'foo', 'value' => 'bar'));
$db->insert('server', array('key' => 'bar', 'value' => 'baz'));
$db->insert('server', array('key' => 'baz', 'value' => 'foo'));

catch (Exception $e) {

Solution found:

No comments:

Post a Comment