zoukankan      html  css  js  c++  java
  • Symfony2 多数据库连接

    一、在app/config/parameters.ini里配置

    ; These parameters can be imported into other config files
    ; by enclosing the key with % (like %database_user%)
    ; Comments start with ';', as in php.ini
    [parameters]
        database_driver   = pdo_mysql
        database_host     = 192.168.0.1
        database_port     =
        database_name     = sym
        database_user     = root
        database_password = test
        
        database2_host     = 192.168.0.2
        database2_port     =
        database2_name     = sym
        database2_user     = root
        database2_password = test
    
        mailer_transport  = smtp
        mailer_host       = localhost
        mailer_user       =
        mailer_password   =
    
        locale            = en
    
        secret            = ThisTokenIsNotSoSecretChangeIt

    二、在app/config/config.yml里配置

    # Doctrine Configuration
    doctrine:
        dbal:
          connections:
            default:
              driver:   %database_driver%
              host:     %database_host%
              port:     %database_port%
              dbname:   %database_name%
              user:     %database_user%
              password: %database_password%
              charset:  UTF8
            another:
              driver:   %database_driver%
              host:     %database2_host%
              port:     %database2_port%
              dbname:   %database2_name%
              user:     %database2_user%
              password: %database2_password%
              charset:  UTF8
    
        orm:
            default_entity_manager: default
            entity_managers:
              default:
                connection: default
                mappings:
    #              AcmeDemoBundle: ~
                  AcmeTicketBundle: ~
              another:
                 connection: another
                 mappings:
    #              AcmeDemoBundle: ~
                  AcmeTicketBundle: ~
    #        auto_generate_proxy_classes: %kernel.debug%
    #        auto_mapping: true
    
    # Swiftmailer Configuration

    三、调用方式

    if ($type == 1) {
                $this->emtitymanager = $this->get('doctrine')->getEntityManager(); //默认
            } else {
                $this->emtitymanager = $this->get('doctrine')->getEntityManager('another'); //其他数据库
            }
  • 相关阅读:
    【洛谷P1558】色板游戏
    【SCOI2010】序列操作
    Windows下MySQL下载安装、配置与使用
    Redis和MySQL数据同步及Redis使用场景
    python的设计模式之MVC模式
    python 面试真题
    python调用C语言
    一次跨域请求出现 OPTIONS 请求的问题及解决方法
    服务器宕机问题
    页面静态化
  • 原文地址:https://www.cnblogs.com/dluf/p/2864269.html
Copyright © 2011-2022 走看看