Added transaction method to database connection and eloquent model.

This commit is contained in:
Taylor Otwell
2012-03-18 22:39:04 -05:00
parent fcff36a0ac
commit 0455438ebe
3 changed files with 51 additions and 15 deletions

View File

@@ -84,6 +84,33 @@ class Connection {
}
}
/**
* Execute a callback wrapped in a database transaction.
*
* @param Closure $callback
* @return void
*/
public function transaction($callback)
{
$this->pdo->beginTransaction();
// After beginning the database transaction, we will call the Closure
// so that it can do its database work. If an exception occurs we'll
// rollback the transaction and re-throw back to the developer.
try
{
call_user_func($callback);
}
catch (\Exception $e)
{
$this->pdo->rollBack();
throw $e;
}
$this->pdo->commit();
}
/**
* Execute a SQL query against the connection and return a single column result.
*