Generating a single Entity from existing database using symfony2 and doctrine

PhpDatabaseSymfonyDoctrine

Php Problem Overview


Is it possible to generate a single entity from database using the Symfony2 console tool?

In the middle of coding I had to add a table and there are modifications made to the existing entity classes. So I don't want all my entities regenerated.

Any suggestions will be appreciated!

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

@fyrye's comment that is currently hidden deserves the credit, wanted to add this so it's not missed by others. This is the approach:

/** @var \Doctrine\DBAL\Connection $connection */
$config = $connection->getConfiguration();

// for excluding an specific table
$config->setFilterSchemaAssetsExpression('/^(table_to_reverse_engineer_1|table_to_reverse_engineer_2).*$/');

source: https://coderwall.com/p/jofhdw/doctrine-tell-which-tables-to-work-with

I was having issues when running the following command because of large number of badly defined legacy tables

php ./vendor/bin/doctrine orm:convert-mapping --force --from-database annotation ./src/UI/Entity/

It turns out that the --filter flag only filters AFTER it has read meta data from all of your tables which, if they don't have primary keys or have some other issue, will cause the command to fail

Solution 5 - Php

None of the answers were quite right for me using Symfony 3. I ended up doing:

php bin/console doctrine:mapping:import --force MyBundle xml --filter="MyTable"

php bin/console doctrine:mapping:convert annotation ./src --filter="MyTable"

php bin/console doctrine:generate:entities MyBundle:MyTable --path ./src

Solution 6 - Php

I would have left this as a comment to the accepted answer but I'm a newbie.

For those like me who had trouble with the --filter switch mapping multiple tables with coincident strings in names, one can use a pattern.

Example table names:

Vendor VendorContact

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

That command will convert both tables rather than just Vendor. If you want just Vendor and not VendorContact, use a pattern in --filter:

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

Hope that helps someone!

Solution 7 - Php

Works great with Symfony 3 too.

If you are getting "No Metadata Classes to process." message try convert your tablename to Doctrine camel casing in the filter parameter.

"my_table_name" needs to be write as "MyTableName".

Solution 8 - Php

--filter works with entity name, not table name ! php bin/console doctrine:mapping:import "App\Entity" annotation --path=config/doctrine --filter="YourEntity"

Solution 9 - Php

for Symfony 3

To generate the entities for a new "Group" table

php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/AppBundle/Entity --filter Group

enter image description here

as written in the symfony 3 documentation

Solution 10 - Php

I had exactly the same issue with Symfony 2.4 and MySQL.

None of the workarounds posted above worked for me.

I ended up creating a new database with the tables I want to extract (this can be easy to do because MySQL provides the creation script).

Then changed the connection to that new database and executed the entity extraction command from there.

Seems to be a bit radical, but I will not create the entities by hand.

Hope that helps

Solution 11 - Php

Didn't work any of these for my symfony 3.3. So I just created a copy of directory and re-generated all of the entities in copy directory. Then I copied required entities in my original directory.

--filter does not work due to this issue https://github.com/symfony/symfony/issues/7717

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSethunath K MView Question on Stackoverflow
Solution 1 - PhpSnrokiView Answer on Stackoverflow
Solution 2 - PhpjpbView Answer on Stackoverflow
Solution 3 - PhpDungView Answer on Stackoverflow
Solution 4 - PhpCarltonView Answer on Stackoverflow
Solution 5 - PhpBrett ThomasView Answer on Stackoverflow
Solution 6 - PhppeejView Answer on Stackoverflow
Solution 7 - PhpIvo NecchiView Answer on Stackoverflow
Solution 8 - Phpuser13424261View Answer on Stackoverflow
Solution 9 - Phpmax4everView Answer on Stackoverflow
Solution 10 - PhpNicolasView Answer on Stackoverflow
Solution 11 - Phphanish singlaView Answer on Stackoverflow