改进Model类 添加getDb静态方法获取db查询对象

This commit is contained in:
thinkphp
2016-09-22 11:37:19 +08:00
parent 681e5eeac7
commit 0f5151dde4

View File

@@ -1346,11 +1346,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public static function __callStatic($method, $params) public static function __callStatic($method, $params)
{ {
$model = get_called_class(); $query = self::getDb();
if (!isset(self::$links[$model])) {
self::$links[$model] = (new static())->db();
}
$query = self::$links[$model];
// 全局作用域 // 全局作用域
if (static::$useGlobalScope && method_exists($model, 'base')) { if (static::$useGlobalScope && method_exists($model, 'base')) {
call_user_func_array('static::base', [ & $query]); call_user_func_array('static::base', [ & $query]);
@@ -1358,6 +1354,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return call_user_func_array([$query, $method], $params); return call_user_func_array([$query, $method], $params);
} }
protected static function getDb()
{
$model = get_called_class();
if (!isset(self::$links[$model])) {
self::$links[$model] = (new static())->db();
}
return self::$links[$model];
}
/** /**
* 修改器 设置数据对象的值 * 修改器 设置数据对象的值
* @access public * @access public