Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4, a way for generate Entities from an Existing Database?

With Symfony 3 and its console, we can generate entities from an already existing database via the command "php bin/console doctrine:mapping:import" (very usefull !).

From symfony 4, the command "./bin/console doctrine:mapping:import" needs a bundle name but symfony 4 doesn't work with bundle now.

With the new version of symfony, is there a way I didn't see for generate entities from an existing Database (mysql by example) ? Or must I wait a new version of doctrine for have a "doctrine:mapping:import" compatible with Symfony 4 ?

I found a(n) (ugly) solution yet. I deploy a disposable symfony 3, I link the symfony 3 to my database and I generate entities in a bundle. Then I copy generates files to symfony 4. It's ugly but it works haha

like image 589
spacecodeur Avatar asked Dec 07 '17 05:12

spacecodeur


People also ask

How should be the process to add a new entity to the app in Symfony?

With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.

What are Symfony entities?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.


1 Answers

You can use

php bin/console doctrine:mapping:convert --from-database annotation ./src/Entity

which should create the entities based on the database setting. Don’t forget to add the namespaces, and you will still need to add the getters and setters, but the bulk of the properties, including annotations and some of the relationships are already included. (Source)

Please also note, that Doctrine will not support this anymore in the next Doctrine version. As written in the Symfony docs

Moreover, this feature to generate entities from existing databases will be completely removed in the next Doctrine version.

like image 121
Oliver Adria Avatar answered Oct 24 '22 18:10

Oliver Adria