Skip to content

tito10047/doctrine-transaction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tests Coverage Status

Doctrine Transaction

When you use Repository classes in your application, and don't use EntityManager directly, you can't use the transaction methods on Repository classes. This package allows you to use transactions anywhere in your application.

This package is also useful when you need to use multiple connections in the same transaction.

Setup

composer require tito10047/doctrine-transaction

Try it

#service.yaml
services:
    Tito10047\DoctrineTransaction\TransactionManagerInterface:
        class: Tito10047\DoctrineTransaction\TransactionManager
use Tito10047\DoctrineTransaction\TransactionManager;

class MyService
{

    public function __construct(private readonly TransactionManagerInterface $tm)
    {
    }

    public function myMethod()
    {
        $transaction = $this->tm->beginTransaction();
        try {
            // Your code
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollback();
            throw $e;
        }
    }
    
    public function myBatchMethod() {
        $transaction = $this->tm->beginTransaction();
        try {
            for($i = 0; $i < 100; $i++) {
                $myEntity = new MyEntity();
                if ($transaction->batchCommit($i,10)){
                    $transaction->clear(MyEntity::class);
                }
            }
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollback();
            throw $e;
        }    
    }
    
    public function myCallbacksMethod() {
        $transaction = $this->tm
            ->beginTransaction()
            ->addCommitHandler(function() {
                // Your code
            })
            ->addRollbackHandler(function() {
                // Your code
            });
        try {
            for($i = 0; $i < 100; $i++) {
                $myEntity = new MyEntity();
                if ($transaction->batchCommit($i,10)){
                    $transaction->clear(MyEntity::class);
                }
            }
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollback();
            throw $e;
        }
    }
    
    public function multipleConnections() {
        $transaction = $this->tm->beginTransaction('connection1','connection2');
        try {
            // Your code
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollback();
            throw $e;
        }
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages