简体   繁体   English

Symfony2中的多个数据库

[英]Multiple databases in Symfony2

I have an application with two database connections. 我有一个具有两个数据库连接的应用程序。 First database is application's own database and second database is from another application (same server, different subdomain). 第一个数据库是应用程序自己的数据库,第二个数据库来自另一个应用程序(同一服务器,不同的子域)。

For SQL queries on the second database I use services. 对于第二个数据库上的SQL查询,我使用服务。

services.yml services.yml

login.company:
    class: JP\CoreBundle\Service\Login\CompanyService
    arguments:
        em: "@doctrine.orm.login_entity_manager"

CompanyService.php CompanyService.php

class CompanyService {    
    private $_em;

    public function __construct(EntityManager $em){
        $this->_em = $em;
    }

    public function getSomethingBySomething($something){ // Intentionally obscured method and parameter name
        $conn = $this->_em->getConnection();

        $sql = ''; // Intentionally removed SQL query

        $stmt = $conn->prepare($sql);
        $stmt->bindParam('something', $something); // Intentionally obscured real name to something
        $stmt->execute();

        return ($stmt->rowCount() == 1) ? $stmt->fetch(Query::HYDRATE_ARRAY) : false;
    }
}

config.yml config.yml

doctrine:
    dbal:
        default_connection: analysis
        connections:
            analysis:
                driver:   "%database_driver1%"
                host:     "%database_host1%"
                port:     "%database_port1%"
                dbname:   "%database_name1%"
                user:     "%database_user1%"
                password: "%database_password1%"
                charset:  UTF8
            login:
                driver:   "%database_driver2%"
                host:     "%database_host2%"
                port:     "%database_port2%"
                dbname:   "%database_name2%"
                user:     "%database_user2%"
                password: "%database_password2%"
                charset:  UTF8
    orm:
        default_entity_manager: analysis
        entity_managers:
            analysis:
                connection: analysis
                mappings:
                    JPCoreBundle: ~
            login:
                connection: login
        auto_generate_proxy_classes: "%kernel.debug%"

Question: Is this correct use of multiple databases in Symfony2 environment? 问题: Symfony2环境中是否正确使用了多个数据库? Could it be improved somehow? 可以以某种方式进行改进吗?

Is this correct use of multiple databases in Symfony2 environment? 在Symfony2环境中是否正确使用了多个数据库?

This is the correct use of multiple databases in Symfony2. 这是Symfony2中多个数据库的正确用法。

Could it be improved somehow? 可以以某种方式进行改进吗?

The only improvement (as mentioned in comments) which is not directly related to use of multiple database in Symfony2 is to use DQL or QueryBuilder instead of using the connection directly. 与Symfony2中使用多个数据库不直接相关的唯一改进(如注释中所述)是使用DQL或QueryBuilder而不是直接使用连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM