forked from hetao29/slightphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDb.php
More file actions
257 lines (251 loc) · 7.23 KB
/
SDb.php
File metadata and controls
257 lines (251 loc) · 7.23 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/*{{{LICENSE
+-----------------------------------------------------------------------+
| SlightPHP Framework |
+-----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation. You should have received a copy of the |
| GNU General Public License along with this program. If not, see |
| http://www.gnu.org/licenses/. |
| Copyright (C) 2008-2009. All Rights Reserved. |
+-----------------------------------------------------------------------+
| Supports: http://www.slightphp.com |
+-----------------------------------------------------------------------+
}}}*/
if(!defined("SLIGHTPHP_PLUGINS_DIR"))define("SLIGHTPHP_PLUGINS_DIR",dirname(__FILE__));
require_once(SLIGHTPHP_PLUGINS_DIR."/SConfig.php");
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbData.php");
require_once(SLIGHTPHP_PLUGINS_DIR."/db/DbObject.php");
require_once(SLIGHTPHP_PLUGINS_DIR."/db/Db.php");
/**
* @package SlightPHP
*/
class SDb extends SlightPHP\Db{
private static $_config;
static function setConfigFile($file){
self::$_config = $file;
}
/**
* @param string $zone
* @param string $type main|query
* @return array
*/
static function getConfig($zone=null,$type="main"){
$config = SConfig::getConfig(self::$_config,$zone);
if(isset($config->$type)){
return $config->$type;
}elseif(isset($config->main)){
return $config->main;
}
return;
}
/**
* 切换数据库配置文件
* @param string $zone
* @param string $type main|query
* @return array
*/
function useConfig($zone,$type="main"){
$config = self::getConfig($zone,$type);
if(is_object($config)){
self::init($config);
}elseif(is_array($config)){
self::init($config[array_rand($config)]);
}
}
private $foreign_keys =array();
private $table_name=""; //表名,默认和 类名 user 一样
/**
* 字段属性结构
* $user->id=1;
* $user->name="XX";
* $user->nickname="nickname";
* $user->profile->address="my address";
* $user->profile->comment="comment";
*/
private $_fields;
public function __get($k){
if(isset($this->_fields->$k)){
return $this->_fields->$k;
}else{
//判断是不是属于外键
foreach($this->foreign_keys as $k2=>$v2){
$tmp = explode(".",$v2);
if(!empty($tmp[0]) && !empty($tmp[1])){
$tbl_name = trim($tmp[0]);
if($k==$tbl_name)
return $this->_fields->$k = new stdclass;
}
}
}
return null;
}
public function __set($k,$v){
$this->_fields->$k = $v;
}
/**
* 构造方法
* @param string $table_name
* @return void
**/
public function __construct($table_name="",$config=array()){
if(!empty($table_name))$this->table_name = $table_name;
if(!empty($config))parent::init($config);
}
/*
* 按条件获取所有信息
* @param array $condition 条件,参照Db::select()里的定义
* @param boolean $foreign_info 是否返回外键信息
* @return array
*/
public function listAll($condition,$foreign_info=false){
$result = parent::select($this->table_name,$condition);
if(!empty($result->items)){
foreach($result->items as &$r){
//获取外键信息
if($foreign_info){
foreach($this->foreign_keys as $k=>$v){
$tmp = explode(".",$v);
if(!empty($tmp[0]) && !empty($tmp[1]) && isset($r[$k])){
$tbl_name = trim($tmp[0]);
$condition = array(trim($tmp[1])=>$r[$k]);
$result2 = parent::select($tbl_name,$condition);
if(!empty($result2->items)){
if(count($result2->items)==1){
$r[$tbl_name]=$result2->items[0];
}else{
$r[$tbl_name]=$result2->items;
}
}
}
}
}
}
return $result->items;
}
return false;
}
/**
* 重设所有参数
**/
public function reset(){
$this->_fields = new stdclass;
}
/**
* 得到带外键的信息
**/
public function getAll(){
return $this->get(true);
}
/**
* 得到信息,返回数组,可以用对像获取本身
**/
public function get($foreign_info=false){
if(!empty($this->_fields)){
$condition=array();
foreach($this->_fields as $k=>$v){
if(!is_object($v))$condition[$k]=$v;
}
if(!empty($condition)){
$items = $this->listAll($condition,$foreign_info);
if($items){
$r = $items[0];
//设置信息
foreach($r as $k=>$v){
if(is_array($v)){
foreach($v as $k2=>$v2){
$this->_fields->$k->$k2=$v2;
}
}else{
$this->_fields->$k=$v;
}
}
return $r;
}
}
}
return false;
}
/**
* 删除信息,可以连带外键一起删除
**/
function del($foreign_info=false){
if(!empty($this->_fields)){
$condition=array();
foreach($this->_fields as $k=>$v){
if(!is_object($v))$condition[$k]=$v;
}
if(!empty($condition)){
if($this->get()!==false){
$result = parent::delete($this->table_name,$condition);
if($result!==false){
if($foreign_info){
foreach($this->foreign_keys as $k=>$v){
$tmp = explode(".",$v);
if(!empty($tmp[0]) && !empty($tmp[1]) && isset($this->_fields->$k)){
$tbl_name = trim($tmp[0]);
$condition = array(trim($tmp[1])=>$this->_fields->$k);
parent::delete($tbl_name,$condition);
}
}
}
}
return $result;
}
}
}
return false;
}
/**
* 保存信息,支持外键属性保存
* 当外键属性保存时,特别注意:
* 你必须初始化外键的值,否则可能无效,如:
* $test->user_profile = new stdclass;
* $test->user_profile->field_name = "field_value";
*/
public function set(){
$r = false;
if(!empty($this->_fields)){
$condition=array();
foreach($this->_fields as $k=>$v){
if(!is_object($v))$condition[$k]=$v;
}
if(count($condition)>1){
//当只有修改2个以上字段时才更新
$r = parent::insert($this->table_name,$condition,false,false,$condition);
}
//更新外键信息
foreach($this->foreign_keys as $k=>$v){
//判断外键有没有值,如果没有值,必须重新获取
//TODO
$tmp = explode(".",$v);
if(!empty($tmp[0]) && !empty($tmp[1])){
$tbl_name = trim($tmp[0]);
$filed_name = trim($tmp[1]);
//有外键设置
if(isset($this->_fields->$tbl_name) && is_object($this->_fields->$tbl_name)){
//判断关联主键有没有条件
if(!isset($this->_fields->$k)){
$this->get(false);
}
//如果能获取到才修改
if(isset($this->_fields->$k)){
$items = $this->_fields->$tbl_name;
//把外键条件加入进去
$items->$filed_name = $this->_fields->$k;
$r = parent::insert($tbl_name,$items,false,false,$items);
}
}
}
}
}
return $r;
}
/**
* 设置外键关联
*/
public function setForeignKey($keys=array()){
$this->foreign_keys = $keys;
}
}