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