5

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?

j-guyon
  • 1,822
  • 1
  • 19
  • 31
Łukasz Zaroda
  • 869
  • 2
  • 19
  • 55
  • 3
    You can tell doctrine which directories to search for entities under the mappings section: http://symfony.com/doc/current/reference/configuration/doctrine.html So I suppose you could put this entity in it's own directory. But I suspect you are probably making things more difficult than they need to be. – Cerad Mar 04 '16 at 01:08
  • The reason for it is that I have two bundles currently: Config, and ConfigDb, the second is storage backend for the first, it is using Doctrine. I would like to merge both and provide database backend as default, but switchable. Now the thing is that if it will be switched with another backend-service, obviously I don't want for entity tables to be created :) . So I need to create those tables, and use those entities conditionally. – Łukasz Zaroda Mar 06 '16 at 15:21
  • You can disable automapping in config and use manual mapping. Or I think better way is to create model classes with mappings and implements just empty entity extending this models in each of your app. You can even have this models implement interface and use it in references and than substitute interface with real class in doctrine configuration. – zajca Mar 10 '16 at 12:40

2 Answers2

2

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.

Community
  • 1
  • 1
Rvanlaak
  • 2,971
  • 20
  • 40
0

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.

abdiel
  • 2,078
  • 16
  • 24