brokalia/doctrine-entity-generator
Composer 安装命令:
composer require brokalia/doctrine-entity-generator
包简介
Generates a doctrine entity and mapper from domain entity
README 文档
README
This Symfony bundle provides a console command to generate doctrine entities with mapping attributes and a mapper class from domain entity class.
Then you can use domain entities in your domain and application layers and map it to doctrine entities for persistence.
Usage
bin/console doctrine-generator:entity "App\Domain\MyDomainEntity"
Conventions
The primary key of the doctrine entity must be the "id" property of domain entity. If there are not an "id" property in the domain entity, the doctrine entity will not have primary key.
class MyDomainEntity { private string $id; // Will be the doctrine primary key } class DoctrineMyDomainEntity { #[ORM\Column(type: 'string')] #[ORM\Id] public string $id; }
Complete Example
Given MyDomainEntity with some value objects:
// src/Domain/MyDomainEntity.php class MyDomainEntity { public function __construct( private MyDomainEntityId $id, private SampleValueObject $valueObject, ) { } public function getId(): MyDomainEntityId { return $this->id; } public function getValueObject(): SampleValueObject { return $this->valueObject; } }
// src/Domain/MyDomainEntityId.php class MyDomainEntityId { public function __construct(private string $value) { } public function getValue(): string { return $this->value; } }
// src/Domain/EntityId.php class SampleValueObject { public function __construct( private string $firstAttribute, private int $secondAttribute ) { } public function getFirstAttribute(): string { return $this->firstAttribute; } public function getSecondAttribute(): int { return $this->secondAttribute; } }
Generates a doctrine entity and a mapper to map between domain and doctrine entities:
// src/Infrastructure/Persistence/DoctrineMyDomainEntity.php class DoctrineMyDomainEntity { #[ORM\Column(type: 'string')] #[ORM\Id] public string $id; #[ORM\Column(type: 'string')] public string $valueObject_firstAttribute; #[ORM\Column(type: 'integer')] public int $valueObject_secondAttribute; }
// src/Infrastructure/Persistence/DoctrineMyDomainEntityMapper.php class DoctrineMyDomainEntityMapper { public function fromDomain( MyDomainEntity $domainEntity, ?DoctrineMyDomainEntity $doctrineEntity, ): DoctrineMyDomainEntity { $doctrineEntity = $doctrineEntity ?? new DoctrineMyDomainEntity(); $doctrineEntity->id = $domainEntity->getId()->getValue(); $doctrineEntity->valueObject_firstAttribute = $domainEntity->getValueObject()->getFirstAttribute(); $doctrineEntity->valueObject_secondAttribute = $domainEntity->getValueObject()->getSecondAttribute(); return $doctrineEntity; } public function toDomain(DoctrineMyDomainEntity $doctrineEntity): MyDomainEntity { $reflector = new ReflectionClass(MyDomainEntity::class); $constructor = $reflector->getConstructor(); if (!$constructor) { throw new RuntimeException('No constructor'); } $object = $reflector->newInstanceWithoutConstructor(); $constructor->invoke( $object, new MyDomainEntityId($doctrineEntity->id), new SampleValueObject( $doctrineEntity->valueObject_firstAttribute, $doctrineEntity->valueObject_secondAttribute ), ); return $object; } }
Now you can create a repository for the domain entity using doctrine to persist the entity with the mapper.
class MyDomainEntityRepository { public function __construct( private EntityManagerInterface $entityManager, private DoctrineMyDomainEntityMapper $mapper, ) { } public function save(MyDomainEntity $entity): void { // Get previous existent doctrine entity if exists for update cases $existentDoctrineEntity = $this->findDoctrineEntity($entity->getId()->getValue()); // Map domain entity to doctrine entity $doctrineEntity = $this->mapper->fromDomain( $entity, $existentDoctrineEntity ?? new DoctrineMyDomainEntity() ); // Persist $this->entityManager->persist($doctrineEntity); $this->entityManager->flush(); } public function findById(MyDomainEntityId $id): ?MyDomainEntity { // Get doctrine entity $doctrineEntity = $this->findDoctrineEntity($id->getValue()); if (!$doctrineEntity) { return null; } // Return domain entity mapped return $this->mapper->toDomain($doctrineEntity); } private function findDoctrineEntity(string $id): ?DoctrineMyDomainEntity { return $this->entityManager->getRepository(DoctrineMyDomainEntity::class)->find($id); } }
Installation
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Applications that use Symfony Flex
Open a command console, enter your project directory and execute:
$ composer require --dev brokalia/doctrine-entity-generator
Applications that don't use Symfony Flex
Step 1: Download the Bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require --dev brokalia/doctrine-entity-generator
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php file of your project:
// config/bundles.php return [ // ... Brokalia\DoctrineEntityGenerator\DoctrineEntityGeneratorBundle::class => ['dev' => true], ];
brokalia/doctrine-entity-generator 适用场景与选型建议
brokalia/doctrine-entity-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.69k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 11 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 brokalia/doctrine-entity-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brokalia/doctrine-entity-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-18