npo / doctrine-extension
Library containing helper classes and services Doctrine
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
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.