php mysqli选择数据表,PHP-MySQLi-Database-Class 使用数据表模型类时如何指定或选择连接?...

易弘阔
2023-12-01

看了一下源码,代码中有这么一段:

public function addConnection($name, array $params)

{

$this->connectionsSettings[$name] = array();

foreach (array('host', 'username', 'password', 'db', 'port', 'socket', 'charset') as $k) {

$prm = isset($params[$k]) ? $params[$k] : null;

if ($k == 'host') {

if (is_object($prm))

$this->_mysqli[$name] = $prm;

if (!is_string($prm))

$prm = null;

}

$this->connectionsSettings[$name][$k] = $prm;

}

return $this;

}

上面这段是你需要添加的链接

public function connection($name)

{

if (!isset($this->connectionsSettings[$name]))

throw new Exception('Connection ' . $name . ' was not added.');

$this->defConnectionName = $name;

return $this;

}

这段是你要 *protected $connection = 'datacenter';* 这种, 个人认为 你得写个构造方法, 一进来以后 判断配置 $connection 了没有 如果没有 就 调用 下面个方法:

try{

$this->db->connection($this->connection);

}catch(\Excepotion $e) {

$this->db->addConnection($this->connection,['host'=>'127.0.0.1','db'=>'?' ]) // 这里补全

$this->db->connection($this->connection);

}

 类似资料: