-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding TransactionalManagerInterface
- Loading branch information
Showing
4 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,17 @@ | |
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\EntityRepository; | ||
use Sonata\Doctrine\Exception\TransactionException; | ||
use Sonata\Doctrine\Model\BaseManager; | ||
use Sonata\Doctrine\Model\TransactionalManagerInterface; | ||
|
||
/** | ||
* @author Sylvain Deloux <[email protected]> | ||
* | ||
* @phpstan-template T of object | ||
* @phpstan-extends BaseManager<T> | ||
*/ | ||
abstract class BaseEntityManager extends BaseManager | ||
abstract class BaseEntityManager extends BaseManager implements TransactionalManagerInterface | ||
{ | ||
/** | ||
* Make sure the code is compatible with legacy code. | ||
|
@@ -78,6 +80,25 @@ public function getEntityManager() | |
return $objectManager; | ||
} | ||
|
||
public function beginTransaction(): void | ||
{ | ||
$this->getEntityManager()->beginTransaction(); | ||
} | ||
|
||
public function commit(): void | ||
{ | ||
try { | ||
$this->getEntityManager()->commit(); | ||
} catch (\Throwable $exception) { | ||
throw new TransactionException($exception->getMessage(), (int) $exception->getCode(), $exception); | ||
} | ||
} | ||
|
||
public function rollBack(): void | ||
{ | ||
$this->getEntityManager()->rollback(); | ||
} | ||
|
||
/** | ||
* @phpstan-return EntityRepository<T> | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\Doctrine\Exception; | ||
|
||
/** | ||
* @author Erison Silva <[email protected]> | ||
*/ | ||
final class TransactionException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Sonata Project package. | ||
* | ||
* (c) Thomas Rabaix <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sonata\Doctrine\Model; | ||
|
||
use Sonata\Doctrine\Exception\TransactionException; | ||
|
||
/** | ||
* @author Erison Silva <[email protected]> | ||
*/ | ||
interface TransactionalManagerInterface | ||
{ | ||
public function beginTransaction(): void; | ||
|
||
/** | ||
* @throws TransactionException | ||
*/ | ||
public function commit(): void; | ||
|
||
public function rollBack(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters