tourze/symfony-aop-doctrine-bundle

Doctrine enhance

0.0.2 2025-04-24 09:17 UTC

This package is auto-updated.

Last update: 2025-05-11 17:47:35 UTC


README

English | 中文

Latest Version Build Status Quality Score Total Downloads

A Symfony bundle that enhances Doctrine ORM with declarative transaction management via AOP (Aspect-Oriented Programming), inspired by the design of Spring Boot.

Features

  • Declarative transaction support with #[Transactional] attribute
  • Automatic transaction begin/commit/rollback
  • Nested transaction and transaction propagation
  • Detailed transaction logging
  • Smart transaction reuse and performance optimization

Installation

composer require tourze/symfony-aop-doctrine-bundle

Quick Start

use Tourze\Symfony\AopDoctrineBundle\Attribute\Transactional;

class YourService
{
    #[Transactional]
    public function doSomething()
    {
        $this->entityManager->persist($entity);
        $this->entityManager->flush();
        // If an exception is thrown, the transaction will be rolled back automatically.
        // If completed normally, the transaction will be committed automatically.
    }
}

Nested Transactions

class YourService
{
    #[Transactional]
    public function outerMethod()
    {
        $this->innerMethod();
        // The transaction is committed here.
    }

    #[Transactional]
    public function innerMethod()
    {
        // This method reuses the outer transaction, no new transaction is created.
    }
}

Transaction Logging

use Psr\Log\LoggerInterface;

class YourService
{
    public function __construct(private LoggerInterface $logger) {}

    #[Transactional]
    public function doSomething()
    {
        // Transaction start and end will be automatically logged.
        $this->entityManager->persist($entity);
        $this->entityManager->flush();
    }
}

Notes

  • The #[Transactional] attribute can only be applied to public methods.
  • Nested transactions are automatically reused.
  • Any exception thrown in a transactional method will cause a rollback.
  • Make sure to use the correct EntityManager inside transactional methods.
  • Avoid long-running transactions and non-DB operations inside transactions.
  • Distributed and cross-database transactions are not supported.

Contributing

See CONTRIBUTING.md for details.

License

MIT License. See LICENSE for details.

Changelog

See CHANGELOG.md for version history and upgrade notes.