Symfony flag-toggling entities - symfony

I want to ask, is it possible to have "Flagception - Feature toggle bundle", to have entities ignored in doctrine migrations?
I have a huge project, which I have to reuse many times. I can simply use it, flag unnecessary elements and it is pretty much ready, but doctrine migrations are still using unused entities, and creating tables for them. Is it possible flag specific entities, so doctrine migration would ignore them?

Try using doctrine dbal schema_filter option like this:
doctrine:
dbal:
schema_filter: ~^(?!prefix_)~
Every table starting with prefix_ will be ignored.
More about ignoring custom tables here

You can do it via command line no need to write in doctrine.yaml file everytime. Using this command would filter out all tables starting with foobar:
php bin/console doctrine:migrations:diff --filter-expression='~^(?!foobar)~'

Related

How to define entire database schema in Symfony 4?

Is there a way to define the complete database schema in one go in Symfony 4?
I understand that individual entities/objects can be created using the make:entity and make:migration commands but I'm wondering if I could just define the entire schema in one sitting and then use it to build the associated entities and database.
I recall that in earlier versions of Symfony it was possible to define the entire schema in a YAML file and then just issue a build command.
Yes, you can create complete database schema mappings using any of supported mapping formats (e.g. YAML or XML) and declare mappings location in Doctrine configuration. After that you will be able to use any Doctrine console tools to generate and update schema. You can also use tools for reverse engineering mappings from already available database and to convert mappings between formats
Please notice that Doctrine commands names in Symfony application are different from ones that natively provided by Doctrine. You need to use:
doctrine:schema:validate for schema validation
doctrine:schema:create for initial schema generation with subsequent calls of doctrine:schema:update with either --dump-sql or --force depending on your needs
doctrine:mapping:convert to reverse engineer available database (with use of --from-database option) or convert between mapping types in a case if you want to.

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?
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.

Doctrine does not see my custom Entity

I'm new to Symfony/Doctrine. I've create an entity "Merchant" based on a table I had created. It worked perfectly.
Now I've created an entity "Provider" and would like to generate the table for it.
I've used doctrine:mapping:import and it only imports the Merchant one. The Provider is nowhere to be found.
Thanks!
Edit : I've tried using "shema:update" but it returns "Nothing to update - Your database is already in sync".
Make sure your "Provider" class "name" or "namespace" is right and also do't forget to use
use Doctrine\ORM\Mapping as ORM;
if your entity class is in the annotation format ..
Sometimes may be it's file name is "Provide" rather than "Provider.php" and may be class name is not as per its namespace or also you have forgotten to use namespace ..
If you are trying create database schema based on your entities you should use different command.
Following one will drop and create the schema (pay attention that this will remove all your database data):
app/console doctrine:schema:drop --force
app/console doctrine:schema:create
Or you can just update existing schema:
app/console doctrine:schema:update --force
In addition to above command you can just look at sql changes that doctrine is going to apply to your db, without applying them:
app/console doctrine:schema:update --dump-sql
Make sure your database config file has your database name that matches your Entity's schema name.
If they are mismatched, no differences are notec, no SQL gets created
I had same problem. Console restart worked for me.

Symfony Doctrine Mapping:Import & Table Association

I am working with Symfony 2.3 on a new project using an existing database with numerous associations - many-to-many, one-to-many etc. During my initial import last week, I found somewhere in the docs stipulated that a doctrine:mapping:import would generate orm.yml files of my database which it did without a hitch. However, I also see that only ManytoOne relationships are generated in the yml files ... not any other kind of associations.
My statement was:
$ php app/console doctrine:mapping:import –em=buv DBImportTestBundle yml
Also, I did a generate entities to create classes and basic CRUD for each table using:
$ php app/console doctrine:generate:entities DBImportTestBundle
This also worked EXCEPT that I do not see any annotated associations generated in the doc blocks for any of the entity properties.
I'm looking through the docs but do not see any specific information on the exact requirements for associations on imported dbs. It could be I'm not looking in the right place.
I'm trying to determine the most efficient way to maintain my db schema within symfony/doctrine ... My understanding was that I would need to explicitly define certain associations manually but I'm not sure what the exact requirements would be OR if perhaps I'm simply not passing in the correct arguments to create my annotated associations via generate:entities.
Can someone point me to any docs that refer to what I'm talking about or explain the proper approach to defining complex associations within doctrine? Thank you.
$ php app/console doctrine:mapping:convert annotation ./src to generate entity classes with annotation mappings, before running :
$ php app/console doctrine:generate:entities DBImportTestBundle
there is a cookbook for that

Doctrine Skip entities when creating database

I have a Symfony2 application, with a bundle that have a couple of entities that isn't part of my main application.
How can I update the schema but exclude some of the entities?
E.G when I run the following command after updating one of my entities
php app/console doctrine:schema:update --dump-sql
then doctrine generates sql for all the entities, which means it creates tables for the entities that I don't need. So how can I tell doctrine to skip those specific entities when generating the sql?
The Symfony command doesn't allow that way to exclude any entity.
If you want to differenciate enities from main part to the others, I suggest you define 2 different entity managers, 1 for main entites, 1 other for your annexed entities...
Official doc : How to work with Multiple Entity Managers

Resources