简体   繁体   English

PHP使用多个MySQL数据库连接

[英]PHP using multiple MySQL database connections

I'm creating an application that makes use of 3 different databases on different servers. 我正在创建一个在不同服务器上使用3个不同数据库的应用程序。 Fetching data happens in my Data mappers, each extending an abstract DataMapper parent class: 获取数据发生在我的Data mappers中,每个都扩展了一个抽象的DataMapper父类:

abstract class DataMapper {

    protected $db;
    protected $db2;
    protected $db3;

    function __construct() {
        $this->db = new Database('db1');
        $this->db2 = new Database('db2');
        $this->db3 = new Database('db3');
    }

}

However, this would be a little overkill for pages that only requires one of these 3 connections. 但是,对于只需要这3个连接中的一个的页面来说,这会有点过分。 What would be the best way to return the correct Database connection for every part of the application? 为应用程序的每个部分返回正确的数据库连接的最佳方法是什么? I've heard about Application Registries, but I have no idea how to set up something like that. 我听说过应用程序注册表,但我不知道如何设置这样的东西。

I don't like how you're setting up your data mappers. 我不喜欢你如何设置你的数据映射器。 You're creating a new connection for every mapper, even if it uses a provider that already has an established connection. 您正在为每个映射器创建新连接,即使它使用已建立连接的提供程序也是如此。 In other words, every data mapper creates a new database object. 换句话说,每个数据映射器都会创建一个新的数据库对象。

Ideally, these database objects should be saved and passed to the data mappers that need them. 理想情况下,应保存这些数据库对象并将其传递给需要它们的数据映射器。 Auto injecting typically works pretty well. 自动注射通常效果很好。 What that means is you don't have to instantiate objects with the new keyword, but by requesting them through your object's constructing paramters. 这意味着您不必使用new关键字实例化对象,而是通过对象的构造参数请求它们。

For example: 例如:

class Example1Mapper extends DataMapper {
    function __construct( Provider1 $provider1 ) { ... }
}

class Example2Mapper extends DataMapper {
    function __construct( Provider1 $provider1, Provider2 $provider2 ) { ... }
}

Above, two mapper classes need different providers. 上面,两个映射器类需要不同的提供者。 The only thing you need to do is specify this through the object's constructor. 您需要做的唯一事情是通过对象的构造函数指定它。 Automatic dependency injecting/autowiring does the rest. 其余的是自动依赖注入/自动装配。

I don't know how your architecture is established, but this is what I do: the router and injector work together. 我不知道你的架构是如何建立的,但这就是我所做的:路由器和注入器协同工作。 The router determines what controller should be called and what action (method) should be called. 路由器确定应该调用哪个控制器以及应该调用什么操作(方法)。 The injector takes this information and reflects the controller's parameters. 进样器获取此信息并反映控制器的参数。 It also reflects the paramters' paramaters, and so on... The injector creates all the objects and decides what db providers, domain objects, etc... to pass. 它还反映了参数的参数,等等......注入器创建所有对象并决定要传递的数据库提供程序,域对象等。 Here and here would be good places to begin learning on injectors, but you probably want to do some reading around. 这里这里将是开始学习注射器的好地方,但你可能想要做一些阅读。 There are also a few good lightweight DICs. 还有一些很好的轻量级DIC。

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

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