npo / doctrine-extension
包含辅助类和服务的 Doctrine 库
2.0
2024-05-08 08:57 UTC
Requires
- php: ^8.3
- doctrine/orm: ^3.1
- symfony/framework-bundle: ^6.3 || ^7.0
- symfony/property-access: ^6.3 || ^7.0
Requires (Dev)
- dg/bypass-finals: ^1.5.0
- friendsofphp/php-cs-fixer: ^3.23.0
- mockery/mockery: ^1.6
- npo/phpunit-extension: ^1.0
- phpcompatibility/php-compatibility: ^9.3.5
- phpstan/extension-installer: ^1.3.1
- phpstan/phpstan: ^1.10.30
- phpstan/phpstan-phpunit: ^1.3.13
- phpstan/phpstan-symfony: ^1.3.2
- phpunit/phpunit: ^10.3.2
- squizlabs/php_codesniffer: ^3.7.2
README
在 Doctrine 2.x 中,由于孤儿删除功能依赖于 PersistentCollection 类,因此无法实现 DDD。然而,此包通过不使用 PersistentCollection 类即可支持孤儿删除,因此使其符合 DDD 规范。
注意
包含(替代 PersistentCollection 的)实体的数据结构必须是 iterable
(查阅 https://php.ac.cn/manual/en/language.types.iterable.php)
安装
composer require npo/doctrine-extension
将包添加到 config/bundles.php
NpoMessage\DoctrineExtension\DoctrineExtensionBundle::class => ['all' => true],
用法
在 config/packages/npo_doctrine_extension.yaml
文件中配置设置孤儿删除的属性。例如
#config/packages/npo_doctrine_extension.yaml
npo_doctrine_extension:
orphan_removal:
App\Domain\Entity\User:
notes:
class: App\Domain\Entity\Note
mapped_by: user
images:
class: App\Domain\Entity\Image
id: guid
mapped_by: user
App\Domain\Entity\Post:
images:
method_name: giveMeTheImages
class: App\Domain\Entity\Image
mapped_by: post
id
默认情况下,id 使用类元数据提取。但是,您可以通过传递 id
属性来指定要使用的 id
。
method_name
默认情况下,使用 Symfony\Component\PropertyAccess\PropertyAccessorInterface
提取值。默认情况下,PropertyAccessorInterface
调用以 get
、is
或 has
为前缀的方法(查阅 https://symfony.com.cn/doc/current/components/property_access.html#accessing-public-properties)。不过,您可以通过传递 method_name
属性来指定要使用的方法名称。