From 351579547d8987a04425af935291357bb90478b4 Mon Sep 17 00:00:00 2001 From: Peter Burian Date: Fri, 15 Nov 2024 07:45:08 +0000 Subject: [PATCH] Refactor low-level transactions to `DatabaseTransaction/Repository->transaction()` API to handle nested transactions - Important: It is recommended to update transactions to use new `DatabaseTransaction` API to ensure correct transaction handling within nested transactions. - Introduced new `DatabaseTransaction` API usable directly via DI or via `Repository->getTransaction()` remp/crm#3330 --- src/Forms/SubscriptionTransferConfirmationFormFactory.php | 4 +--- src/Repositories/SubscriptionTypeTagsRepository.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Forms/SubscriptionTransferConfirmationFormFactory.php b/src/Forms/SubscriptionTransferConfirmationFormFactory.php index 1d6eae0..88887db 100644 --- a/src/Forms/SubscriptionTransferConfirmationFormFactory.php +++ b/src/Forms/SubscriptionTransferConfirmationFormFactory.php @@ -10,7 +10,6 @@ use Crm\UsersModule\Repositories\UsersRepository; use Exception; use Nette\Application\UI\Form; -use Nette\Database\Connection; use Nette\Utils\ArrayHash; use Tomaj\Form\Renderer\BootstrapVerticalRenderer; @@ -29,7 +28,6 @@ public function __construct( private readonly SubscriptionsRepository $subscriptionsRepository, private readonly UsersRepository $usersRepository, private readonly Translator $translator, - private readonly Connection $connection, ) { } @@ -118,7 +116,7 @@ private function transferSubscription(array $providers, ActiveRow $subscription, throw new Exception('User needs to transfer subscription to another user.'); } - $this->connection->transaction(function () use ($providers, $subscription, $userToTransferTo, $values) { + $this->usersRepository->getTransaction()->wrap(function () use ($providers, $subscription, $userToTransferTo, $values) { foreach ($providers as $provider) { $provider->transfer($subscription, $userToTransferTo, $values); } diff --git a/src/Repositories/SubscriptionTypeTagsRepository.php b/src/Repositories/SubscriptionTypeTagsRepository.php index 07376dc..3570231 100644 --- a/src/Repositories/SubscriptionTypeTagsRepository.php +++ b/src/Repositories/SubscriptionTypeTagsRepository.php @@ -43,7 +43,7 @@ final public function removeTagsForSubscriptionType(ActiveRow $subscriptionType) final public function setTagsForSubscriptionType(ActiveRow $subscriptionType, array $tags): void { - $this->database->transaction(function () use ($tags, $subscriptionType) { + $this->getTransaction()->wrap(function () use ($tags, $subscriptionType) { $this->removeTagsForSubscriptionType($subscriptionType); foreach ($tags as $tag) { $this->insert([