Łukasz Zaroda
Łukasz Zaroda

Reputation: 767

Symfony2: Creating entity table conditionally

I have a bundle with entity defined in it. I want to be able to configure this bundle in such a way, that this entity will or won't be relevant. So if bundle is configured properly entity table shouldn't be created with app/console doctrine:schema:update etc, or should be - it should depend on configuration.

How to conditionally "disable" entity so its table won't be created by app/console doctrine:schema:update?

Upvotes: 5

Views: 418

Answers (2)

Rvanlaak
Rvanlaak

Reputation: 3085

Your scenario requires you to disable the auto_mapping, but it seems to be set to false by default. http://symfony.com/doc/current/reference/configuration/doctrine.html

Next thing to do is make sure the build function of your bundle conditionally adds the wanted DoctrineOrmMappingPass as also is explained here: https://stackoverflow.com/a/26975083/1794894

As you can see in the source, build only is executed once the cache is empty so this is the place where you can do this. You can also take a look at how to add compiler passes there.

Upvotes: 2

abdiel
abdiel

Reputation: 2106

I think that although maybe you could find a way, you are complicating your self. If the back-end bundle is independent then always could be optional to install it and by consequence it's entities created or not.

You can find an example in Sonata bundles, you can manage the users as you want, but if you are using FOSUserBundle, the you have the option to install SonataUserBundle, then tell to fos_user configuration that the new class belong to the Sonata User and as consequence the new entity will be persisted with a lot of new attributes thanks to class inheritance, and all the crud operations for user will be already configured in sonata views. SonataUser also have it's own user entity for using in a standalone way.

I know that this is not what you asking for but may be you just need manage to follow a model like this.

Upvotes: 0

Related Questions