kenny1911/doctrine-aggrolock 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kenny1911/doctrine-aggrolock

Composer 安装命令:

composer require kenny1911/doctrine-aggrolock

包简介

Doctrine ORM extension, that supports optimistic lock for aggregate entities

README 文档

README

DoctrineAggroLock is Doctrine ORM extension that adds support optimistic lock of aggregate entities (associations). DoctrineAggroLock prevents data loss during concurrent changes using an aggregate versioning mechanism.

Features

  • Optimistic lock for aggregates and their associations
  • Integration with Doctrine ORM
  • Configuration via interfaces (AggregateRoot and AggregateEntity)
  • Automatic aggregates versioning
  • DDD (Domain-Driven Design) support

Installation

composer require kenny1911/doctrine-aggrolock

Register the Doctrine Event Subscriber Kenny1911\DoctrineAggroLock\AggroLock.

Symfony Framework Example:

services:
  
  Kenny1911\DoctrineAggroLock\AggregateEntitySubscriber:
    tags:
      - name: doctrine.event_subscriber

Example

Aggregate Root:

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ReadableCollection;
use Doctrine\ORM\Mapping as ORM;
use Kenny1911\DoctrineAggroLock\AggregateRoot;
use Ramsey\Uuid\UuidInterface as Uuid;

/**
 * @final
 */
#[ORM\Entity]
class Order implements AggregateRoot
{
    #[ORM\Version]
    #[ORM\Column(type: 'integer')]
    private int $version = 1;

    /** @var Collection<array-key, OrderItem> */
    #[ORM\OneToMany(targetEntity: OrderItem::class, mappedBy: 'order', cascade: ['persist', 'remove'], orphanRemoval: true)]
    private Collection $items;

    public function __construct(
        #[ORM\Id]
        #[ORM\Column(type: 'uuid')]
        private Uuid $id,
    ) {
        $this->id = $id;
        $this->customerName = $customerName;
        $this->items = new ArrayCollection();
    }

    /**
     * @param non-negative-int $quantity
     * @param non-negative-int $price
     */
    public function addItem(Uuid $productId, int $quantity, int $price): void
    {
        $this->items->add(new OrderItem($this, $productId, $quantity, $price)); // TODO
    }

    /**
     * @return ReadableCollection<array-key, OrderItem>
     */
    public function getItems(): ReadableCollection
    {
        return $this->items;
    }
}

Aggregate Entity

use Doctrine\ORM\Mapping as ORM;
use Kenny1911\DoctrineAggroLock\AggregateEntity;
use Kenny1911\DoctrineAggroLock\AggregateRoot;
use Ramsey\Uuid\UuidInterface as Uuid;

/**
 * @final
 */
#[ORM\Entity]
class OrderItem implements AggregateEntity
{
    /**
     * @param non-negative-int $quantity
     * @param non-negative-int $price
     */
    public function __construct(
        #[ORM\ManyToOne(targetEntity: Order::class, inversedBy: 'items')]
        private Order $order,
        
        #[ORM\Column(type: 'uuid')]
        private Uuid $productId,
        
        #[ORM\Column(type: 'integer')]
        private int $quantity,
        
        #[ORM\Column(type: 'integer')]
        private float $price
    ) {}

    public function getAggregateRoot(): AggregateRoot
    {
        return $this->order;
    }
}

Explanations

  1. Aggregate Root (Order) implements the AggregateRoot interface. It guarantees that the object is the root of the aggregate and not just an entity.

  2. Aggregate Entities (OrderItem) implement the AggregateEntity interface and are associated with the aggregate root through a ManyToOne relation.

  3. Optimistic Locking is implemented using the standard Doctrine ORM approach with a special version field (version).

统计信息

  • 总下载量: 1.25k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-07