mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进model类的db方法,支持直接使用:
$model->db('config') //读取config配置参数作为数据库连接信息
$model->db('config','mysql://root@127.0.0.1/test') //读取指定数据库连接信息连接
This commit is contained in:
@@ -701,28 +701,31 @@ class Model {
|
|||||||
/**
|
/**
|
||||||
* 切换当前的数据库连接
|
* 切换当前的数据库连接
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $linkNum 连接序号
|
* @param mixed $linkId 连接标识
|
||||||
* @param mixed $config 数据库连接信息
|
* @param mixed $config 数据库连接信息
|
||||||
* @return Model
|
* @return Model
|
||||||
*/
|
*/
|
||||||
public function db($linkNum='',$config=''){
|
public function db($linkId='',$config=''){
|
||||||
if(''===$linkNum && $this->db) {
|
if(''===$linkId && $this->db) {
|
||||||
return $this->db;
|
return $this->db;
|
||||||
}
|
}
|
||||||
if(!isset($this->_db[$linkNum])) {
|
|
||||||
|
if(!isset($this->_db[$linkId])) {
|
||||||
// 创建一个新的实例
|
// 创建一个新的实例
|
||||||
if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数
|
if(is_string($linkId) && ''==$config){
|
||||||
|
$config = Config::get($linkId);
|
||||||
|
}elseif(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数
|
||||||
$config = Config::get($config);
|
$config = Config::get($config);
|
||||||
}
|
}
|
||||||
$this->_db[$linkNum] = Db::instance($config);
|
$this->_db[$linkId] = Db::instance($config);
|
||||||
}elseif(null === $config){
|
}elseif(null === $config){
|
||||||
$this->_db[$linkNum]->close(); // 关闭数据库连接
|
$this->_db[$linkId]->close(); // 关闭数据库连接
|
||||||
unset($this->_db[$linkNum]);
|
unset($this->_db[$linkId]);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 切换数据库连接
|
// 切换数据库连接
|
||||||
$this->db = $this->_db[$linkNum];
|
$this->db = $this->_db[$linkId];
|
||||||
$this->_after_db();
|
$this->_after_db();
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user