Home > OS >  SYMFONY 6 - DOCTRINE : mapping and import only some table from an existing databse
SYMFONY 6 - DOCTRINE : mapping and import only some table from an existing databse

Time:03-03

I'm actually looking to map and import an existing database into a symfony 6 project.

I know we can do this by using this command :

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

But, this database is very huge and have a lot of tables. I don't want them all.

Do you know a way to "select" the tables i want to map. I know the tables that i don't want start with " _ " or " inv_ ". Perhaps there is a way to have a "where" clause ?

CodePudding user response:

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:

  1. 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)
  2. drop all other tables which you don't want
  3. rename some, if you think, their names wouldn't fit to doctrine's naming convention.
  4. switch to that copy-database in your .env (change db name in DATABASE_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

  • Related