I want to generate entity classes from a database for my symfony application. So I followed the following three steps (From How to generate Entities from an Existing Database)
1. php app/console doctrine:mapping:convert yml ./src/Acme/BlogBundle/Resources/config/doctrine/metadata/orm --from-database --force
2. php app/console doctrine:mapping:import AcmeBlogBundle yml
3. php app/console doctrine:generate:entities AcmeBlogBundle
For knowing the working of these three commands, I just removed all files from ./src/Acme/BlogBundle/Resources/config/doctrine/metadata/orm. I added one more field in one table. After that I followed command-2 and command-3.
I checked the modified table's entity for the new field. The new field was present in the entity.
Then why should we use the doctrine:mapping:convert command in order to generate entities from an existing database?
I don't quite understand what you guys are saying there, because for me the 3 commands are needed and different between themselves.
If you want to introspect a DB schema to generate the needed symfony ORM files and entities then you must use the 3 commands.
Here is what they do: 1)
php app/console doctrine:mapping:convert yml ./src/< vendor>/< bundle_name>/Resources/config/doctrine/metadata/orm --from-database --force
This builds entity mapping files from the DB by introspection.For each table on the DB this will merely generate an < entity>.orm.yml in:
< proj_folder>/src/< vendor>/< bundle_name>/Resources/config/doctrine/metadata/orm/< entity>.orm.yml
2)
php app/console doctrine:mapping:import < vendor>< bundle_name> annotation**
Generate entity classes for each table introspected from DB:
OUTPUT: writing /var/www/html/< project_folder>/src/< vendor>/< bundle_name>/Entity/< entity>.php
3)
php app/console doctrine:generate:entities < vendor>< nameBundle>**
This only generate getters and setters for all entity class properties.
What I know, and as I explained it in this post, mapping:convert can't resolve namespaces for relations in your model. That's why you then need to process mapping:import and get your final mapping files. After mapping:import has been processed, you can delete YourBundle/Resources/config/doctrine/metadata/orm
directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With