如何连接到php文件中的多个db?

如何连接到php文件中的多个db?,php,symfony,symfony-3.2,Php,Symfony,Symfony 3.2,我正在使用DBAL with Doctrine在Symfony 3中与数据库建立连接。我想做的是将所有查询放在一个PHP文件中,而不是控制器中 在控制器中,我可以使用: $conn = $this->get('doctrine.dbal.database2_connection'); 获得一个连接,但在一个简单的PHP中我不能 因此,我不知道如何在不使用EntityManager对象的情况下调用多个连接,因为我没有使用实体类。如果使用PDO,每个连接都被视为一个对象,并且可以同时拥有任意

我正在使用DBAL with Doctrine在Symfony 3中与数据库建立连接。我想做的是将所有查询放在一个PHP文件中,而不是控制器中

在控制器中,我可以使用:

$conn = $this->get('doctrine.dbal.database2_connection');
获得一个连接,但在一个简单的PHP中我不能


因此,我不知道如何在不使用EntityManager对象的情况下调用多个连接,因为我没有使用实体类。

如果使用PDO,每个连接都被视为一个对象,并且可以同时拥有任意数量的连接

如果使用PDO,每个连接都被视为一个对象,可以同时拥有任意数量的连接

要实现您想要的目标,需要掌握以下几点:

  • 您需要检查如何使用多个数据库(我想这就是您所说的多个连接)

  • 此外,您还需要了解如何

  • 您需要使用来执行自己的查询

  • 最后一点是你需要习惯


  • 请随意跳过您已经知道的所有内容。我不能在这里详细说明一切,因为你的问题很模糊。因此,这个答案将指导你如何找到解决方案,或者至少关注你的问题

    要实现您想要的目标,需要掌握以下几点:

  • 您需要检查如何使用多个数据库(我想这就是您所说的多个连接)

  • 此外,您还需要了解如何

  • 您需要使用来执行自己的查询

  • 最后一点是你需要习惯


  • 请随意跳过您已经知道的所有内容。我不能在这里详细说明一切,因为你的问题很模糊。因此,这个答案将指导你如何找到解决方案,或者至少关注你的问题

    Symfony允许您连接多个数据库。你需要做一个bt训练来实现这一点

    步骤#1

    将参数添加到
    parameters.yml
    文件中。可以使用以下参数添加第二个数据库连接:

    Parameters:
    
    # First database
    
      database_host: 127.0.0.1   
      database_port: null   
      database_name: qmsfumgabd  
      database_user: qmsfumgabd 
      database_password: xxx9bxxMxx
    
    # Second database
    
      database2_host: 127.0.0.1 
      database2_port: null 
      database2_name: huscqxzwaw
      database2_user: huscqxzwaw
      database2_password: dxxxFXxxxB
    
    第2步

    下一步是在
    config.yml
    中获取这些凭据:

    doctrine:
    
      # Configure the abstraction layer
    
      dbal:
    
          # Set the default connection to default
    
          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
    
             database2:
    
                   driver:    pdo_mysql
    
                   host:      '%database2_host%'
    
                   port:      '%database2_port%'
    
                   dbname:    '%database2_name%'
    
                   user:      '%database2_user%'
    
                   password:  '%database2_password%'
    
                   charset:   UTF8
    
    第三步

    最后,指定项目中每个捆绑包的映射:

    # Configure the ORM
    
      orm:
    
          default_entity_manager: default
    
          entity_managers:
    
              # Register which bundle should use which connection
    
              default:
    
                  connection: default
    
                  mappings:
    
                      AppBundle:  ~
    
                      DemoBundle: ~
    
              database2:
    
                  connection: database2
    
                  mappings:
    
                      CloudwaysBundle: ~
    
    第四步

    现在,要调用任何实体管理器,只需使用连接名称:

    class UserController extends Controller
    
    {
    
       public function indexAction()
    
       {
    
           // All 3 return the "default" entity manager
    
           $em = $this->getDoctrine()->getManager();
    
           $em = $this->getDoctrine()->getManager('default');
    
           $em = $this->get('doctrine.orm.default_entity_manager');
    
    
    
           // Both of these return the "database2" entity manager
    
           $anotherEm = $this->getDoctrine()->getManager('database2');
    
           $anotherEm = $this->get('doctrine.orm.database2_entity_manager');
    
       }
    
    }
    

    您还可以从symfony的文档中获取帮助。

    symfony允许您连接多个数据库。你需要做一个bt训练来实现这一点

    步骤#1

    将参数添加到
    parameters.yml
    文件中。可以使用以下参数添加第二个数据库连接:

    Parameters:
    
    # First database
    
      database_host: 127.0.0.1   
      database_port: null   
      database_name: qmsfumgabd  
      database_user: qmsfumgabd 
      database_password: xxx9bxxMxx
    
    # Second database
    
      database2_host: 127.0.0.1 
      database2_port: null 
      database2_name: huscqxzwaw
      database2_user: huscqxzwaw
      database2_password: dxxxFXxxxB
    
    第2步

    下一步是在
    config.yml
    中获取这些凭据:

    doctrine:
    
      # Configure the abstraction layer
    
      dbal:
    
          # Set the default connection to default
    
          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
    
             database2:
    
                   driver:    pdo_mysql
    
                   host:      '%database2_host%'
    
                   port:      '%database2_port%'
    
                   dbname:    '%database2_name%'
    
                   user:      '%database2_user%'
    
                   password:  '%database2_password%'
    
                   charset:   UTF8
    
    第三步

    最后,指定项目中每个捆绑包的映射:

    # Configure the ORM
    
      orm:
    
          default_entity_manager: default
    
          entity_managers:
    
              # Register which bundle should use which connection
    
              default:
    
                  connection: default
    
                  mappings:
    
                      AppBundle:  ~
    
                      DemoBundle: ~
    
              database2:
    
                  connection: database2
    
                  mappings:
    
                      CloudwaysBundle: ~
    
    第四步

    现在,要调用任何实体管理器,只需使用连接名称:

    class UserController extends Controller
    
    {
    
       public function indexAction()
    
       {
    
           // All 3 return the "default" entity manager
    
           $em = $this->getDoctrine()->getManager();
    
           $em = $this->getDoctrine()->getManager('default');
    
           $em = $this->get('doctrine.orm.default_entity_manager');
    
    
    
           // Both of these return the "database2" entity manager
    
           $anotherEm = $this->getDoctrine()->getManager('database2');
    
           $anotherEm = $this->get('doctrine.orm.database2_entity_manager');
    
       }
    
    }
    

    您还可以从symfony的文档中获得帮助。

    首先,您所说的“简单PHP文件”是什么意思,我认为symfony(2和3)方法不允许使用“简单PHP文件”。我的意思是一个PHP类不声明为controller@fabricekabongota查看服务配置和依赖注入。首先,你所说的“简单PHP文件”是什么意思?我认为Symfony(2和3)方法不允许使用“简单PHP文件”。我指的是一个PHP类,它没有声明为controller@FabriceKabongoTake查看服务配置和依赖注入。