Model.class.php
5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
* ThinkPHP 简洁模式Model模型类
* 只支持原生SQL操作 支持多数据库连接和切换
*/
class Model {
// 当前数据库操作对象
protected $db = null;
// 数据表前缀
protected $tablePrefix = '';
// 模型名称
protected $name = '';
// 数据库名称
protected $dbName = '';
// 数据表名(不包含表前缀)
protected $tableName = '';
// 实际数据表名(包含表前缀)
protected $trueTableName ='';
// 最近错误信息
protected $error = '';
/**
* 架构函数
* 取得DB类的实例对象
* @param string $name 模型名称
* @access public
*/
public function __construct($name='') {
// 模型初始化
$this->_initialize();
// 获取模型名称
if(!empty($name)) {
$this->name = $name;
}elseif(empty($this->name)){
$this->name = $this->getModelName();
}
// 数据库初始化操作
// 获取数据库操作对象
// 当前模型有独立的数据库连接信息
$this->db(0,empty($this->connection)?$connection:$this->connection);
// 设置表前缀
$this->tablePrefix = $this->tablePrefix?$this->tablePrefix:C('DB_PREFIX');
}
// 回调方法 初始化模型
protected function _initialize() {}
/**
* SQL查询
* @access public
* @param mixed $sql SQL指令
* @return array
*/
public function query($sql) {
if(is_array($sql)) {
return $this->patchQuery($sql);
}
if(!empty($sql)) {
if(strpos($sql,'__TABLE__')) {
$sql = str_replace('__TABLE__',$this->getTableName(),$sql);
}
return $this->db->query($sql);
}else{
return false;
}
}
/**
* 执行SQL语句
* @access public
* @param string $sql SQL指令
* @return false | integer
*/
public function execute($sql='') {
if(!empty($sql)) {
if(strpos($sql,'__TABLE__')) {
$sql = str_replace('__TABLE__',$this->getTableName(),$sql);
}
$result = $this->db->execute($sql);
return $result;
}else {
return false;
}
}
/**
* 得到当前的数据对象名称
* @access public
* @return string
*/
public function getModelName() {
if(empty($this->name)) {
$this->name = substr(get_class($this),0,-5);
}
return $this->name;
}
/**
* 得到完整的数据表名
* @access public
* @return string
*/
public function getTableName() {
if(empty($this->trueTableName)) {
$tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
if(!empty($this->tableName)) {
$tableName .= $this->tableName;
}else{
$tableName .= parse_name($this->name);
}
$this->trueTableName = strtolower($tableName);
}
return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
}
/**
* 启动事务
* @access public
* @return void
*/
public function startTrans() {
$this->commit();
$this->db->startTrans();
return ;
}
/**
* 提交事务
* @access public
* @return boolean
*/
public function commit() {
return $this->db->commit();
}
/**
* 事务回滚
* @access public
* @return boolean
*/
public function rollback() {
return $this->db->rollback();
}
/**
* 切换当前的数据库连接
* @access public
* @param integer $linkNum 连接序号
* @param mixed $config 数据库连接信息
* @return Model
*/
public function db($linkNum='',$config=''){
if(''===$linkNum && $this->db) {
return $this->db;
}
static $_linkNum = array();
static $_db = array();
if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $_linkNum[$linkNum]!=$config) ) {
// 创建一个新的实例
if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数
$config = C($config);
}
$_db[$linkNum] = Db::getInstance($config);
}elseif(NULL === $config){
$_db[$linkNum]->close(); // 关闭数据库连接
unset($_db[$linkNum]);
return ;
}
// 记录连接信息
$_linkNum[$linkNum] = $config;
// 切换数据库连接
$this->db = $_db[$linkNum];
return $this;
}
}