强制 Eloquent 模型重新解析数据库连接 [英] Forcing Eloquent models to re resolve database connection

查看:14
本文介绍了强制 Eloquent 模型重新解析数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法强制 Eloquent 模型重新解析它们实例化的连接?

Is there any way to force Eloquent models to re resolve the connection they were instantiated with?

现在我有一个方法可以在循环中更改数据库连接:

Right now I have a method that changes the database connection in a loop :

foreach ($array as $key => $value) {

   Config::set('database.connections.mysql.database', $key . '_' . $database);

   $productRepo = new ProductRepository();    

   $products = $productRepo->all();

}

在产品存储库中,我调用 Eloquent 方法 all() 以获取数据库中的所有产品.

Inside the product repository, i'm calling the Eloquent methods all() to get all the products in the database.

我发现连接已更改为数组中的第一个值,但在进一步执行循环时不会更改.

What i discover was that the connection was changed to the first value in the array but it does not change on further executions of the loop.

我能做些什么来强制 Eloquent 在运行时解析连接参数?

Is there anything I can do to force Eloquent to resolve the connection parameters at runtime?

推荐答案

每个数据库实际上都是一个独立的连接,所以我会在 config/database.php 文件中设置到数据库的所有连接,然后可以使用模型上的setConnection()方法在它们之间切换.

Each database is really a separate connection, so I would setup all the connections to the database in the config/database.php file, and then you can use the setConnection() method on the model to switch between them.

database.php:

database.php:

return [
    'default' => 'mysql-key1',
    'connections' => [
        'mysql-key1' => [
            'driver'    => 'mysql',
            'database'  => 'key1_dbname,
            // etc
        ],
        'mysql-key2' => [
            'driver'    => 'mysql',
            'database'  => 'key2_dbname',
            // etc
        ]
    ]
];

ProductRepository.php:

ProductRepository.php:

public function setConnection($name) {
    // assumes $this->product is your Product model
    $this->product->setConnection($name);
}

代码:

$productRepo = new ProductRepository();
foreach ($array as $key => $value) {
   $productRepo->setConnection($key . '_' . $database);
   $products = $productRepo->all();
}

编辑

基于评论的附加信息

Laravel 只会在第一次需要时实例化到数据库的连接.创建连接后,它将重用它.如果每次查询都必须重新连接到数据库,这将是一个巨大的性能问题.

Laravel is only going to instantiate a connection to the database the first time it is needed. Once the connection has been created, it is going to reuse it. It would be a huge performance issue if you had to reconnect to the database for every single query.

话虽如此,如果您真的想按照最初设计的方式执行此操作,您只需在更新配置后调用 DB::reconnect() 方法即可.

That being said, if you really want to do this the way you originally designed it, all you would need to do is call the DB::reconnect() method after updating the config.

foreach ($array as $key => $value) {
    Config::set('database.connections.mysql.database', $key . '_' . $database);
    DB::reconnect();

    $productRepo = new ProductRepository();
    $products = $productRepo->all();
}

但是,我强烈建议您考虑最初描述的选项,以免进行大量不必要的连接.

However, I would strongly urge you to consider the originally described option so that you're not making a ton of unnecessary connections.

这篇关于强制 Eloquent 模型重新解析数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆