Adrien.B
Adrien.B

Reputation: 53

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

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 ?

Upvotes: 3

Views: 9572

Answers (3)

stloc
stloc

Reputation: 1548

--filter does not work anymore in case of DB Oracle for mee (SF6). So I modify temporarily vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php

foreach ($this->sm->listTables() as $table) {
            $tableName   = $table->getName();
            if (str_contains($tableName, 'XXXXX')) {

Upvotes: 1

Hugo Trial
Hugo Trial

Reputation: 396

If you happen to have a diagram of your database done with MySQL Workbench, you can use these tools to export your diagram into working entities :

First you setup this with composer: https://github.com/mysql-workbench-schema-exporter/mysql-workbench-schema-exporter And then this : https://github.com/mysql-workbench-schema-exporter/doctrine2-exporter

Then you run the following command to get to know more how to use it :

php composer require --dev mysql-workbench-schema-exporter/doctrine2-exporter

You have plenty of options to parameter desired output, entities namespace and so on, everything is described in the second github rep if you want to use the doctrine2 ORM export format.

Upvotes: 3

V-Light
V-Light

Reputation: 3115

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

Upvotes: 1

Related Questions