delete('Users') ; // here we check our current transaction scope and create a transaction or savepoint based on need $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE ; if ($useSavepoint) { $conn->beginTransaction(__FUNCTION__) ; } else { $conn->beginTransaction() ; } try // always wrap transactional statements in a try/catch block { $deleted = $q->execute() ; // execute our doctrine query // commit our transactions or the savepoint if ($useSavepoint) { $conn->commit(__FUNCTION__) ; } else { $conn->commit() ; } } catch(Exception $e) // if we have a problem... { // if we started with a savepoint, let's end with one, otherwise, rollback globally if ($useSavepoint) { $conn->rollback(__FUNCTION__) ; } else { $conn->rollback() ; } // ALWAYS log rollbacks with as much useful information as possible $err = 'Couldn\'t delete users\'! Rolled back changes!' ; sfContext::getInstance()->getLogger()->err($err) ; throw $e ; // always remember to throw an exception after rollback } // except where other output is desired, return the number of operations performed return $deleted ; } /** * A method to add users * * @param array $users An indexed array of user names to be added to the Users table. * * array( $username1, $username2, ... ) * * @param Doctrine_Connection $conn An optional doctrine connection object. If one is not passed * the default will be used. */ public function addUsers ($users, Doctrine_Connection $conn = NULL) { // here you can pick up the default connection if not passed one explicitly if (is_null($conn)) { $conn = Doctrine_Manager::connection() ; } $coll = new Doctrine_Collection('Users') ; // set up our collection foreach ($users as $user) // loop through our users array { // create a new doctrine record object for the Users table $newUser = new User() ; $newUser['name'] = $user ; $coll->add($newUser) ; // add the new user object to our collection } // here we check our current transaction scope and create a transaction or savepoint based on need $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE ; if ($useSavepoint) { $conn->beginTransaction(__FUNCTION__) ; } else { $conn->beginTransaction() ; } try // always wrap transactional statements in a try/catch block { $coll->save($conn) ; // save our collection // commit our transactions or the savepoint if ($useSavepoint) { $conn->commit(__FUNCTION__) ; } else { $conn->commit() ; } } catch(Exception $e) // if we have a problem... { // if we started with a savepoint, let's end with one, otherwise, rollback globally if ($useSavepoint) { $conn->rollback(__FUNCTION__) ; } else { $conn->rollback() ; } // ALWAYS log rollbacks with as much useful information as possible $err = sprintf('Couldn\'t insert users %s! Rolled back changes!', json_encode($users)) ; sfContext::getInstance()->getLogger()->err($err) ; throw $e ; // always remember to throw an exception after rollback } // except where other output is desired, return the number of operations performed return count($coll) ; } /** * A method to delete all users, then add new ones from the $users parameter. * * @param array $users An indexed array of user names to be added to the Users table. * * array( $username1, $username2, ... ) * * @param Doctrine_Connection $conn An optional doctrine connection object. If one is not passed * the default will be used. */ public function deleteAndAddUsers($users, Doctrine_Connection $conn = NULL) { $results = array() ; // our results array // here you can pick up the default connection if not passed one explicitly if (is_null($conn)) { $conn = Doctrine_Manager::connection() ; } // here we check our current transaction scope and create a transaction or savepoint based on need $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE ; if ($useSavepoint) { $conn->beginTransaction(__FUNCTION__) ; } else { $conn->beginTransaction() ; } try // always wrap transactional statements in a try/catch block { $results['deleted'] = $this->delUsers($conn) ; // execute delUsers, passing it our connection $results['added'] = $this->addUsers($users, $conn) ; // execute addUsers, passing our connection // commit our transactions or the savepoint if ($useSavepoint) { $conn->commit(__FUNCTION__) ; } else { $conn->commit() ; } } catch(Exception $e) // if we have a problem... { // if we started with a savepoint, let's end with one, otherwise, rollback globally if ($useSavepoint) { $conn->rollback(__FUNCTION__) ; } else { $conn->rollback() ; } // ALWAYS log rollbacks with as much useful information as possible $err = sprintf('Failed to execute %s.', __FUNCTION__) ; sfContext::getInstance()->getLogger()->err($err) ; throw $e ; // always remember to throw an exception after rollback } return $results ; // just return our results array } } $users = array('Fester', 'Bester', 'Lester', 'Esther', 'Chester') ; // array of users $mtc = new myTransactionsClass() ; $mtc->delUsers() ; $mtc->addUsers($users) ; $mtc->deleteAndAddUsers($users) ;