How to automatically add a property on every Entities

184 Views Asked by At

It's been days that I'm trying to achieve the following :

Add a deletedAt property on every entities by default without having to use a trait, or adding inheritance (or mapped superclasses) to all of my already created entities.

The goal here is to also make it work with doctrine migration script so that it would automatically add the associated column in every tables.

Tried to subscribe to the Doctrine\ORM\Events::loadClassMetadata event this way

    class ClassMetadataEventSubscriber implements EventSubscriber
    {
        public function getSubscribedEvents()
        {
            return [
                Events::loadClassMetadata
            ];
        }
        
        public function loadClassMetadata(LoadClassMetadataEventArgs $args) {
            $metadata = $args->getClassMetadata();
            $metadata->mapField([
                'fieldName' => 'deletedAt',
                'type' => 'datetime',
                'nullable' => true,
                'columnName' => 'deleted_at'
            ]);
        }
    }

But I get the ReflectionProperty::_construct() Exception

App\Entity\Etudiant::$deletedAt does not exist

Exception trace: at /srv/api/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/RuntimeReflectionService.php:90 ReflectionProperty->__construct() Doctrine\Persistence\Mapping\RuntimeReflectionService->getAccessibleProperty() Doctrine\ORM\Mapping\ClassMetadataInfo->wakeupReflection() Doctrine\ORM\Mapping\ClassMetadataFactory->wakeupReflection() Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata() Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor() Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() Doctrine\Migrations\Provider\OrmSchemaProvider->createSchema() Doctrine\Migrations\Generator\DiffGenerator->createToSchema() Doctrine\Migrations\Generator\DiffGenerator->generate() Doctrine\Migrations\Tools\Console\Command\DiffCommand->execute()

Also, the deletedAt property is here to enable soft deletion through gedmo/doctrine-extensions

0

There are 0 best solutions below