From 26bd3e381195ba0901c506e8257e0eb6e5d5c931 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Mon, 25 Apr 2011 18:32:43 -0300 Subject: [PATCH] Implemented support for closure return on EntityManager::transactional. Fixes DDC-1125 --- lib/Doctrine/ORM/EntityManager.php | 7 ++++++- tests/Doctrine/Tests/ORM/EntityManagerTest.php | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index d06d2dea9..0379cc435 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -203,13 +203,18 @@ class EntityManager implements ObjectManager public function transactional(Closure $func) { $this->conn->beginTransaction(); + try { - $func($this); + $return = $func($this); + $this->flush(); $this->conn->commit(); + + return $return ?: true; } catch (Exception $e) { $this->close(); $this->conn->rollback(); + throw $e; } } diff --git a/tests/Doctrine/Tests/ORM/EntityManagerTest.php b/tests/Doctrine/Tests/ORM/EntityManagerTest.php index 8f9045c34..ca896ad10 100644 --- a/tests/Doctrine/Tests/ORM/EntityManagerTest.php +++ b/tests/Doctrine/Tests/ORM/EntityManagerTest.php @@ -143,4 +143,16 @@ class EntityManagerTest extends \Doctrine\Tests\OrmTestCase $this->_em->close(); $this->_em->$methodName(new \stdClass()); } + + /** + * @group DDC-1125 + */ + public function testTransactionalAcceptsReturn() + { + $return = $this->_em->transactional(function ($em) { + return 'foo'; + }); + + $this->assertEquals('foo', $return); + } } \ No newline at end of file