Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate entity from existing table in symfony2?

Tags:

php

symfony

I have table "my_table" with some fields. I want generate Entity in MyBundle used "my_table". But I don't want recreate all entities in MyBundle. How can I do this?

like image 535
Alastor Avatar asked Nov 15 '12 09:11

Alastor


4 Answers

Here is the way you can do it,

First step, ask Doctrine to introspect the database and generate the corresponding xml or yml metadata files.

php app/console doctrine:mapping:convert [xml|yml] Path/To/MyBundle/Resources/config/doctrine/metadata/orm --from-database --force --filter=MyTable

Second step, ask Doctrine to import the schema and build related entity classes by executing the following two commands.

php app/console doctrine:mapping:import MyBundle [xml|yml|annotation] --filter=MyTable

php app/console doctrine:generate:entities Path\To\MyBundle\EntityFolder\\MyTable

Take a look at the How to generate Entities from an Existing Database section of the documentation

like image 131
Ahmed Siouani Avatar answered Oct 20 '22 16:10

Ahmed Siouani


Although this is an old post but if someone gets following error,

Database does not have any mapping information.

Check

If your table name is blog_post then in filter option use BlogPost and not blog_post

Reference: https://stackoverflow.com/a/27019561/6504104

though this is covered by answers above but I missed it and was getting this error

So I wanted to make this explicit

Also in symfony >= 3.4 its' php bin/console e.g.

php bin/console doctrine:mapping:import --force AppBundle xml --filter="BlogPost"

and then

php bin/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="BlogPost"

Thanks...

like image 41
Azhar Khattak Avatar answered Oct 20 '22 12:10

Azhar Khattak


Simple Working Solution for Symfony 2.7 option annotation and for [/xml/yml] see http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

do 3 commands in 3 steps:

Command #1:

$ php app/console doctrine:mapping:import --force AppBundle xml --filter="Meeting"

Output:

writing C:\xampp\htdocs\localxyz\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml


Command #2:

$ php app/console doctrine:mapping:convert annotation ./src/AppBundle/Entity --from-database --filter="Meeting"

Output:

Processing entity "Meeting"

Exporting "annotation" mapping information to "C:\xampp\htdocs\localxyz\src\Entity"


Command #3:

$ php app/console doctrine:generate:entities AppBundle:Meeting --no-backup

Output:

Generating entity "AppBundle\Entity\Meeting" generating AppBundle\Entity\Meeting

where:

AppBundle is exactly your "AppBundle" in 2.7 symfony Meeting is the target table (case sensitive)

TO BE SURE, check this directory:

C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/Meeting.orm.xml

C:\xampp\htdocs\myproj\src\AppBundle/Resources/config/doctrine/MeetingOriginal.orm.xml

AND MAKING SURE you only have .xml files for the table you want to create entity class files and no others.

It works very well for me.

For explanation please read: http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

like image 6
Dung Avatar answered Oct 20 '22 14:10

Dung


php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity
like image 2
Ondrej Slinták Avatar answered Oct 20 '22 16:10

Ondrej Slinták