Creating a single object from an existing database using symfony2 and the doctrine

Can I create a single object from a database using the Symfony2 console tool?

In the middle of coding, I had to add a table and make changes to existing entity classes. Therefore, I do not want all my essences to be reborn.

Any suggestions would be appreciated!

+55
database php symfony doctrine
Apr 29 2018-12-12T00:
source share
10 answers

I had the same problem, you should do this:

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 you want to generate (e.g. xml, yml, annotation)

And finally

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

Like this doctrine, only the entity you need will be loaded. Just be careful on the filter you should use CamelCase!

Hope this helps you

+100
May 16 '12 at 8:34
source share

For the third team, the doctrine continued to restore all Entity files. By adding the name of the object after the package, he only created the object that interested me.

 php app/console doctrine:generate:entities AppMyBundle:Yourtablename --no-backup 
+28
Nov 02
source share

A simple working solution for annotating Symfony 2.7 parameters and for [/ xml / yml] see http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

complete 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 the doctrine to find the name of your table. This is because the doctrine will always underline and add a cale to your table name. If not, you will get this error: "There is no matching information in the database.")

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

( NOTE: make sure you have the namespace AppBundle\Entity; as shown below in the Meeting.php file, for example:

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

Where:

  • AppBundle is exactly your "AppBundle" in Symfony 2.7
  • Meeting - Target Table (Camel Sensitive)

MAKE SURE check this directory:

CSI \ AppBundle / Resources / Configuration / Doctrine / Meeting.orm.xml

AND MAKE SURE that you only have .xml files for the table in which you want to create entity class files, while others do not. Then run this command below to generate the get and set methods for your entity class that you created earlier

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

Note 2: As a last step, you should remove the xml doctrine of the orm db file, for example, src\AppBundle/Resources/config/doctrine/VisitorData.orm.xml

This works very well for me.

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

+12
Aug 14 '15 at 5:46
source share

@Fyrye's comment, which is currently hidden, deserves credit, wanted to add this so he doesn't miss the 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 had problems running the following command due to the large number of poorly defined obsolete 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 reads metadata from all of your tables, which, if they don't have primary keys or have some other problem, will cause the command to fail

+8
Feb 18 '15 at 10:12
source share

None of the answers work for me using Symfony 3 . I've finished:

 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 
+5
Mar 12 '17 at 4:42 on
source share

I would leave this as a comment on the accepted answer, but I'm a newbie.

For people like me who have had problems with the -filter switch displaying multiple tables with matching strings in the names, you can use the template.

Examples of table names:

Vendor VendorContact

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

This command converts both tables, not just Vendor. If you need only Vendor and not VendorContact, use the template in the -filter file:

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

Hope this helps someone!

+3
Dec 19 '15 at 5:00
source share

Works well with symfony 3.

If you get "No metadata classes to process." message try to convert your name to a camel frame in the filter doctrine in the filter parameter.

"my_table_name" must be written as "MyTableName".

+3
Mar 29 '17 at 16:48
source share

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

None of the workarounds posted above worked for me.

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

Then the connection to this new database changed and the command to extract the entity from there was executed.

This seems to be a little radical, but I will not create objects manually.

Hope that helps

+1
Oct 30 '15 at 13:39
source share

None of them work for my symfony 3.3. So I just made a copy of the directory and regenerated all the entities in the copy directory. Then I copied the required objects in my source directory.

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

0
Aug 23 '17 at 5:09 on
source share

for symfony 3

To generate entities for the 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

0
Jul 05 '19 at 12:24
source share



All Articles