【问题标题】:Symfony3.4 Using two databases in one ApplicationSymfony3.4 在一个应用程序中使用两个数据库
【发布时间】:2018-08-10 22:38:21
【问题描述】:

我尝试使用 symfony 3.4 构建一个访问两个不同数据库的应用程序。

https://symfony.com/doc/3.4/doctrine/multiple_entity_managers.html

在本文档中,我看到我必须像这样配置我的 config.yml:

doctrine:
  dbal:
    default_connection: default
    connections:

        default:
            driver: pdo_mysql
            host: '%database_host%'
            port: '%database_port%'
            dbname: '%database_name%'
            user: '%database_user%'
            password: '%database_password%'
            charset: UTF8

        employeedb:
            driver:   pdo_mysql
            host:     '%database_host%'
            port:     '%database_port%'
            dbname:   '%database_name2%'
            user:     '%database_user%'
            password: '%database_password%'
            charset:  UTF8

  orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            mappings:
                AppBundle:  ~
        employeedb:
            connection: employeedb
            mappings:
                EmployeeBundle:  ~

我为 EmployeeBundle 生成了第二个捆绑包。 现在,如果我尝试通过简单的查询从 seccond 数据库中获取值,它工作正常:

$em = $this->getDoctrine()->getManager('employeedb'); <br/>
$RAW_QUERY = "SELECT * FROM employee";

如果我尝试这样的教义,我会得到一个错误(ORMException):

$employees = $this->getDoctrine()
                  ->getRepository('EmployeeBundle:Employee')
                  ->findAll();

未知实体命名空间别名“EmployeeBundle”。

我的配置有什么问题?我应该在我的 entity_manager 员工数据库中添加一些东西吗?

【问题讨论】:

  • 没人能帮我吗?

标签: database entitymanager symfony-3.4


【解决方案1】:

解决方法是在教义命令中给出新实体管理器的名称:

     $results = $this->getDoctrine()
                    ->getManager('employeedb')
                    ->getRepository('EmployeeBundle:Employee')
                    ->findAll();

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多