1、修改TP5框架数据库连接方式为不重新链接。
2、不同表连接不同数据库。
我们只需要重写db函数即可,至于写在那个函数文件里,就看项目了。
推荐写在common.php项目函数公共文件里
方法源码如下:
<?php
// +----------------------------------------------------------------------
// | 项目共用函数库
// +----------------------------------------------------------------------
use think\Db;
/**
* 实例化数据库类,默认改为不重新链接
* @param string $name 操作的数据表名称(不含前缀)
* @param array|string $config 数据库配置参数
* @param bool $force 是否强制重新连接
* @return object
*/
function db($name = '', $config = [], $force = false)
{
return Db::connect(getConnection($name), $force)->name($name);
}
/**
* 获取须查询表对应数据库配置(不同表需要连接不同的数据库)
* @param string $name 操作的数据表名称(不含前缀)
* return array
*/
function getConnection($name){
static $cnnt = [];
$__tbname = strtolower($name);
if(!isset($cnnt[$__tbname])){
$cnnt[$__tbname] = include(APP_PATH.'/database.php');
}
return $cnnt[$__tbname];
}
?>database.php数据库连接配置文件配置详情:
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 默认库1
$default = [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '172.0.0.1',
// 数据库名
'database' => 'test01',
// 用户名
'username' => 'root',
// 密码
'password' => 'root',
// 端口
'hostport' => '3306',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 't_',
// 数据库调试模式
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型 array 数组 collection Collection对象
'resultset_type' => 'array',
// 是否自动写入时间戳字段
'auto_timestamp' => false,
// 是否需要进行SQL性能分析
'sql_explain' => true,
];
# 数据库2
$test02 = [
'type' => 'mysql',
'hostname' => '172.0.0.1',
'database' => 'test02',
'username' => 'root',
'password' => 'root',
'hostport' => '3306',
'dsn' => '',
'params' => [],
'charset' => 'utf8',
'prefix' => 't_',
'debug' => true,
'deploy' => 0,
'rw_separate' => false,
'master_num' => 1,
'slave_no' => '',
'fields_strict' => true,
'resultset_type' => 'array',
'auto_timestamp' => false,
'sql_explain' => false,
];
/*
例如:
1、操作的是定单表(order)时,连接数据库2(test02)
2、操作的是充值表(pay)时,连接数据库2(test02)
......
*/
$databases = [
'order' => 'test02',
'pay' => 'test02',
];
if(!empty($__tbname)){
if(isset($databases[$__tbname])){
$cnt = $databases[$__tbname];
if($cnt == 'friend'){
if(empty(param('testing'))){
return $default;
}
}
return $$cnt;
}
}
return $default;转载请注明来源地址:小川编程 » https://www.youhutong.com/index.php/article/index/189.html
1、本站发布的内容仅限用于学习和研究目的.请勿用于商业或非法用途,下载后请24小时内删除。
2、本站所有内容均不能保证其完整性,不能接受请勿购买或下载,如需完整程序,请去其官方购买正版使用
3、本站联系方式Email:admin@youhutong.com ,收到邮件会第一时间处理。
4、如侵犯到任何版权问题,请立即告知本站(立即在线告知),本站将及时删除并致以最深的歉意

