Schema Facade中的DB :: reconnect()?

时间:2016-12-28 13:44:07

标签: database laravel

我正在研究一个多数据库系统。系统将集成客户端本地mssql服务器。

我会在我的主数据库中存储凭证信息。我想将一些表迁移到我的客户本地数据库。

如您所知,我们可以选择以下连接。

Schema::connection('sqlsrv')->create('products', function (Blueprint $table) {

});

但我必须动态使用它。

此连接从config / database.php

读取
'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('database.sqlite')),
        'prefix' => '',
    ],

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

    'sqlsrv' => [
        'driver'   => 'sqlsrv',
        'host'     => env('SQLSRV_HOST', 'localhost'),
        'database' => env('SQLSRV_DATABASE', 'forge'),
        'username' => env('SQLSRV_USERNAME', 'forge'),
        'password' => env('SQLSRV_PASSWORD', ''),
        'collation' => 'Turkish_CI_AS',
        'charset'  => 'utf8',
        'prefix'   => env('SQLSRV_PREFIX', ''),
    ],

    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
    ],

],

我在使用DB Facade时做到了。

public function getConnection($connection_id)
{
    /** Bağlantı Bilgilerini Çeker */
    $connection = Connection::find($connection_id);

    try
    {
        /** Bağlantı Ayarları */
        config()->set('database.connections.sqlsrv.host', $connection->host);
        config()->set('database.connections.sqlsrv.database',  $connection->db);
        config()->set('database.connections.sqlsrv.username',  $connection->username);
        config()->set('database.connections.sqlsrv.password',  $connection->password);
        config()->set('database.connections.sqlsrv.charset',  $connection->charset);
        config()->set('database.connections.sqlsrv.collation',  $connection->collation);
        config()->set('database.connections.sqlsrv.prefix',  $connection->prefix);

        /** Ayarlar ile yeniden bağlanma */
        return DB::reconnect($connection->driver);
    }catch (\Exception $e)
    {
        throw new \Exception($e);
    }
}

在Schema Facade中是否有类似DB :: reconnect()的方法或解决方案?

1 个答案:

答案 0 :(得分:1)

这是我最终做的事情。希望它有助于你的情况。

    // Erase the tenant connection, thus making Laravel
    // get the default values all over again.
    DB::purge('tenant');

    // Make sure to use the database name we want to establish a connection.
    Config::set('database.connections.tenant.host', $company->database->main_server);
    Config::set('database.connections.tenant.database', $company->mysql_database);
    Config::set('database.connections.tenant.username', $company->mysql_username);
    Config::set('database.connections.tenant.password', $company->mysql_password);

    // Rearrange the connection data
    DB::reconnect('tenant');

    // Ping the database. This will thrown an exception in case
    // the database does not exists.
    Schema::connection('tenant')->getConnection()->reconnect();

我注意到如果你没有DB::purge,Schema将无法接收任何连接更改。

另一方面,如果您进行清除,但数据库不存在,则在实际尝试对数据库连接执行某些操作之前,您不会获得异常。最后一行中的reconnect()将触发该行。