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

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

问题描述



现在我有一种方法来改变循环中的数据库连接:$ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ set('database.connections.mysql.database',$ key。'_'。$ database);

$ productRepo = new ProductRepository();

$ products = $ productRepo-> all();

}

在产品库内,我打电话给Eloquent方法all()获取数据库中的所有产品。



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



有没有什么可以做的,强制强制在运行时解析连接参数?

解决方案

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



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:

  public function setConnection($ name){
//假设$ this->产品是您的产品型号
$ this-> product-> setConnection($ name) ;
}

代码:

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



编辑





Laravel仅在首次需要时实例化与数据库的连接。一旦创建了连接,它将重用它。如果您必须重新连接到每个查询的数据库,这将是一个巨大的性能问题。



就是说,如果你真的想这样做你原来的方式设计它,所有你需要做的是更新配置后调用 DB :: reconnect()方法。

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

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

但是,我强烈建议您考虑最初描述的选项,以便您没有一大堆不必要的连接。


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();

}

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.

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

解决方案

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:

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

ProductRepository.php:

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

Code:

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

Edit

additional information based on comments

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.

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.

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

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