grifix/entity-manager

此包已被弃用且不再维护。未建议替代包。

具有乐观锁的实体管理器

维护者

详细信息

gitlab.com/grifix/entity-manager/

dev-main 2023-02-24 12:43 UTC

This package is auto-updated.

Last update: 2023-07-26 07:03:59 UTC


README

当我们使用CQRS模式时,通常不需要经典ORM提供的一切。我们不需要关系、条件、延迟加载等。我们只需要添加聚合并从存储库中获取聚合。这个库提供了这种简单功能,还提供了使用Grifix Normalizer库升级实体结构。它还提供了乐观锁以解决并发问题。

安装

composer require grifix/entity-manager

使用方法

想象我们有一个名为Person的类,我们希望将其存储到数据库中

final class Person
{
    public function __construct(
        public readonly string $id,
        public string $firstName,
        public string $lastName
    ) {
    }
}

创建具有以下结构的表

create table persons
(
    id uuid constraint persons_pk primary key,
    version bigint not null,
    data    jsonb  not null
);

创建实体管理器

$dbConnection =  \Doctrine\DBAL\DriverManager::getConnection(
    [
        'dbname' => 'dbname',
        'user' => 'user',
        'password' => 'password',
        'host' => 'host',
        'driver' => 'pdo_pgsql'
    ],
);
$entityManager = \Grifix\EntityManager\EntityManager::create($dbConnection);

注册Person实体类型

$entityManager->registerEntityType(
    'person',
    Person::class,
    'persons',
    [
        Schema::create()
            ->withStringProperty('id')
            ->withStringProperty('firstName')
            ->withStringProperty('lastName')
    ]
);

现在您可以将其存储到数据库中

$person = new Person(
            '46acc317-3d11-4aa5-b306-4d6a88f57378',
            'John',
            'Connor'
        );
$this->entityManager->add($person, '46acc317-3d11-4aa5-b306-4d6a88f57378');
$this->entityManager->flush();

并可以从数据库中获取它

$person = $this->entityManager->get(Person::class, '46acc317-3d11-4aa5-b306-4d6a88f57378');

实体数据结构升级

在实体内部注入依赖