Generating a single Entity from existing database using symfony2 and doctrine Generating a single Entity from existing database using symfony2 and doctrine symfony symfony

Generating a single Entity from existing database using symfony2 and doctrine


I had the same problem, you've to do this way:

php app/console doctrine:mapping:convert metadata_format \    ./src/App/MyBundle/Resources/config/doctrine \    --from-database \    --filter="Yourtablename"

Then

php app/console doctrine:mapping:import AppMyBundle \    metadata_format --filter="Yourtablename"

Where metadata_format is the file ending you want to generate (e.g. xml, yml, annotation)

And finally

php app/console doctrine:generate:entities AppMyBundle --no-backup

Like this doctrine will load only the entity you need. Just be carefull on the filter you must use the CamelCase !

Hope this will help you


For the third command, doctrine kept regenerating all of the Entity files. By adding the entity name after the bundle, it only generated the entity I was interested in.

php app/console doctrine:generate:entities AppMyBundle:Yourtablename --no-backup


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:

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

(NOTE: if your database name is my_meeting You will need to change it to MyMeeting in filter="MyMeeting" for doctrine to find your table name. This is because doctrine will always strip underscores and add Camel-case to your table name. If not, you will get this error: "Database does not have any mapping information".)

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

(NOTE: making sure you have namespace AppBundle\Entity; as below in your Meeting.php class file like this:

<?php/*** Created by PhpStorm.* User:* Date: 03-Sep-2015* Time: 3:23 PM*/namespace AppBundle\Entity;use Doctrine\ORM\Mapping as ORM;

If not add it in.)

where:

  • AppBundle is exactly your "AppBundle" in Symfony 2.7
  • Meeting is the target table (Camel-Case sensitive)

TO BE SURE, check this directory:

src\AppBundle/Resources/config/doctrine/Meeting.orm.xml

AND MAKING SURE you only have .xml files for the table you want to create entity class files and no others. Then run this below command to generate get and set methods for your entity class that you created previously

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

NOTE2:As the last step you must delete the xml doctrine orm db file in for example src\AppBundle/Resources/config/doctrine/VisitorData.orm.xml

It works very well for me.

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