10

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?

1

4 Answers 4

16

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

Sign up to request clarification or add additional context in comments.

1 Comment

In the final command, I was able to use the shortcut MyBundle:MyTable in lieu of Path\To\MyBundle\EntityFolder\\MyTable
6

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

1 Comment

This will work for a default entity manager.If you have to pull out the data from a different database then provide the below param --em="ENTITY MANGER NAME"
2
php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.