-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.php
136 lines (121 loc) · 3.6 KB
/
Model.php
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
<?php
require_once 'config.php';
/**
*
*/
class Model
{
private $conn;
private $table = null;
private $dbms = 'mysql';
private $dbName = null;
public function __construct($table = null, $servername = "127.0.0.1", $username = "root", $password = "yingtao", $dbName = 'basic')
{
$this->table = $table;
$this->dbName = $basic;
try {
$this->conn = new PDO("mysql:host=" . $servername . ';dbname=' . $dbName . ';', $username, $password);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
/**
* [query 执行Sql 返回影响条数]
* @param [type] $sql [description]
* @return [type] [description]
*/
public function exec($sql)
{
$count = $this->conn->exec($sql);
return $count;
}
/**
* [query 执行Sql 返回数据]
* @param [type] $sql [description]
* @return [type] [description]
*/
public function query($sql)
{
$data = $this->conn->query($sql);
return $data;
}
/**
* [insert 添加数据]
* @param [type] $data [description]
* @return [type] [description]
*/
public function insert($data)
{
// 数据处理
foreach ($data as $k => $v) {
$value = $this->_returnValue($v);
if (is_scalar($value)) {
$values[] = $value;
$fields[] = $this->_returnField($k);
}
}
//创建时间,假如表里有此字段并且添加的时候未定义
if (!array_key_exists('created_at', $data) && $this->columnExist('created_at') == 0) {
$fields[] = $this->_returnField('created_at');
$values[] = $this->conn->quote(date('Y-m-d H:i:s', time()));
}
//更新时间,假如表里有此字段并且添加的时候未定义
if (!array_key_exists('updated_at', $data) && $this->columnExist('updated_at') == 0) {
$fields[] = $this->_returnField('updated_at');
$values[] = $this->conn->quote(date('Y-m-d H:i:s', time()));
}
$sql = 'insert into ' . $this->table . ' (' . implode(',', $fields) . ') VALUES (' . implode(',', $values) . ')';
return $this->exec($sql);
}
public function getInsertId()
{
$id = $this->conn->lastInsertId();
return $id;
}
/**
* 解析字段名,防止字段名是关键字
*
* @param unknown $value
* @return string
*/
private function _returnField($fieldName)
{
return '`' . $fieldName . '`';
}
/**
* 根据值的类型返回SQL语句式的值
*
* @param unknown $value
* @return string
*/
private function _returnValue($value)
{
if (is_int($value) || is_float($value)) {
return $value;
} else {
return $this->_returnStr($value);
}
}
/**
* 格式化用于数据库的字符串
*
* @param unknown $value
* @return string
*/
private function _returnStr($value)
{
$value = mysql_real_escape_string($value, $this->conn);
return $this->conn->quote($value);
}
/**
* [columnExist 确定数据表中指定字段是否存在]
* @param [type] $column [description]
* @return [type] [description]
*/
public function columnExist($column)
{
$sql = "select count(*) from " . $this->dbName . ".columns where table_name = '" . $this->table . "' and column_name = '" . $column . "'";
$data = $this->conn->query($sql);
return $data;
}
}