duankeye2342 2014-03-11 23:45
浏览 45

在Symfony2中创建数据库

I've inherited a Symfony project that uses a MySQL DB. I need to create a second database. I've changed the …/htdocs/projectFolder/app/config/parameters.yml to have a new dbName.

parameters:
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: 8889
    database_name: inherited_db_name
    database_user: root
    database_password: root
    mailer_transport: smtp
    mailer_host: localhost
    mailer_user: null
    mailer_password: null
    locale: en
    secret: nosecret
    database_path: null

Changed to:

parameters:
    database_driver: pdo_mysql
    database_host: 127.0.0.1 #can't use "localhost"
    database_port: 8889
    database_name: new_db_to_create
    database_user: root
    database_password: root
    mailer_transport: smtp
    mailer_host: localhost
    mailer_user: null
    mailer_password: null
    locale: en
    secret: nosecret
    database_path: null

I have tried all the tricks I can thing of but the command: doctrine:database:create just keeps trying to create “inherited_db_name”

If I drop the “inherited_db_name” DB and then run the command again w/ the new parameters.yml the “inherited_db_name” is recreated. I've searched and grep-ed for the “inherited_db_name” but cannot find out where it's coming from. ANY suggestions would be welcomed.

  • 写回答

1条回答 默认 最新

  • dtvam48220 2014-03-12 15:51
    关注

    You should configure your Symfony2 application to manage both databases.

    So your parameters.yml file will look like:

    parameters:
        database_driver: pdo_mysql
        database_host: 127.0.0.1
        database_port: 8889
        database_name: inherited_db_name
        database_user: root
        database_password: root
        database_driver2: pdo_mysql
        database_host2: 127.0.0.1 #can't use "localhost"
        database_port2: 8889
        database_name2: new_db_to_create
        database_user2: root
        database_password2: root
        mailer_transport: smtp
        mailer_host: localhost
        mailer_user: null
        mailer_password: null
        locale: en
        secret: nosecret
        database_path: null
    

    Now you just need to configure Doctrine properly:

    doctrine:
        dbal:
            default_connection: default
            connections:
                default:
                    driver:   "%database_driver%"
                    host:     "%database_host%"
                    port:     "%database_port%"
                    dbname:   "%database_name%"
                    user:     "%database_user%"
                    password: "%database_password%"
                    charset:  UTF8
                customer:
                    driver:   "%database_driver2%"
                    host:     "%database_host2%"
                    port:     "%database_port2%"
                    dbname:   "%database_name2%"
                    user:     "%database_user2%"
                    password: "%database_password2%"
                    charset:  UTF8
    
        orm:
            default_entity_manager: default
            entity_managers:
                default:
                    connection: default
                    mappings:
                        AcmeDemoBundle:  ~
                        AcmeStoreBundle: ~
                customer:
                    connection: customer
                    mappings:
                        AcmeCustomerBundle: ~
    

    Please take a look to the Symfony2 cookbook: http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html

    评论

报告相同问题?