Users/Documentation/1.2/HowTos/Transactions

Transactions

Propel uses transactions internally to encapsulate atomic actions, like deleting a row & cascade deleting related rows. You can also specify your own transaction points in Propel.

You can use your own transaction points in Propel. Note that for database that do not support nested transactions (e.g. SQLite) the transaction points specified outside of Propel methods will take precedence (nested begin/commit statements will be ignored by drivers that don't support nested transactions).

To set your own transaction points, you need to retrieve the Creole Connection object and call the begin() and commit() methods yourself.

  // get the Connection object from Propel
  $con = Propel::getConnection(MyPeer::DATABASE_NAME);
 
  try {

    $con->begin();
    
    $obj1->save($con);
    $obj2->save($con);  
    MyPeer::doDelete($obj3, $con);

    $con->commit();

  } catch (Exception $e) {
     $con->rollback();
     throw $e;
  }
}

In the example above if any of the save() or doDelete() methods throw an exception (i.e. are unable to perform their delete action) then the entire group of statements will be rolled back.

Also note in the example above that the $con is passed to the save() and delete() methods. Technically this is unnecessary, since the transaction has been started on the Connection object that will be returned by Propel::getConnection() (remember everything by reference in PHP5), but I find it useful as a reminder to pass in the connection when there are outside-set qualities of the connection being used (in this case, that a transaction has been started). Also, it avoids an unnecessary call to Propel::getConnection() within the methods.

Limitations

Currently there is no support for SELECT blah FOR UPDATE, this is to be scheduled to be fixed in Propel 2.0.