Home > Back-end >  Is it possible to make a Doctrine entitiy / database table optional in Symfony 5 bundle?
Is it possible to make a Doctrine entitiy / database table optional in Symfony 5 bundle?

Time:11-25

I am working on a Symfony 5 bundle which I want to use in different projects. The bundle includes a number of different entities which can be used in the projects. Some projects will use all those entities, other just a few and some will not use any of these entities but only the other functionality the bundle offers.

However, Doctrine will always automatically create the corresponding database tables for all these entities in all projects. So there are a lot of empty / not used tables in the project databases.

While the empty tables do not any real harm, I think this is not really a clean approach. Is it possible to make these entities optional to let the project decide which to use and which tables have to be created?

Of course it would be the bundles responsibility to make sure, that deactivated entities cannot be used, but this is no problem.

Obviously I could break down the bundle into multiple different bundles which all contain only a few/one entity, but this would create a lot of overhead and would not be clean either.

CodePudding user response:

You can create a custom behaviour by writing your own Metadata driver.

In order to not rewrite everything, you could extend the Doctrine\ORM\Mapping\Driver\AnnotationDriver class and only call parent::loadMetadataForClass($className, $metadata); if the className is on your entity allow-list, depending on your bundle configuration.

You'll need to register your metadata driver on Symfony:

doctrine:
  orm:
    mappings:
      your-bundle:
        is_bundle: true
        type: Path\To\Your\Driver
        prefix: 'Namespace\of\your\bundle\Entity\'
  • Related