-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseObject.php
342 lines (268 loc) · 10.3 KB
/
DatabaseObject.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* @link http://www.akmnahid.com
* @author Nahid Hossain <[email protected]>
* @package akmnahid\simple-orm
* @copyright Copyright © Nahid Hossain, 2016
* @phone +8801727456280
* @created 27/03/2016 12:56 PM
* @license GNU GPL
*/
namespace akmnahid\simpleORM;
abstract class DatabaseObject implements DbInterface
{
/* @var $dbInstance \PDO */
protected static $dbInstance;
/**
* @return string
*/
public static function getPrimaryKey(){
return "id";
}
/**
* @param null $query
* @param array $bindParams
* @return static[]
*/
public static function findAll($query = null,$bindParams = []){
if($query instanceof \PDOStatement){
$sth = $query;
}
elseif(is_int($query)){
$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName()." WHERE `".static::getPrimaryKey()."`= ".$query );
}
elseif($query==null) {
$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName());
}
elseif(is_string($query)) {
$sth = static::getDbInstance()->prepare($query);
/* foreach ($bindParams as $key => &$val) {
$sth->bindParam($key, $val);
}*/
}
elseif(is_array($query)) {
//$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName());
$sql = array();
$l_it =0;;
foreach($query as $key => $value){
if(is_array($value)){
if(is_object($value[0]) && $value[0] instanceof Expression ){
//$sql[] = " " . $value[0] . " `" . $key . "` = " . $value[1] . " ";
$glue = "AND";
if(isset($value[1]))
$glue =$value[1];
if($l_it==0)
$glue="";
$operator = "=";
if(isset($value[2]))
$operator = $value[2];
if($value[0]->column != null){
// $sql[] = " `" . $value[1]->column . "` " . $value[0] . "'" . $value[1] . "' ";
$sql[] = " " . $glue . " " . $value[0]->column . " ".$operator." " . $value[0] . " ";
}
else {
$sql[] = " " . $glue . " `" . $key . "` ".$operator." " . $value[0] . " ";
}
//$sql[] = " `" . $key . "` " . $value[0] . "'" . $value[1] . "' ";
}else {
$sql[] = " " . $value[0] . " `" . $key . "` = :" . $key . " ";
$bindParams[":" . $key] = $value[1];
}
}
else {
if($l_it==0) {
$sql[] = " `".$key . "` = :" . $key . " ";
$bindParams[":" . $key] = $value;
}
else{
$sql[] = " AND `" . $key . "` = :" . $key . " ";
$bindParams[":" . $key] = $value;
}
}
$l_it++;
}
$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName()." WHERE ".implode(' ',$sql));
/*
//$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName());
$sql = array();
foreach($query as $key => $value){
$sql[]=" `".$key."` = :".$key." ";
$bindParams[":".$key] = $value;
}
$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName()." WHERE ".implode(' AND ',$sql));
*/
}
//echo $sth->queryString."\n<br>";
$sth->execute($bindParams);
return $sth->fetchAll(\PDO::FETCH_CLASS , get_called_class());
}
/**
* @param mixed $query
* @param array $bindParams
* @return static
*/
public static function findOne($query,$bindParams = []){
if($query instanceof \PDOStatement){
$sth = $query;
}
elseif(is_int($query)){
$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName()." WHERE `".static::getPrimaryKey()."`= ".$query );
}
elseif(is_array($query)) {
//$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName());
$sql = array();
$l_it =0;;
foreach($query as $key => $value){
if(is_array($value)){
if(is_object($value[0]) && $value[0] instanceof Expression ){
//$sql[] = " " . $value[0] . " `" . $key . "` = " . $value[1] . " ";
$glue = "AND";
if(isset($value[1]))
$glue =$value[1];
if($l_it==0)
$glue="";
$operator = "=";
if(isset($value[2]))
$operator = $value[2];
if($value[0]->column != null){
// $sql[] = " `" . $value[1]->column . "` " . $value[0] . "'" . $value[1] . "' ";
$sql[] = " " . $glue . " " . $value[0]->column . " ".$operator." " . $value[0] . " ";
}
else {
$sql[] = " " . $glue . " `" . $key . "` ".$operator." " . $value[0] . " ";
}
//$sql[] = " `" . $key . "` " . $value[0] . "'" . $value[1] . "' ";
}else {
$sql[] = " " . $value[0] . " `" . $key . "` = :" . $key . " ";
$bindParams[":" . $key] = $value[1];
}
}
else {
if($l_it==0) {
$sql[] = " `".$key . "` = :" . $key . " ";
$bindParams[":" . $key] = $value;
}
else{
$sql[] = " AND `" . $key . "` = :" . $key . " ";
$bindParams[":" . $key] = $value;
}
}
$l_it++;
}
$sth = static::getDbInstance()->prepare("SELECT * from " . static::getTableName()." WHERE ".implode(' ',$sql));
}
elseif(is_string($query)) {
$sth = static::getDbInstance()->prepare($query);
/* foreach ($bindParams as $key => &$val) {
$sth->bindParam($key, $val);
} */
}
// print_r($sth->debugDumpParams());
//echo $sth->queryString;
$sth->execute($bindParams);
return $sth->fetchObject(get_called_class());
}
/**
* Save current object as the row of table
* @return bool
* @throws \Exception
*/
public function save(){
$this->beforeSave();
/*
if($this->isNewRecord()){
//db insert operation
$cols = implode("`,`",$this->getColumns());
$values = implode(", :",$this->getColumns());
$sql = "INSERT INTO `".static::getTableName()."` ( `".$cols."` ) VALUES ( :".$values." )";
}
else{
*/
$sql="";
foreach(static::getColumns() as $column){
if(is_object($this->{$column}) && $this->{$column} instanceof Expression){
$sql[]= " `".$column."`=".$this->{$column};
}
else
$sql[]= " `".$column."`=:".$column;
}
if($this->isNewRecord()){
$sql = "INSERT INTO `" . static::getTableName() . "` SET " . implode(',', $sql);
}
else {
$sql = "Update `" . static::getTableName() . "` SET " . implode(',', $sql) . " WHERE `" . static::getPrimaryKey() . "`=" . (int)$this->{static::getPrimaryKey()};
}
try{
$stmt = static::getDbInstance()->prepare($sql);
foreach(static::getColumns() as $column){
if(is_object($this->{$column}) && $this->{$column} instanceof Expression){
continue;
}
$stmt->bindParam(":".$column,$this->{$column});
}
$stmt->execute();
if($this->isNewRecord()){
$this->{static::getPrimaryKey()} = static::getDbInstance()->lastInsertId();
}
}
catch (\PDOException $ex){
throw $ex;
}
catch(\Exception $ex){
throw $ex;
}
return true;
}
public function delete(){
$sql= "Delete From `".static::getTableName()."` WHERE `".static::getPrimaryKey()."`=".$this->{static::getPrimaryKey()};
echo $sql;
//static::getDbInstance()->query()
}
/**
* @return bool
*/
public function isNewRecord(){
if($this->{self::getPrimaryKey()}==null)
return true;
else return false;
}
/**
* @param \PDO $dbInstance
*/
public static function setDbInstance(\PDO &$dbInstance){
static::$dbInstance = $dbInstance;
static::getDbInstance()->exec("SET CHARACTER SET utf8");
// Set error reporting:
static::getDbInstance()->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
}
public static function createDbInstance($dsn,$username,$pass){
try{
static::$dbInstance = new \PDO($dsn, $username, $pass);
}
catch(\Exception $ex){
echo die($ex->getMessage());
// throw new \Exception("Cannot Create database instance ");
}
}
public static function getDbInstance(){
if(static::$dbInstance==null){
throw new \Exception("Database Instance not Set");
}
return static::$dbInstance;
}
/**
* @param $data []
*/
public function populate($data){
foreach($this->getColumns() as $key => $value){
if(array_key_exists($value,$data)){
$this->$value = $data[$value];
}
}
}
/**
*Execute before save
*/
public function beforeSave(){
}
}