npo/doctrine-extension

Library containing helper classes and services Doctrine

2.0 2024-05-08 08:57 UTC

This package is not auto-updated.

Last update: 2024-11-20 10:19:46 UTC


README

When it comes to orphan removal in Doctrine 2.x, DDD is not made possible as the orphan removal feature relies on the PersistentCollection class. However, this package enables support for orphan removal without the use of the PersistentCollection class, thus making it DDD compliant.

Note:

The data structure containing (the one replacing the PersistentCollection) the entities must be an iterable (lookup https://www.php.net/manual/en/language.types.iterable.php)

Installation

composer require npo/doctrine-extension

Add the bundle to config/bundles.php:

NpoMessage\DoctrineExtension\DoctrineExtensionBundle::class => ['all' => true],

Usage

Configure the properties that are set for orphan removal in the config/packages/npo_doctrine_extension.yaml file. For example:

#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

By default, the id is extracted using class metadata. However, you can specify which id to use by passing the id property.

method_name

By default, the Symfony\Component\PropertyAccess\PropertyAccessorInterface is used to extract the values. The PropertyAccessorInterface calls methods that are prefixed by either get, is, or has by default (lookup https://symfony.com/doc/current/components/property_access.html#accessing-public-properties). Nonetheless, you can specify the method name to be used by passing the method_name property.