tp5配置多数据库
tinkphp5配置多数据库以及Db切库和模型切库
database.php
return [
'd1'=>[
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'd1',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
// 端口
'hostport' => '3306',
],
'd2'=>[
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'd2',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
// 端口
'hostport' => '3306',
]
];
Model:
protected function initialize()
{
parent::initialize();
$this->connection = 'database.d2';
}
Controller:
Db::connect('database.d2')->table('user')->find();