-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorm.php
194 lines (180 loc) · 6.48 KB
/
orm.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
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
<?php
include_once 'connection.php';
class ORM {
protected $_data = array();
protected $_timestamp = '';
protected $_table = '';
protected $_primaryKey = 'id';
public function __construct($id, $data = array()) {
if (empty($data)) {
$con = Connection::getConnection();
$query = "SELECT * FROM `" . $this->_table . "` WHERE `" . $this->_primaryKey . "`=$id";
$res = $con->query($query);
$res->data_seek(0);
$row = $res->fetch_assoc();
if ($row) {
$fields = array_keys($row);
foreach ($fields as $field) {
$this->_data[$field] = $row[$field];
}
} else {
throw new Exception('Cannot find entity ' . $id);
}
} else {
$this->_data = $data;
}
}
public function __toString() {
return '<pre>' . print_r($this->_data, true) . '</pre>';
}
public function save() {
if ($this->_timestamp) {
date_default_timezone_set("America/Toronto");
$dateObject = new DateTime('NOW');
$dateStr = $dateObject->format("Y-m-d H:i:s");
if (is_array($this->_timestamp)) {
foreach ($this->_timestamp as $timestamp) {
$this->_data[$timestamp] = $dateStr;
}
} else {
$this->_data[$timestamp] = $dateStr;
}
}
$databaseName = Connection::getDatabaseName();
$table = $this->_table;
$primaryKey = $this->_primaryKey;
$id = $this->_data[$this->_primaryKey];
$keyValue = array();
foreach (array_keys($this->_data) as $key) {
if (is_numeric($this->_data[$key])) {
$value = $this->_data[$key];
} else {
$value = "'" . $this->_data[$key] . "'";
}
$keyValue[] = " `$key` = $value ";
}
$dataStr = implode(',', $keyValue);
$query = "UPDATE `$databaseName`.`$table` SET $dataStr WHERE `$table`.`$primaryKey` = $id";
$con = Connection::getConnection();
$stmt = $con->prepare($query);
IF (!($stmt->execute())) {
return false;
}
$stmt->close();
return true;
}
public function __call($name, $arguments) {
$field = lcfirst($this->name2field($name));
if (array_key_exists($field, $this->_data)) {
if ($arguments) {
$this->_data[$field] = $arguments[0];
}
return $this->_data[$field];
}
}
private function name2field($name) {
return str_replace(
array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'), array('_a', '_b', '_c', '_d', '_e', '_f', '_g', '_h', '_i', '_j', '_k', '_l', '_m', '_n', '_o', '_p', '_q', '_r', '_s', '_t', '_u', '_v', '_w', '_x', '_y', '_z'), lcfirst($name)
);
}
public static function add($data) {
$databaseName = Connection::getDatabaseName();
$className = get_called_class();
$table = lcfirst(get_called_class());
$primaryKey = 'id';
$keyValue = array($primaryKey => 'NULL');
foreach (array_keys($data) as $key) {
if (is_numeric($data[$key])) {
$value = $data[$key];
} else {
$value = "'" . $data[$key] . "'";
}
$keyValue["`$key`"] = $value;
}
$fields = implode(',', array_keys($keyValue));
$questions = array();
for ($i = 0; $i < count($keyValue); $i++) {
$questions[] = '?';
}
$questionStr = implode(',', $questions);
$values = implode(',', array_values($keyValue));
$query = "INSERT INTO `$databaseName`.`$table`($fields) VALUES ($values);";
$con = Connection::getConnection();
$stmt = $con->prepare($query);
if (!($stmt->execute())) {
return false;
}
$id = $con->insert_id;
$stmt->close();
$object = new $className($id);
$object->touch();
return $object;
}
public function touch() {
if ($this->_timestamp) {
$this->save();
}
}
public static function search($criteria, $orderBy = null, $limit = null) {
$databaseName = Connection::getDatabaseName();
$className = get_called_class();
$table = lcfirst(get_called_class());
$con = Connection::getConnection();
$query = "SELECT * FROM `$databaseName`.`$table`";
$where = array();
foreach ($criteria as $field => $match) {
$match = str_replace("'", "", $match);
if (!is_numeric($match) && !is_array($match)) {
$match = "'$match'";
}
if (is_numeric($field)) {
$where[] = "`id` = $match";
} elseif (strpos($field, '?') === false) {
if (is_array($match)) {
$orConds = array();
foreach ($match as $value) {
$orConds[] = "`$field` = $value";
}
$where[] = '( ' . implode(' OR ', $orConds) . ' ) ';
} else {
$where[] = "`$field` = $match";
}
} else {
$where[] = str_replace('?', $match, $field);
}
}
$whereStr = implode(" AND ", $where);
if($where){
$whereStr = " WHERE ".$whereStr;
}
$orderByStr = '';
$limitStr = '';
if ($orderBy) {
if (is_array($orderBy)) {
$orderByStr = 'ORDER BY ' . implode(' , ', $orderBy);
} else {
$orderByStr = 'ORDER BY ' . $orderBy;
}
}
if ($limit) {
$limitStr = "LIMIT $limit";
}
$query = " $query $whereStr $orderByStr $limitStr";
$query = "SELECT * FROM `userDetail` WHERE 1";
$result = array();
$res = $con->query($query);
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
if ($row) {
$data = array();
$fields = array_keys($row);
foreach ($fields as $field) {
$data[$field] = $row[$field];
}
$result[] = new $className(null, $data);
}
}
return $result;
}
}
?>