Skip to content

Commit ccb8aa2

Browse files
committed
改进
1 parent 2fa6eca commit ccb8aa2

File tree

6 files changed

+74
-15
lines changed

6 files changed

+74
-15
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"think\\": "src"
1717
},
1818
"files": [
19+
"src/config.php"
1920
]
2021
}
2122
}

src/Db.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Db
8686

8787
public static function setConfig($config = [])
8888
{
89-
self::$config = $config;
89+
self::$config = array_merge(self::$config, $config);
9090
}
9191

9292
public static function getConfig($name = null)

src/Model.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ public function newInstance($data = [], $isUpdate = false, $where = null)
142142
protected function buildQuery()
143143
{
144144
// 设置当前模型 确保查询返回模型对象
145-
$class = $this->query;
146-
$query = new $class();
147-
$query->connect($this->connection)->model($this);
145+
$query = Db::connect($this->connection)->model($this);
148146

149147
// 设置当前数据表和模型名
150148
if (!empty($this->table)) {

src/config.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2017 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: liu21st <[email protected]>
10+
// +----------------------------------------------------------------------
11+
namespace think;
12+
13+
Db::setConfig([
14+
// 数据库类型
15+
'type' => '',
16+
// 服务器地址
17+
'hostname' => '',
18+
// 数据库名
19+
'database' => '',
20+
// 用户名
21+
'username' => '',
22+
// 密码
23+
'password' => '',
24+
// 端口
25+
'hostport' => '',
26+
// 连接dsn
27+
'dsn' => '',
28+
// 数据库连接参数
29+
'params' => [],
30+
// 数据库编码默认采用utf8
31+
'charset' => 'utf8',
32+
// 数据库表前缀
33+
'prefix' => '',
34+
// 数据库调试模式
35+
'debug' => false,
36+
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
37+
'deploy' => 0,
38+
// 数据库读写是否分离 主从式有效
39+
'rw_separate' => false,
40+
// 读写分离后 主服务器数量
41+
'master_num' => 1,
42+
// 指定从服务器序号
43+
'slave_no' => '',
44+
// 是否严格检查字段是否存在
45+
'fields_strict' => true,
46+
// 数据集返回类型
47+
'resultset_type' => '',
48+
// 自动写入时间戳字段
49+
'auto_timestamp' => false,
50+
// 时间字段取出后的默认时间格式
51+
'datetime_format' => 'Y-m-d H:i:s',
52+
// 是否需要进行SQL性能分析
53+
'sql_explain' => false,
54+
// Builder类
55+
'builder' => '',
56+
// Query类
57+
'query' => '\\think\\db\\Query',
58+
// 是否需要断线重连
59+
'break_reconnect' => false,
60+
]);

src/db/Connection.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ public function find(Query $query)
803803

804804
$bind = $query->getBind();
805805

806-
if ($options['fetch_sql']) {
806+
if (!empty($options['fetch_sql'])) {
807807
// 获取实际执行的SQL语句
808808
return $this->getRealSql($sql, $bind);
809809
}
@@ -882,7 +882,7 @@ public function select(Query $query)
882882

883883
$bind = $query->getBind();
884884

885-
if ($options['fetch_sql']) {
885+
if (!empty($options['fetch_sql'])) {
886886
// 获取实际执行的SQL语句
887887
return $this->getRealSql($sql, $bind);
888888
}
@@ -926,7 +926,7 @@ public function insert(Query $query, $replace = false, $getLastInsID = false, $s
926926

927927
$bind = $query->getBind();
928928

929-
if ($options['fetch_sql']) {
929+
if (!empty($options['fetch_sql'])) {
930930
// 获取实际执行的SQL语句
931931
return $this->getRealSql($sql, $bind);
932932
}
@@ -988,7 +988,7 @@ public function insertAll(Query $query, $dataSet = [], $replace = false, $limit
988988

989989
$bind = $query->getBind();
990990

991-
if ($options['fetch_sql']) {
991+
if (!empty($options['fetch_sql'])) {
992992
// 获取实际执行的SQL语句
993993
return $this->getRealSql($sql, $bind);
994994
} elseif (is_array($sql)) {
@@ -1021,7 +1021,7 @@ public function selectInsert(Query $query, $fields, $table)
10211021

10221022
$bind = $query->getBind();
10231023

1024-
if ($options['fetch_sql']) {
1024+
if (!empty($options['fetch_sql'])) {
10251025
// 获取实际执行的SQL语句
10261026
return $this->getRealSql($sql, $bind);
10271027
} else {
@@ -1088,7 +1088,7 @@ public function update(Query $query)
10881088
$sql = $this->builder->update($query);
10891089
$bind = $query->getBind();
10901090

1091-
if ($options['fetch_sql']) {
1091+
if (!empty($options['fetch_sql'])) {
10921092
// 获取实际执行的SQL语句
10931093
return $this->getRealSql($sql, $bind);
10941094
} else {
@@ -1150,7 +1150,7 @@ public function delete(Query $query)
11501150

11511151
$bind = $query->getBind();
11521152

1153-
if ($options['fetch_sql']) {
1153+
if (!empty($options['fetch_sql'])) {
11541154
// 获取实际执行的SQL语句
11551155
return $this->getRealSql($sql, $bind);
11561156
}
@@ -1217,7 +1217,7 @@ public function value(Query $query, $field, $default = null)
12171217

12181218
$bind = $query->getBind();
12191219

1220-
if ($options['fetch_sql']) {
1220+
if (!empty($options['fetch_sql'])) {
12211221
// 获取实际执行的SQL语句
12221222
return $this->getRealSql($sql, $bind);
12231223
}
@@ -1285,7 +1285,7 @@ public function column(Query $query, $field, $key = '')
12851285

12861286
$bind = $query->getBind();
12871287

1288-
if ($options['fetch_sql']) {
1288+
if (!empty($options['fetch_sql'])) {
12891289
// 获取实际执行的SQL语句
12901290
return $this->getRealSql($sql, $bind);
12911291
}
@@ -1349,7 +1349,7 @@ public function pdo(Query $query)
13491349

13501350
$bind = $query->getBind();
13511351

1352-
if ($options['fetch_sql']) {
1352+
if (!empty($options['fetch_sql'])) {
13531353
// 获取实际执行的SQL语句
13541354
return $this->getRealSql($sql, $bind);
13551355
}

src/db/builder/Mongo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected function parseWhereItem(Query $query, $field, $val)
320320
protected function parseDateTime(Query $query, $value, $key)
321321
{
322322
// 获取时间字段类型
323-
$type = $this->connection->getTableInfo('', 'type');
323+
$type = $this->connection->getFieldsType($query->getOptions('table') ?: $query->getTable());
324324

325325
if (isset($type[$key])) {
326326
$value = strtotime($value) ?: $value;

0 commit comments

Comments
 (0)