Symfony 2.8两个数据库连接

问题描述 投票:0回答:1

我需要连接到外部数据库,我只需要在一个表中进行一个简单的查询,所以我认为不需要创建新的实体管理器。我相信所有配置都正确设置,但是我仍然无法连接到新数据库。所以我缺少了一些东西却找不到,这里是我的文件:

# Doctrine Configuration
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
                # if using pdo_sqlite as your database driver:
                #   1. add the path in parameters.yml
                #     e.g. database_path: '%kernel.root_dir%/data/data.db3'
                #   2. Uncomment database_path in parameters.yml.dist
                #   3. Uncomment next line:
                #path: '%database_path%'

            database2:
                driver: pdo_mysql
                host: '%database2_host%'
                port: '%database2_port%'
                dbname: '%database2_name%'
                user: '%database2_user%'
                password: '%database2_password%'
                charset: UTF8

    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

parameters:
#Set_Goals Database
    database_host: 127.0.0.1
    database_port: null
    database_name: set_goals
    database_user: root
    database_password: null

#Database2 Database
    database2_host: 127.0.0.1
    database2_port: null
    database2_name: second
    database2_user: root
    database2_password: null
Repository:
public function getAccounts(){
    $conn = $this->getEntityManager()->getConnection('database2');
    $sql = 'SELECT * FROM leme_account';
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    return $stmt->fetchAll();
}

错误:执行“ SELECT * FROM leme_account”时发生异常:

SQLSTATE [42S02]:找不到基本表或视图:1146表'set_goals.leme_account'不存在

set_goals数据库是默认连接。

[在此先感谢您,我真的很想念一些简单的东西,但是我对Symfony和程序设计还是陌生的,我在这里遵循了文档以及一些相关的问题,但是不能使它正常工作。

symfony doctrine
1个回答
0
投票

我认为您需要创建另一个实体经理

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
                # if using pdo_sqlite as your database driver:
                #   1. add the path in parameters.yml
                #     e.g. database_path: '%kernel.root_dir%/data/data.db3'
                #   2. Uncomment database_path in parameters.yml.dist
                #   3. Uncomment next line:
                #path: '%database_path%'

            database2:
                driver: pdo_mysql
                host: '%database2_host%'
                port: '%database2_port%'
                dbname: '%database2_name%'
                user: '%database2_user%'
                password: '%database2_password%'
                charset: UTF8

    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

        default_entity_manager: default
            entity_managers:
                default:
                    connection: default
                database2:
                    connection: database2

然后输入您的代码

$em = $this->get('doctrine')->getManager();
$em2 = $this->get('doctrine')->getManager('database2');

文档中提到了映射键,但我不知道它是否是必需的。

参考:https://symfony.com/doc/2.8/doctrine/multiple_entity_managers.html

© www.soinside.com 2019 - 2024. All rights reserved.