Symfony2: Creating entity table conditionally - symfony

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?

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.

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.

Related

partial use of doctrine on an existing php project

I am a new developer on an existing php project that respect its proper mvc.
I have successfully "plug" a Symfony installation on it, in order to replace some of the already existing Symfony components such as Router, Request etc...
I have some functionalities to develop and I am isolating them under one bundle.
My question is : can I use Doctrine for this one in order to start a sanitize work on the existing database ? If I want to use foreign keys with the existing others tables I need to configure the mapping on them...It's a problem because I cannot start a refactoring for the objects of this project that are not entities-like. Is there a solution to use doctrine only for my bundle and keeping the use of foreign-keys, cascading etc... ?
Thank you for all
If you wish to only generate the entities for your isolated bundle, you can do it.
Check documentation: http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_doctrine_entity.html
Are you planning to create your tables in the same database or in a differente database?

How can I get the Doctrine entity metadata before compiling Symfony container?

In one Symfony bundle I define a compiler pass to preprocess some configuration. Part of that config is based on Doctrine entities, so I need to get the full metadata information for all application entities.
The compiler pass is executed very late (PassConfig::TYPE_BEFORE_REMOVING). I'm using $container->get('doctrine') like this to get the entity metadata:
$em = $container->get('doctrine')->getManagerForClass($entityClass);
$entityMetadata = $em->getMetadataFactory()->getMetadataFor($entityClass);
However, this is causing random failures for some users because of the use of the doctrine service during the Symfony container compilation.
I'd recommend to change your entities addressing. Mainly - create your models with interfaces and make entities implementing them.
Using resolve_target_entities Doctrine will "convert" them to the particular classes.
An example code is here: https://github.com/Sylius/SyliusResourceBundle/blob/master/DependencyInjection/Compiler/DoctrineTargetEntitiesResolverPass.php
Just make sure your bundle is registered before DoctrineBundle is registered.
Then - in your whole app - instead of AppBundle::Entity addressing, use FQDN of interface bound to an entity earlier.
I've experimented a bit with compilers and services and it's a very bad idea to base on cross-bundle services under compiling container process... Why? It's not reliable - sometimes it will work as you want, sometimes it will fail as you described.
Thank you all for your comments and ideas. I post an answer to explain how I solved this problem.
Trying to use the Doctrine service in a compiler pass was creating more and more problems for our users (and it was creating other minor issues with other services such as Twig). So this was definitely a bad solution for our needs.
So at the end I decided to change everything. We no longer use a compiler pass to process the configuration and instead we use a regular PHP class called during runtime.
In the dev environment, the configuration is processed for each request. It's a bit slower than before, but we prevent any caching issue. In the prod environment we use Doctrine Cache to process the configuration once. Besides, we've create a cache warmer to create the cached configuration before the first request hits the application.

Installing sfGuard without changing db

I am to integrate sfGuard plugin with existing application. What I want to do is to keep as much code as possible untouched. Any guides? It'd be perfect if I can use actual database schema, or bind it somehow to be used by sfGuard. I know about setting userProfile class but I'm not sure how should I get to it, not to destroy my app.
Greetings
Just install plugin. And try make migrations. doctrine::generate-migrations-diff
php symfony doctrine:generate-migrations-diff
And migrate php symfony doctrine:migrate :
php symfony doctrine:migrate
Check out question: Rebuild model without loss data in MySQL for Symfony

Namespace alias for Doctrine

I became responsible for a large legacy web application and I am trying to slowly refactor it into the Symfony2 framework. The first thing I have done is to include Doctrine.
I have installed Doctrine with the help of Composer and set up a bootstrap file for it. My entities, to avoid future complications, already follow the namespacing scheme Company\BundleName\Entity\Object. The following works:
$em->getRepository('Company\\BundleName\\Entity\\Object')
->find($id)
;
I was unable to find any reference of how to inform Doctrine of namespace aliases as Symfony2 does, so I can write
$em->getRepository('CompanyBundleName:Object')
->find($id)
;
instead. How can I achieve that?
There is an easier way now:
$config = Setup::createAnnotationMetadataConfiguration(...);
$config->addEntityNamespace('CompanyBundleName', 'Company\BundleName\Entity');
will do what you want. It took me several hours hunting to find this! Its not in the docs anywhere I could find.
The functionality for this is set up in Symfony2 by the DoctrineBridge bundle, specifically the getMappingDriverBundleConfigDefaults function.
If you want to reflect this functionality without Symfony2, you'll need to extend the Doctrine entity manager and generate the prefix yourself in the getRepository function. It is not part of the Doctrine system.

Location of propel.ini for symfony2?

I am migrating from symfony1 to symfony2, I have hard time implementing propel behaviors. Where do I actually have the propel.ini in symfony2?
Well in sf1.4, it was inside root config directory. How about symfony2?
The Propel ORM Symfony2 page says this:
You can add a app/config/propel.ini file in your project to specify some configuration parameters. ... However, the recommended way to configure Propel is to rely on build properties.
You can define build properties by creating a propel.ini file in app/config like below, but you can also follow the Symfony2 convention by adding build properties in app/config/config.yml
So I believe you can come close to the Symfony1 behaviour by creating app/config/propel.ini, but the more idiomatic way is to use app/config/config.yml as that page illustrates.
Caveat: I haven't used Propel with Symfony2, so this answer is solely based on the manual.

Resources