风.fox thinkphp5.x之Collection(集合)解析 php集合 http://blog.csdn.net/fenglailea/article/details/52723586 thinkphp5 数据库 链接 http://blog.csdn.net/fenglailea/article/details/52728899
db函数
本函数没什么好说,直接PASS
/**
* 实例化数据库类
* @param string $name 操作的数据表名称(不含前缀)
* @param array|string $config 数据库配置参数
* @param bool $force 是否强制重新连接
* @return \think\db\Query
*/
function db($name =
'', $config = [], $force =
true)
{
return Db::connect($config, $force)->name($name);
}
Db类
namespace think;
use think\
App;
use think\
Collection;
use think\
db\
Query;
use think\
paginator\
Collection as PaginatorCollection;
class Db
{
private static $instance = [];
public static $queryTimes =
0;
public static $executeTimes =
0;
public static function connect($config = [], $name = false)
{
if (
false ===
$name) {
$name = md5(serialize(
$config));
}
if (
true ===
$name || !
isset(
self::
$instance[
$name])) {
$options =
self::parseConfig(
$config);
if (
empty(
$options[
'type'])) {
throw new \InvalidArgumentException(
'Underfined db type');
}
$class =
false !== strpos(
$options[
'type'],
'\\') ?
$options[
'type'] :
'\\think\\db\\connector\\' . ucwords(
$options[
'type']);
if (App::
$debug) {
Log::record(
'[ DB ] INIT ' .
$options[
'type'],
'info');
}
if (
true ===
$name) {
return new $class(
$options);
}
else {
self::
$instance[
$name] =
new $class(
$options);
}
}
return self::
$instance[
$name];
}
private static function parseConfig($config)
{
if (
empty(
$config)) {
$config = Config::get(
'database');
}
elseif (is_string(
$config) &&
false === strpos(
$config,
'/')) {
$config = Config::get(
$config);
}
if (is_string(
$config)) {
return self::parseDsn(
$config);
}
else {
return $config;
}
}
private static function parseDsn($dsnStr)
{
$info = parse_url(
$dsnStr);
if (!
$info) {
return [];
}
$dsn = [
'type' =>
$info[
'scheme'],
'username' =>
isset(
$info[
'user']) ?
$info[
'user'] :
'',
'password' =>
isset(
$info[
'pass']) ?
$info[
'pass'] :
'',
'hostname' =>
isset(
$info[
'host']) ?
$info[
'host'] :
'',
'hostport' =>
isset(
$info[
'port']) ?
$info[
'port'] :
'',
'database' => !
empty(
$info[
'path']) ? ltrim(
$info[
'path'],
'/') :
'',
'charset' =>
isset(
$info[
'fragment']) ?
$info[
'fragment'] :
'utf8',
];
if (
isset(
$info[
'query'])) {
parse_str(
$info[
'query'],
$dsn[
'params']);
}
else {
$dsn[
'params'] = [];
}
return $dsn;
}
public static function __callStatic($method, $params)
{
return call_user_func_array([
self::connect(),
$method],
$params);
}
}
相关资源:仿Thinkphp的数据库类,超级好用,一个php文件