SUBSCRIBE VIA RSS


Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.

Topics

TWITTER

Stack Overflow


The old fashioned way

RECENT TUNES

February 29, 2008 – 1:45pm Propel Transactions with Symfony

It’s easy to create transactions with Propel. This can be very useful when you are deleting rows from your database from multiple tables, and want to do an “all or nothing” approach so that things don’t get messy. In this example, we want to delete an event from our database, however this event may have related rows in the user_favorite_event table, and we’d be unable to delete the event without getting a foreign key constraint error from the database. So, we first delete all the user favorites, and then delete the event. If the first query fails, we’ll rollback the deletion of the user_favorite_event rows.

$con = Propel::getConnection(EventPeer::DATABASE_NAME);
try {
	$con->begin();
	UserFavoriteEventPeer::deleteAllForEvent($this->getId());
	EventPeer::doDelete($this, $con);
	$con->commit();
} catch (PropelException $e) {
	$con->rollback();
	throw $e;
}
Posted in  Web Development   |     |  delicious  Digg

5 Responses to “Propel Transactions with Symfony”

  1. Is this meant to be overriding the Event::Delete method? I would expect so, but you’re not very clear…

    By hackel on Mar 19, 2008

  2. Transactions don’t have override the Event::delete method, but in this case it does, thus the calls to $this->getId(). you could also put this type transaction into your actions if you like rather than in the object’s class.

    By Scott Meves on Mar 19, 2008

  3. doesnt work… for one… its beginTransaction()

    By ricardo on May 17, 2010

  4. It depends on what version of propel you are using. This post is from 2008. The API for transactions was changed in Propel 1.3.

    By Scott Meves on May 17, 2010

1 Trackback(s)

  1. Mar 2, 2008: rpsblog.com » A week of symfony #61 (25 february -> 2 march 2008)

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word