npo/doctrine-extension

包含辅助类和服务的 Doctrine 库

2.0 2024-05-08 08:57 UTC

This package is not auto-updated.

Last update: 2024-09-25 09:35:51 UTC


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 调用以 getishas 为前缀的方法(查阅 https://symfony.com.cn/doc/current/components/property_access.html#accessing-public-properties)。不过,您可以通过传递 method_name 属性来指定要使用的方法名称。