Symfony Doctrine import "join" table from database - symfony

I would like to import existing tables from database into Symfony project (realise them as entities).
I used commands:
php app/console doctrine:mapping:import --force BundleName yml
php app/console doctrine:mapping:convert annotation ./src
php app/console doctrine:generate:entities BundleName
The commands worked fine and the problem is that any "join" table (foreign keys in place) will be missing,
e.g. If I have table Order, Item and OrderItem, where OrderItem contains the mapping of Order and Item, then Symfony/Doctrine resolves Order and Item into a many to many relation and creates entities for Order and Item only.
Question is how can I make doctrine to import the "join" table as well and resolve it into two many to one relations with respect to the target tables.

Doctrine won't be able to guess the relations exactly. It have even seen imports fail because some tables did not contain primary keys. One to many / many to many relations are defined by the way it is used and enforced by constraints.
The import is a run-once facility. You will have to check and update the mapping yourself afterwards.

Related

Symfony flag-toggling entities

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)~'

make entities with multiple databases

I have to do large refactoring/enhancements on web apps. We decided to use Symfony4.
I want to define 2 (or more) databases: the old one and the new one.
(In the future, I think to have more location, person databases common at several web apps)
In my researches, I use Multiple Entity Managers, create my databases as mentionned, then create my src/Entity/Main and src/Entity/Customer folders.
Then, I want to create my entities, especially new one with php bin/console make:entity but it creates files in Entity folder, not in Entity/Main (default) or Customer and returns
[ERROR] Only annotation mapping is supported by make:entity
Are there any solution to use this make:entity command or should I define all entity files myself?
Nota: I don't put my config/packages/doctrine.yaml, it is the same as in help sample except server_version: '5.6' to be compliant with my MariaDB version
The answer to create Product entity in Main is:
php bin/console make:entity Main\\Product
and to create Customer entity in Customer is:
php bin/console make:entity Customer\\Customer

Symfony, how to keep the declaration of entities repository after re-import my table

The structure of the database I am using for my project is constantly changed by another team. So I have to import regularly this structure into Symfony. To do so I use the following command:
php app/console doctrine:mapping:import --force EgBundle yml [--filter="Table"]
php app/console doctrine:generate:entities [Company/EgBundle/Entity/Table]
Every time I run those command I loose the declaration of my repositories and I have to had them manually:
repositoryClass: Company\EgBundle\Entity\TableRepository
Is there any way to keep this declaration?
There is no way for Doctrine to know what the repository class shall be when importing a mapping from database. The information can't be obtained from the database schema.
A possible solution for your use-case would be extending\overriding the doctrine:mapping:import command to add a Repository to the mapping information automatically using a naming convention.

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