TP5配置多个数据库和使用

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';
}

 

控制器:

Db::connect('database.d2')->table('user')->find();

 

 

 

 

 

 

 

 

————占位符

posted @ 2018-08-26 17:56  小寒1206  阅读(3258)  评论(0编辑  收藏  举报