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
Generate single Symfony2 entity from an existing table
After finding that the --filter
does not do what I hoped, I scripted tbl_remote
and created it in my test database. I then changed parameters.yml
to look at the test database and after running: php app/console doctrine:mapping:import --filter="tbl_remote" AppBundle
i was greeted with: "Database does not have any mapping information."
This is not my day so I just ran: php app/console doctrine:mapping:import AppBundle
and removed the .orm.xml that I did not need.
I was then able to run: php app/console doctrine:generate:entities AppBundle
to generate the single entity!! Not what I wanted but not a complete loss.
Symfony 4, a way for generate Entities from an Existing Database?
You can use
php bin/console doctrine:mapping:convert --from-database annotation ./src/Entity
which should create the entities based on the database setting. Don’t forget to add the namespaces, and you will still need to add the getters and setters, but the bulk of the properties, including annotations and some of the relationships are already included. (Source)
Please also note, that Doctrine will not support this anymore in the next Doctrine version. As written in the Symfony docs
Moreover, this feature to generate entities from existing databases will be completely removed in the next Doctrine version.
Create Single Entity on Doctrine 2 + Symfony 2
php app/console doctrine:mapping:import --force AcmeBlogBundle:YOURENTITYNAME yml
SYMFONY 6 - DOCTRINE : mapping and import only some table from an existing databse
There is a --filer=
option in doctrine:mapping:import
but I think it's not what you're looking for.
If you migrate your codebase to symfony and doctrine and doctrineORM wasn't used before - it would be much easier just to start over and do all by your self. Yes, it's tedious, especially with a huge database, but you will end up with much "cleaner" entities and during this phase you could decide which tables to ignore.
But if you still want try to "import" somehow, consider following steps:
- In your local developer environment, create a copy of your database. Just the schema without data. So you have all tables, but they're empty (how to with mysqldump Don't forget to use
--no-data
) - drop all other tables which you don't want
- rename some, if you think, their names wouldn't fit to doctrine's naming convention.
- switch to that copy-database in your
.env
(change db name inDATABASE_URL
)
Now try to import again with doctrine:mapping:import
. You may need to adjust some tables by repeating step 2) and 3) and then try to import again.
If import succeed, and you have a bunch of entities, now comes the boring and tedious part. You have to manually check all classes in src/Entity
.
Depending on your Database (mysql, postgreSQL, sqlite, etc) not all column-types will be exact what you want.
Furthermore, most many-to-one/one-to-many
relations and all many-to-many
junction tables will be probably converted to standalone Entities like src\CategoryToProduct.php
- which isn't right. So you have to delete them and recreate your relations by hand
Symfony 2 - Doctrine, how to generate entities from database
try this please:
php bin/console doctrine:mapping:import --force AppBundle xml --filter="Yourtablename"
Related Topics
Running Composer Returns: "Could Not Open Input File: Composer.Phar"
How to Pass Arguments to My Function Through Add_Action
How to Use Class Methods as Callbacks
Turn Off Display Errors Using File "Php.Ini"
Multiple Submit Buttons PHP Different Actions
How to Get the Newest File in a Directory in PHP
Inserting Google Calendar Entries with Service Account
Handling Big User Ids Returned by Fql in PHP
Parse JSON String Contents into PHP Array
Prevent Nginx 504 Gateway Timeout Using PHP Set_Time_Limit()
How to Get the Subversion Revision Number in PHP
PHP Function Use Variable from Outside
Structure of a Serialized PHP String
How to Remove the Url Protocol and Slash from User Input in PHP
How to Use "Root" Namespace of PHP