Search code examples
phpsymfonydoctrine

Importing tables from external database in Symfony2 with doctrine


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.


Solution

  • If I get you correctly, you want to import your existing database?

    What I do is:

    php app/console doctrine:mapping:convert xml ./src/App/MyBundle/Resources/config/doctrine/metadata/orm --from-database --force
    

    Then do a selective convert to annotation:

    php app/console doctrine:mapping:import AppMyBundle annotation --filter="users_table"
    

    If you wanted to yml, change annotation to yml.

    warning: when you import to annotation or yml, it will delete your current entity file.