Symfony 3 连接到多个数据库

2022-01-16 00:00:00 database php symfony doctrine
dynamic:
driver:   %database_driver%
host:     %database_host%
port:     %database_port%
dbname:   %database name%
user:     %database_user%
password: %database_password%
charset:  UTF8

大家好

我想动态连接到多个数据库.我阅读了本教程 http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html,它的工作原理和解释一样.

I would like to connect to multiple databases dynamically. I read this tutorial http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html and it works as it was explained.

但我唯一的问题是直接从我的控制器更改 dbname 的值以连接到我的所有数据库.

But my only problem is to change the value of dbname directly from my Controller to connect to all my databases.

我会在 24 小时内解决这个问题.如果你有想法我真的需要它

It will be 24 hours that I am on this problem. if you have ideas I really need it

推荐答案

您的代码示例仅显示一个连接.您必须定义数据库连接,例如 文档中的示例:

Your code example shows only one connection. You've to define to database connection, like the example from the documentation:

如果您想要一个简单的方法,请不要使用 EntityManager.如果您只想获取有关数据库的一些信息并且不想存储信息或使用实体,这可能是矫枉过正.然后,只需创建一个自定义连接并使用vanilla"SQL:

If you want an easy way out, don't use an EntityManager at all. It might be overkill if you only want to get some information about the database and don't want to store information or use Entities. Then, just create a custom connection and use 'vanilla' SQL:

$this->get('doctrine.dbal.connection_factory')->createConnection($params);

您的问题并未说明您期望有多少数据库.所以让我们描述两种可能性:少量(固定)数据库或动态连接.

Your question does not say how many database you expect. So let's describe two possibilities: a small number of (fixed) database or a dynamic connection.

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
            model_a:
                driver:   pdo_mysql
                host:     '%database_hosta%'
                port:     '%database_porta%'
                dbname:   '%database_namea%'
                user:     '%database_usera%'
                password: '%database_passworda%'
                charset:  UTF8
            model_b:
                driver:   pdo_mysql
                host:     '%database_hostb%'
                port:     '%database_portb%'
                dbname:   '%database_nameb%'
                user:     '%database_userb%'
                password: '%database_passwordb%'
                charset:  UTF8
    orm:
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
            model_a:
                connection: model_a
            model_b:
                connection: model_b

默认情况下,将使用连接 default.在您的控制器中,您可以切换到另一个连接:

By default, connection default will be used. In your controller, you can switch to another connection:

$customerEm = $this->getDoctrine()->getManager('model_a');

当然,您可以动态选择您的经理:

And of course, you can dynamically choose your Manager:

//just an example, I don't know how you choose which database to use.
$managerName = $company->getModelName();
$em = $this->getDoctrine()->getManager($managerName);

动态创建EntityManager

DoctrineBundle 不支持动态值(除了第一个示例中的配置值).你可以创建你自己的EntityManager, 如果您不想(或不能)定义多个连接,因为动态表名.

Dynamically create EntityManager

The DoctrineBundle has no support for dynamic values (other than configured values like the first example). You can create your own EntityManager, if you don't want (or can't) define multiple connections because the dynamic table name.

创建工厂并将其定义为服务:

Create a factory and define it a service:

class EntityManagerFactory
{
    public function createManager($dbname)
    {
        $isDevMode = false;
        $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);
        // or if you prefer yaml or XML
        //$config = Setup::createXMLMetadataConfiguration(array(__DIR__."/config/xml"), $isDevMode);
        //$config = Setup::createYAMLMetadataConfiguration(array(__DIR__."/config/yaml"), $isDevMode);

        $conn = [
            'driver' => 'pdo_mysql',
            'username' => 'foo',
            'password' => 'foo',
            'dbname' => $dbname
        ];

        // obtaining the entity manager
        return EntityManager::create($conn, $config);
    }
}

并在您的控制器中使用它:

And use it in your controller:

public function controllerAction()
{
    $em = $this->get(EntityManagerFactory::class)->getManager('model-name');
}

相关文章