-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleORM.php
More file actions
377 lines (331 loc) · 8.45 KB
/
SimpleORM.php
File metadata and controls
377 lines (331 loc) · 8.45 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300)
die('Requires PHP 5.3 or higher');
/**
* Simple ORM
*
* Is a small class to implement a Active Record Design Pattern
*
* Minimum requirements
* - PHP 5.3+
* - PDO drivers for the database you are using
*
* @author Diego Tolentino <diegotolentino@gmail.com>
* @license FreeBSD
*/
abstract class SimpleORM {
/**
* Connect object, implements the Singleton Design Pattern
* @var PDO
*/
protected static $db = null;
/**
* Data base table of the class
*
* @var string
*/
protected static $table_name = null;
/**
* Pk of the table, default 'id'
*
* @var string
*/
protected static $table_pk = 'id';
/**
* Fields of the table, excluding Pk
* Ex:
* <code>
* array('first_name', 'last_name');
* </code>
*
* @var array
*/
protected static $fields = array();
/**
* Fields to check if filled before DB::save() or when call DB::isValid()
*
* <code>
* array(
* //field name required (will be show as "Last Name")
* array('last_name'),
*
* //plus affordable name to show on mensage
* array('email', 'name'=>'E-Mail'),
*
* //affordable mensage to show on require error
* array('first_name', 'message' => 'é obrigatório')
* );
* </code>
*
* @link DB::save()
* @link DB::isValid()
* @var array
*/
protected static $validates_presence_of = array();
/**
* Object to manage errors on bussines rules triggered by DB::save() and DB::isValid()
*
* @link DB::save()
* @link DB::isValid()
* @var Errors
*/
public $errors = null;
public function __construct($aDefaults = null) {
/*set null for all fields*/
foreach (static::$fields as $key) {
$this->$key = null;
}
/*if have defaults, set the default value for fields*/
if ($aDefaults) {
foreach ($aDefaults as $key => $val) {
$this->$key = $val;
}
}
$this->errors = new Errors();
}
/**
* Display debug info
*
* @param string $string
*/
static protected function debug($string) {
/**
* check if show debug info
*/
if (ini_get('display_errors')) {
$aux = debug_backtrace();
$sClassFunc = $aux[1]['class'] . ':' . $aux[1]['function'];
$sFileLine = substr(strrchr($aux[1]['file'], DIRECTORY_SEPARATOR), 1) . ':' . $aux[1]['line'];
echo "<br><b>Debug - $sClassFunc ($sFileLine)</b><br><pre>\t" . wordwrap($string, 80) . '</pre><br>';
}
}
/**
* DB::get_row() Seleciona um unico registro
*
* @param $sql string - SQL
* @return array
*/
public static function findAllBy($field, $value, $limit = null, $orderBy = null) {
$sql = 'select * from ' . static::$table_name . " where $field='$value' ";
if ($limit)
$sql .= "limit $limit ";
if ($orderBy)
$sql .= "order by $orderBy ";
/*send debug info*/
self::debug($sql);
$aDados = self::findBySql($sql);
$aResult = array();
foreach ($aDados as $row) {
$o = new static;
foreach ($row as $key => $val) {
$o->$key = $val;
}
$aResult[] = $o;
}
return $aResult;
}
/**
* Check if the record is valid and return bool, you can view the info using $o->errors->* functions
*
* @return boolean
*/
public function isValid() {
$this->errors->reset();
/*validate required fields*/
foreach (static::$validates_presence_of as $aValidate) {
if (!isset($this->$aValidate[0]) || !$this->$aValidate[0]) {
/*Define field name*/
if (isset($aValidate['name'])) {
$sField = $aValidate['name'];
} else {
$sField = ucwords(str_replace('_', ' ', $aValidate[0]));
}
/*Define mensage*/
if (isset($aValidate['message'])) {
$sMsg = $aValidate['message'];
} else {
$sMsg = 'is required';
}
$this->errors->add($sField, $sMsg);
}
}
/*validate user rules*/
$this->validate();
return $this->errors->count() == 0;
}
/**
* DB::findBySql() Seleciona varios registros
*
* @param $sql string - SQL
* @throws Exception
* @return array
*/
protected static function findBySql($sql) {
/*run sql*/
$oStatement = self::run($sql);
/*fetch data*/
$aReturn = array();
while ($aRow = $oStatement->fetch(PDO::FETCH_ASSOC)) {
$aReturn[] = $aRow;
}
return $aReturn;
}
/**
* Find record by id
*
* @param integer|mixed $id
* @return static
*/
public static function find($id) {
return static::findOneBy(static::$table_pk, $id);
}
/**
* Find record by $field=$value pair
*
* @param string $field
* @param mixed $value
* @return static
*/
public static function findOneBy($field, $value) {
$aResult = static::findAllBy($field, $value, 1);
return isset($aResult[0]) ? $aResult[0] : null;
}
/**
* Save the record
*
* @throws Exception
*/
public function save() {
/*if the record is not valid, throw exception*/
if (!$this->isValid()) {
throw new Exception(join("\n", $this->errors->full_messages()));
}
/*start build the field=value association*/
$fields = '';
foreach (static::$fields as $key) {
$fields .= ($fields ? ', ' : '');
if (isset($this->$key) && $this->$key !== null && $this->$key !== '') {
$fields .= $key . ' = \'' . str_replace("'", "`", $this->$key) . '\'';
} else {
$fields .= $key . ' = null';
}
}
/*check if the pk is defined*/
if (isset($this->{static::$table_pk}) && $this->{static::$table_pk} != '') {
$sql = 'UPDATE ' . static::$table_name;
$sql .= ' SET ' . $fields;
$sql .= ' WHERE ' . static::$table_pk . '="' . $this->{static::$table_pk} . '"';
} else {
$sql = 'INSERT INTO ' . static::$table_name . ' SET ' . $fields;
}
/*send debug info*/
self::debug($sql);
/*run sql*/
self::run($sql);
/*if not pk isset(new record)*/
if (!isset($this->{self::$table_pk}) || !$this->{self::$table_pk}) {
$this->{self::$table_pk} = self::$db->lastInsertId();
}
return $this->{self::$table_pk};
}
/**
* Delete de current row
*
* @throws Exception
* @return number of affected rows
*/
public function delete() {
if (!isset($this->{self::$table_pk}) || !$this->{self::$table_pk}) {
throw new Exception(self::$table_pk . '.' . self::$table_pk . ' not is defined.');
}
$sql = 'DELETE FROM ' . static::$table_name . ' WHERE ' . static::$table_pk . '="' . $this->{static::$table_pk} . '"';
/*send debug info*/
self::debug($sql);
/*run the statement*/
$oStatement = self::run($sql);
/*return the number of affected rows*/
return $oStatement->rowCount();
}
/**
* Setup connection
*
* @throws Exception
*/
protected static function setup() {
try {
$sConn = 'mysql:host=' . db_host . '; dbname=' . db_name;
self::$db = new PDO($sConn, db_user, db_pass, array(PDO::ATTR_PERSISTENT => true));
} catch (PDOException $oPDOEx) {
throw new Exception($oPDOEx->getMessage());
}
}
/**
* Run the sql and return the statement
*
* @return PDOStatement
*/
protected static function run($sql) {
/*if not have connection, setup it*/
if (!self::$db) {
self::setup();
}
/*prepare the statement*/
$statement = self::$db->prepare($sql);
/*execute the statement, throw when have error */
if ($statement->execute() === false) {
$aErroPDO = $statement->errorInfo();
$sErroPDO = $aErroPDO[2] . ' - "' . $sql . '"';
throw new Exception($sErroPDO);
}
return $statement;
}
/**
* Define this function with your business rules
*/
abstract protected function validate();
}
class Errors {
protected $aErrors = null;
function __construct() {
$this->errors = array();
}
/**
* Add info to error array
*
* @param string $field
* @param string $msg
*/
function add($field, $msg) {
$this->aErrors[$field][] = $msg;
}
/**
* Return array with the full field+errors info
*
* @return array
*/
function full_messages() {
$sResult = array();
if ($this->aErrors) {
foreach ($this->aErrors as $field => $aErros) {
foreach ($aErros as $sError) {
$sResult[] = ucfirst($field) . ': ' . $sError;
}
}
}
return $sResult;
}
/**
* Reset the error info
*/
function reset() {
$this->aErrors = array();
}
/**
* Return the count of errors
*
* @return number
*/
function count() {
return count($this->aErrors);
}
}