mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
完善数据库类
This commit is contained in:
@@ -630,24 +630,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
if ($data instanceof \Closure) {
|
if ($data instanceof \Closure) {
|
||||||
call_user_func_array($data, [ & $db]);
|
call_user_func_array($data, [ & $db]);
|
||||||
$data = [];
|
$data = [];
|
||||||
|
} elseif ($data instanceof Query) {
|
||||||
|
return $data->with($with)->cache($cache)->find();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($cache) {
|
$result = self::with($with)->cache($cache)->find($data);
|
||||||
// 查找是否存在缓存
|
|
||||||
$name = basename(str_replace('\\', '/', get_called_class()));
|
|
||||||
$guid = md5('model_' . $name . '_' . serialize($data));
|
|
||||||
$result = Cache::get($guid);
|
|
||||||
if ($result) {
|
|
||||||
return new static($result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = self::with($with)->find($data);
|
|
||||||
|
|
||||||
if ($cache && $result instanceof Model) {
|
|
||||||
// 缓存模型数据
|
|
||||||
Cache::set($guid, $result->toArray());
|
|
||||||
}
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -664,6 +652,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
if ($data instanceof \Closure) {
|
if ($data instanceof \Closure) {
|
||||||
call_user_func_array($data, [ & $db]);
|
call_user_func_array($data, [ & $db]);
|
||||||
$data = [];
|
$data = [];
|
||||||
|
} elseif ($data instanceof Query) {
|
||||||
|
return $data->with($with)->select();
|
||||||
}
|
}
|
||||||
return self::with($with)->select($data);
|
return self::with($with)->select($data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ abstract class Connection
|
|||||||
'password' => '',
|
'password' => '',
|
||||||
// 端口
|
// 端口
|
||||||
'hostport' => '',
|
'hostport' => '',
|
||||||
|
// 连接dsn
|
||||||
'dsn' => '',
|
'dsn' => '',
|
||||||
// 数据库连接参数
|
// 数据库连接参数
|
||||||
'params' => [],
|
'params' => [],
|
||||||
@@ -80,8 +81,6 @@ abstract class Connection
|
|||||||
'master_num' => 1,
|
'master_num' => 1,
|
||||||
// 指定从服务器序号
|
// 指定从服务器序号
|
||||||
'slave_no' => '',
|
'slave_no' => '',
|
||||||
// like字段自动替换为%%包裹
|
|
||||||
'like_fields' => '',
|
|
||||||
// 是否严格检查字段是否存在
|
// 是否严格检查字段是否存在
|
||||||
'fields_strict' => true,
|
'fields_strict' => true,
|
||||||
];
|
];
|
||||||
@@ -123,6 +122,18 @@ abstract class Connection
|
|||||||
return call_user_func_array([$this->query, $method], $args);
|
return call_user_func_array([$this->query, $method], $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置当前name
|
||||||
|
* @access public
|
||||||
|
* @param string $name
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function name($name)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定当前数据表
|
* 指定当前数据表
|
||||||
* @param string $table 数据表名称
|
* @param string $table 数据表名称
|
||||||
@@ -150,18 +161,6 @@ abstract class Connection
|
|||||||
return $tableName;
|
return $tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置当前name
|
|
||||||
* @access public
|
|
||||||
* @param string $name
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function name($name)
|
|
||||||
{
|
|
||||||
$this->name = $name;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取数据库的配置参数
|
* 获取数据库的配置参数
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ namespace think\db;
|
|||||||
|
|
||||||
use PDO;
|
use PDO;
|
||||||
use think\Cache;
|
use think\Cache;
|
||||||
use think\Config;
|
|
||||||
use think\Db;
|
use think\Db;
|
||||||
use think\Exception;
|
use think\Exception;
|
||||||
use think\Loader;
|
use think\Loader;
|
||||||
@@ -71,7 +70,7 @@ class Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前的builder
|
* 获取当前的builder实例对象
|
||||||
* @access protected
|
* @access protected
|
||||||
* @return \think\db\Builder
|
* @return \think\db\Builder
|
||||||
*/
|
*/
|
||||||
@@ -305,7 +304,7 @@ class Query
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$prefix = $this->config['prefix'];
|
$prefix = $this->connection->getAttribute('prefix');
|
||||||
// 传入的表名为数组
|
// 传入的表名为数组
|
||||||
if (is_array($join)) {
|
if (is_array($join)) {
|
||||||
if (0 !== $key = key($join)) {
|
if (0 !== $key = key($join)) {
|
||||||
@@ -324,7 +323,7 @@ class Query
|
|||||||
} else {
|
} else {
|
||||||
$join = trim($join);
|
$join = trim($join);
|
||||||
if (0 === strpos($join, '__')) {
|
if (0 === strpos($join, '__')) {
|
||||||
$table = $this->parseSqlTable($join);
|
$table = $this->connection->parseSqlTable($join);
|
||||||
} elseif (false === strpos($join, '(') && !empty($prefix) && 0 !== strpos($join, $prefix)) {
|
} elseif (false === strpos($join, '(') && !empty($prefix) && 0 !== strpos($join, $prefix)) {
|
||||||
// 传入的表名中不带有'('并且不以默认的表前缀开头时加上默认的表前缀
|
// 传入的表名中不带有'('并且不以默认的表前缀开头时加上默认的表前缀
|
||||||
$table = $prefix . $join;
|
$table = $prefix . $join;
|
||||||
@@ -615,6 +614,26 @@ class Query
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询缓存
|
||||||
|
* @access public
|
||||||
|
* @param mixed $key
|
||||||
|
* @param integer $expire
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function cache($key = true, $expire = null)
|
||||||
|
{
|
||||||
|
// 增加快捷调用方式 cache(10) 等同于 cache(true, 10)
|
||||||
|
if (is_numeric($key) && is_null($expire)) {
|
||||||
|
$expire = $key;
|
||||||
|
$key = true;
|
||||||
|
}
|
||||||
|
if (false !== $key) {
|
||||||
|
$this->options['cache'] = ['key' => $key, 'expire' => $expire];
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定group查询
|
* 指定group查询
|
||||||
* @access public
|
* @access public
|
||||||
@@ -839,7 +858,7 @@ class Query
|
|||||||
if (in_array($type, [Model::HAS_ONE, Model::BELONGS_TO])) {
|
if (in_array($type, [Model::HAS_ONE, Model::BELONGS_TO])) {
|
||||||
if (0 == $i) {
|
if (0 == $i) {
|
||||||
$joinName = strtolower(basename(str_replace('\\', '/', $this->options['model'])));
|
$joinName = strtolower(basename(str_replace('\\', '/', $this->options['model'])));
|
||||||
$joinTable = $this->getTableName();
|
$joinTable = $this->connection->getTableName();
|
||||||
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
||||||
}
|
}
|
||||||
// 预载入封装
|
// 预载入封装
|
||||||
@@ -1030,6 +1049,9 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function select($data = [])
|
public function select($data = [])
|
||||||
{
|
{
|
||||||
|
if ($data instanceof Query) {
|
||||||
|
return $data->select();
|
||||||
|
}
|
||||||
// 分析查询表达式
|
// 分析查询表达式
|
||||||
$options = $this->parseExpress();
|
$options = $this->parseExpress();
|
||||||
|
|
||||||
@@ -1041,10 +1063,17 @@ class Query
|
|||||||
$this->parsePkWhere($data, $options);
|
$this->parsePkWhere($data, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($options['cache'])) {
|
||||||
|
// 判断查询缓存
|
||||||
|
$cache = $options['cache'];
|
||||||
|
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
||||||
|
$resultSet = Cache::get($key);
|
||||||
|
} else {
|
||||||
// 生成查询SQL
|
// 生成查询SQL
|
||||||
$sql = $this->builder()->select($options);
|
$sql = $this->builder()->select($options);
|
||||||
// 执行查询操作
|
// 执行查询操作
|
||||||
$resultSet = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_pdo']);
|
$resultSet = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_pdo']);
|
||||||
|
}
|
||||||
|
|
||||||
// 返回结果处理
|
// 返回结果处理
|
||||||
if (!empty($resultSet)) {
|
if (!empty($resultSet)) {
|
||||||
@@ -1057,19 +1086,22 @@ class Query
|
|||||||
return $resultSet;
|
return $resultSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($cache)) {
|
||||||
|
// 缓存数据集
|
||||||
|
Cache::set($key, $resultSet, $cache['expire']);
|
||||||
|
}
|
||||||
|
|
||||||
// 数据列表读取后的处理
|
// 数据列表读取后的处理
|
||||||
if (!empty($options['model'])) {
|
if (!empty($options['model'])) {
|
||||||
|
// 生成模型对象
|
||||||
|
$model = $options['model'];
|
||||||
foreach ($resultSet as $key => $result) {
|
foreach ($resultSet as $key => $result) {
|
||||||
if (!empty($options['model'])) {
|
$result = new $model($result);
|
||||||
// 返回模型对象
|
|
||||||
$result = new $options['model']($result);
|
|
||||||
$result->isUpdate(true);
|
$result->isUpdate(true);
|
||||||
// 关联查询
|
// 关联查询
|
||||||
if (!empty($options['relation'])) {
|
if (!empty($options['relation'])) {
|
||||||
$result->relationQuery($options['relation']);
|
$result->relationQuery($options['relation']);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
$resultSet[$key] = $result;
|
$resultSet[$key] = $result;
|
||||||
}
|
}
|
||||||
if (!empty($options['with'])) {
|
if (!empty($options['with'])) {
|
||||||
@@ -1089,6 +1121,9 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function find($data = [])
|
public function find($data = [])
|
||||||
{
|
{
|
||||||
|
if ($data instanceof Query) {
|
||||||
|
return $data->find();
|
||||||
|
}
|
||||||
// 分析查询表达式
|
// 分析查询表达式
|
||||||
$options = $this->parseExpress();
|
$options = $this->parseExpress();
|
||||||
|
|
||||||
@@ -1098,8 +1133,18 @@ class Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
$options['limit'] = 1;
|
$options['limit'] = 1;
|
||||||
|
|
||||||
|
if (isset($options['cache'])) {
|
||||||
|
// 判断查询缓存
|
||||||
|
$cache = $options['cache'];
|
||||||
|
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
|
||||||
|
$result = Cache::get($key);
|
||||||
|
} else {
|
||||||
|
// 生成查询SQL
|
||||||
$sql = $this->builder()->select($options);
|
$sql = $this->builder()->select($options);
|
||||||
|
// 执行查询
|
||||||
$result = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_pdo']);
|
$result = $this->connection->query($sql, $this->getBind(), $options['fetch_sql'], $options['master'], $options['fetch_pdo']);
|
||||||
|
}
|
||||||
|
|
||||||
// 数据处理
|
// 数据处理
|
||||||
if (!empty($result)) {
|
if (!empty($result)) {
|
||||||
@@ -1113,6 +1158,11 @@ class Query
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($cache)) {
|
||||||
|
// 缓存数据
|
||||||
|
Cache::set($key, $result, $cache['expire']);
|
||||||
|
}
|
||||||
|
|
||||||
$data = $result[0];
|
$data = $result[0];
|
||||||
if (!empty($options['model'])) {
|
if (!empty($options['model'])) {
|
||||||
// 返回模型对象
|
// 返回模型对象
|
||||||
@@ -1151,7 +1201,7 @@ class Query
|
|||||||
* @param array $data 表达式
|
* @param array $data 表达式
|
||||||
* @return integer
|
* @return integer
|
||||||
*/
|
*/
|
||||||
public function delete($data)
|
public function delete($data = [])
|
||||||
{
|
{
|
||||||
// 分析查询表达式
|
// 分析查询表达式
|
||||||
$options = $this->parseExpress();
|
$options = $this->parseExpress();
|
||||||
|
|||||||
Reference in New Issue
Block a user