Skip to content

Commit

Permalink
Refactor low-level transactions to `DatabaseTransaction/Repository->t…
Browse files Browse the repository at this point in the history
…ransaction()` 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
  • Loading branch information
burithetech committed Nov 15, 2024
1 parent 2547be7 commit 3515795
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/Forms/SubscriptionTransferConfirmationFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,7 +28,6 @@ public function __construct(
private readonly SubscriptionsRepository $subscriptionsRepository,
private readonly UsersRepository $usersRepository,
private readonly Translator $translator,
private readonly Connection $connection,
) {
}

Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/SubscriptionTypeTagsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 3515795

Please sign in to comment.