我想在我的系统中组合多个数据库。大多数时候数据库是MySQL;但将来可能会有所不同,即管理员可以生成这样的报告,这是使用异构数据库系统的来源。
所以我的问题是Laravel 是否提供任何 Facade 来处理这种情况?或者任何其他框架有更适合问题的功能吗?
最佳答案
测试版本(已更新)
定义连接
在 .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mysql_database
DB_USERNAME=root
DB_PASSWORD=secret
DB_CONNECTION_PGSQL=pgsql
DB_HOST_PGSQL=127.0.0.1
DB_PORT_PGSQL=5432
DB_DATABASE_PGSQL=pgsql_database
DB_USERNAME_PGSQL=root
DB_PASSWORD_PGSQL=secret
在 config/database.php
'mysql' => [
'driver' => env('DB_CONNECTION'),
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
'pgsql' => [
'driver' => env('DB_CONNECTION_PGSQL'),
'host' => env('DB_HOST_PGSQL'),
'port' => env('DB_PORT_PGSQL'),
'database' => env('DB_DATABASE_PGSQL'),
'username' => env('DB_USERNAME_PGSQL'),
'password' => env('DB_PASSWORD_PGSQL'),
],
Note: In
pgsql
, ifDB_username
andDB_password
are the same, then you can useenv('DB_USERNAME')
, which is mentioned in.env
first few lines.
Without .env
<= 4.0 (or lower)
app/config/database.php
return array(
'default' => 'mysql',
'connections' => array(
# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'mysql_database',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'pgsql' => [
'driver' => 'pgsql',
'host' => 'localhost',
'port' => '5432',
'database' => 'pgsql_database',
'username' => 'root',
'password' => 'secret',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
]
),
);
<小时/>
架构/迁移
运行connection()
方法指定要使用的连接。
Schema::connection('pgsql')->create('some_table', function($table)
{
$table->increments('id'):
});
或者,在顶部定义一个连接。
protected $connection = 'pgsql';
<小时/>
查询生成器
$users = DB::connection('pgsql')->select(...);
<小时/>
模型(在 Laravel >= 5.0(或更高版本)中)
设置 $connection
模型中的变量
class ModelName extends Model { // extend changed
protected $connection = 'pgsql';
}
Eloquent (在 Laravel <= 4.0(或更低)中)
设置 $connection
模型中的变量
class SomeModel extends Eloquent {
protected $connection = 'pgsql';
}
<小时/>
交易模式
DB::transaction(function () {
DB::connection('mysql')->table('users')->update(['name' => 'John']);
DB::connection('pgsql')->table('orders')->update(['status' => 'shipped']);
});
或
DB::connection('mysql')->beginTransaction();
try {
DB::connection('mysql')->table('users')->update(['name' => 'John']);
DB::connection('pgsql')->beginTransaction();
DB::connection('pgsql')->table('orders')->update(['status' => 'shipped']);
DB::connection('pgsql')->commit();
DB::connection('mysql')->commit();
} catch (\Exception $e) {
DB::connection('mysql')->rollBack();
DB::connection('pgsql')->rollBack();
throw $e;
}
<小时/>
您还可以通过 setConnection
在运行时定义连接方法或 on
静态方法:
class SomeController extends BaseController {
public function someMethod()
{
$someModel = new SomeModel;
$someModel->setConnection('pgsql'); // non-static method
$something = $someModel->find(1);
$something = SomeModel::on('pgsql')->find(1); // static method
return $something;
}
}
<小时/>Note: Be careful about building relationships with tables across databases! It is possible to do, but it can come with caveats depending on your database and settings.
使用多个数据库连接
您可以访问每个connection
通过 DB
上的连接方法 使用多个连接时的外观。 name
传递给 connection
方法应对应于 config/database.php
中列出的连接之一配置文件:
$users = DB::connection('foo')->select(...);
您还可以使用 getPdo
访问原始的底层 PDO 实例。连接实例上的方法:
$pdo = DB::connection()->getPdo();
<小时/>
有用的链接
关于php - 如何在 Laravel 中使用多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845135/