名为“db2”的Symfony2 Doctrine ORM Manager不存在

时间:2016-06-23 13:35:42

标签: php symfony orm doctrine-orm doctrine

我创建yml实体,生成实体并配置多个连接

config.yml

# Doctrine Configuration
doctrine:
dbal:
    default_connection: db1
    connections:
        db1:
            driver:   '%database_driver%'
            host:     '%database_host%'
            port:     '%database_port%'
            dbname:   '%database_name%'
            user:     '%database_user%'
            password: '%database_password%'
            charset:  UTF8
        db2:
            driver:   '%database_driver2%'
            host:     '%database_host2%'
            port:     '%database_port2%'
            dbname:   '%database_name2%'
            user:     '%database_user2%'
            password: '%database_password2%'
            charset:  UTF8
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

我的orm.yml

TestBundle\Entity\Test:
type: entity
table: Test
id:
    id:
        type: integer
        nullable: false
        options:
            unsigned: true
        id: true
fields:
    name:
        type: string
        nullable: false
        length: 255
        options:
            fixed: false
lifecycleCallbacks: {  }

在清理缓存并生成实体后,我尝试运行此代码(在命令控制器中)

<?php 

...

protected function execute(InputInterface $input, OutputInterface $output)
{
    $doctrine = $this->getContainer()->get('doctrine')->getManager('db2');
    //both methods cant work :( db2_entity_manager not found
    $doctrine = $this->getContainer()->get('doctrine.orm.db2_entity_manager');

    $test = $doctrine->getRepository('DionisDataBaseBundle:Test');

}

我有错误:

  

[InvalidArgumentException]
    名为“db2”的Doctrine ORM Manager不存在。

2 个答案:

答案 0 :(得分:1)

将config.yml更改为

 orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    default_entity_manager: db1
    entity_managers:
        db1:
            connection: db1
            mappings:
                DionisDataBaseBundle: ~
        db2:
            connection: db2
            mappings:
                DionisDataBaseBundle: ~
        db3:
            connection: db2
            mappings:
                DionisDataBaseBundle: ~

答案 1 :(得分:0)

你刚从我的一个项目中声明了连接而没有实体经理:

doctrine:
    dbal:

        default_connection: default
        connections:
            default:
                host:     "%database_host%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                driver_class:  Realestate\MssqlBundle\Driver\PDODblib\Driver
                mapping_types:
                    enum: string
                    string: string
                # schema_filter: ~^(sf_fos_user)~
            prod:
                host:     *****
                dbname:   ****
                user:     "%database_user%"
                password: "%database_password%"
                driver_class:   Realestate\MssqlBundle\Driver\PDODblib\Driver
                mapping_types:
                    enum: string
                    string: string
                # schema_filter: ~^(sf_fos_user)~

    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    AppBundle: ~
                    Bundle2: ~
            db2:
                connection: prod
                mappings:
                    otherBundle: ~
        auto_generate_proxy_classes: "%kernel.debug%"