Importing tables from external database in Symfony2 with doctrine

Viewed 28116

I have a Symfony2 project with its own database, and now I want to connect to another database (another project) so I can modify some tables.

I created the new connection in config_dev.yml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_mysql
                host:     localhost
                dbname:   database1
                user:     root
                password: 
            buv:
                driver:   pdo_mysql
                host:     localhost
                dbname:   database2
                user:     root
                password:

I tried to import the schema with the following command:

$ php app/console doctrine:mapping:import --em=buv MyBundle yml

[Doctrine\DBAL\Schema\SchemaException] Index '' does not exist on table 'old_table'

But some of the tables in database2 have no PKs! And the full import dosn't work. But I only want to import two tables, so I tried:

$ php app/console doctrine:mapping:import --em=buv --filter="tablename" MyBundle yml

But I'm getting the same error, seems that --filter isn't working.

The documentation in the console command doctrine:mapping:import only says to put the entity name in the filter option. But I don't have an entity yet.

9 Answers

At the DatabaseDriver.php file reverseEngineerMappingFromDatabase function you can change

throw new MappingException("Table " . $table->getName() . " has no primary key. Doctrine does not "."support reverse engineering from tables that don't have a primary key.");

with

if(! $table->hasColumn('id')){
   $table->addColumn('id', 'integer', array('autoincrement' => true));
}
   $table->setPrimaryKey(array('id'));

Try at least temporary change your doctrine config

doctrine:
    dbal:
        schema_filter: ~^(?!table1|table2)~

And run:

php bin/console doctrine:mapping:convert --force --from-database --filter="CamelCasedTableName" --namespace="App\Entity\\" annotation   .

Spent a lot of hours finding this solution. Stange but upper answer by Courtney Miles didn't work for me. And this one works fine at least on symfony 4.4

Related