-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.php
executable file
·575 lines (484 loc) · 13.2 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
namespace jmvc;
class DataException extends \Exception {
protected $field;
protected $data;
public function __construct($field, $message, $data=null)
{
$this->field = $field;
$this->data = $data;
parent::__construct($message);
}
public function getField()
{
return $this->field;
}
public function getData()
{
return $this->data;
}
}
abstract class Model {
protected $_values = array();
protected $_dirty_values = array();
protected $_loaded = false;
protected $_criteria = false;
protected $_obj_id = false;
private static $db;
private static $cache;
protected static $obj_cache = array();
protected static $obj_cache_count = array();
protected static $_table;
protected static $_find_query;
protected static $_find_prefix = '';
protected static $_find_order = null;
protected static $_field_types = array();
protected static $_group_by = false;
/**
* Create a new object. If an ID or criteria array is passed, populate the object with data from MySQL
* @param mixed $id
* @return model subclass
*/
public function __construct($id=false)
{
if ($id) {
if (is_array($id)) {
$this->_criteria = $id;
} else {
$this->_criteria = array('id'=>$id);
}
if (is_numeric($id)) {
$this->_obj_id = $id;
}
}
}
/**
* Retrieve an object from MySQL. If the object is not found, return null. Takes the same arguments as __construct()
* @param mixed $id
* @return model subclass
*/
public static function factory($id=false)
{
if (is_numeric($id) && isset(self::$obj_cache[static::$_table][$id])) {
return self::$obj_cache[static::$_table][$id];
}
$type = get_called_class();
$obj = new $type($id);
if ($obj->valid()) {
return $obj;
} else {
return null;
}
}
public function __get($key)
{
if (array_key_exists($key, $this->_dirty_values)) {
return $this->_dirty_values[$key];
}
if (!$this->_loaded) {
$this->load();
}
if (array_key_exists($key, $this->_values)) {
$data = $this->_values[$key];
}
if (isset(static::$_field_types[$key])) {
if (static::$_field_types[$key] == 'array' && !is_array($data)) {
if (empty($data)) {
$this->_values[$key] = array();
} else {
$this->_values[$key] = unserialize($data);
$data = $this->_values[$key];
}
} else if (static::$_field_types[$key] == 'json' && !is_array($data)) {
if (empty($data)) {
$this->_values[$key] = array();
} else {
$this->_values[$key] = json_decode($data, true);
$data = $this->_values[$key];
}
}
}
return $data;
}
public function id()
{
if (isset($this->_obj_id)) {
return $this->_obj_id;
} else {
throw new \Exception('Invalid acces to _obj_id!');
}
}
public function __set($key, $value)
{
if ($key[0] == '_') {
$this->$key = $value;
return;
}
if (!$this->_loaded) {
$this->load();
}
if (!isset($this->_values[$key]) && $value !== NULL) {
$this->_dirty_values[$key] = $value;
} else if (is_numeric($value) && is_numeric($this->$key)) {
if ($value*1.0 != $this->$key*1.0) {
$this->_dirty_values[$key] = $value;
}
} else if ($value !== $this->$key) {
$this->_dirty_values[$key] = $value;
}
}
/**
* Set an object parameter to an SQL function. $value will go unquoted, leaving
* all SQL injection protection to the caller. Useful for setting things
* to NOW(), for example
* @param type $key
* @param type $value
* @return void
*/
public function set_raw($key, $value)
{
$this->_dirty_values[$key] = array('raw'=>$value);
}
public function __isset($key)
{
if (isset($this->_dirty_values[$key])) {
return true;
}
if (!$this->_loaded) {
$this->load();
}
if (isset($this->_values[$key])) {
return true;
}
return false;
}
public function __unset($key)
{
unset($this->_dirty_values[$key]);
unset($this->_values[$key]);
}
/**
* Retrieve an instance of the database
* @return \jmvc\Db
*/
protected static function db()
{
if (!self::$db) {
self::$db = Db::instance();
}
return self::$db;
}
/**
* Retrieve an instance of the cache driver
* @return \jmvc\classes\Cache_interface
*/
protected static function cache()
{
if (!self::$cache) {
if (isset($GLOBALS['_CONFIG']['cache_driver'])) {
$class = $GLOBALS['_CONFIG']['cache_driver'];
self::$cache = $class::instance();
} else {
self::$cache = \jmvc\classes\Memcache_Stub::instance();
}
}
return self::$cache;
}
/**
* Populate the object. If an array is passed, populate the object with passed data.
* Otherwise, retrieve data from MySQL.
* @param array $data
* @return void
*/
public function load($data=false)
{
if ($this->_obj_id) $this->_criteria = array('id'=>$this->_obj_id);
if (!$data && $this->_criteria) {
$data = self::find_one($this->_criteria);
}
$this->_loaded = true;
if (!$data) {
if ($this->_obj_id) {
$this->_values = array();
$this->_obj_id = null;
self::$obj_cache[static::$_table][$this->_obj_id] = null;
}
return false;
}
$this->_values = $data;
$this->_obj_id = $data['id'];
// cache the object in memory for the duration of the request
self::$obj_cache[static::$_table][$this->_obj_id] = $this;
}
/**
* Load an array of data to be saved. Array keys should match the table columns.
* @param array $data
* @return void
*/
public function load_dirty($data)
{
$this->_dirty_values = array_merge($this->_dirty_values, $data);
}
/**
* Retrieve the object data in array form
* @return array
*/
public function get_data()
{
if (!$this->_loaded) {
$this->load();
}
return array_merge($this->_values, $this->_dirty_values);
}
/**
* Generate SQL for WHERE/HAVING based on passed criteria array
* @param array $criteria
* @return string
*/
protected static function make_criteria($criteria)
{
$sql = self::make_where($criteria);
if (static::$_group_by) {
$sql .= ' GROUP BY '.static::$_group_by;
if (isset($criteria['having']) && !empty($criteria['having'])) {
$sql .= ' HAVING '.implode(' AND ', $criteria['having']);
}
}
return $sql;
}
/**
* Generate SQL for WHERE based on passed criteria array
* @param array $criteria
* @return string
*/
protected static function make_where($criteria)
{
if (empty($criteria)) {
return '';
}
$where = array();
foreach ($criteria as $key => $value) {
if ($key == 'having') { //non-where argument passed with criteria
continue;
} else {
$where[] = self::db()->make_parameter($key, $value, static::$_find_prefix);
}
}
if (!empty($where)) {
return 'WHERE '.implode(' AND ', $where);
}
}
/**
* Retrieve an group of objects based on the passed criteria.
* @param array $criteria Array keys match table columns. Arguments will be ANDed. Use MySQL dot notation
* to access columns in other tables. Set key 'raw_sql' to an array to add unquoted
* arguments to the query.
* @param mixed $limit May be null, an integer, or array in the form { page: int, page_size: int}.
* @param string $order This is unquoted SQL. It's up to the caller to protect against SQL injection
* @param bool $keyed Return an array with object IDs as array keys
* @return array of model subclass
*/
public static function find($criteria, $limit=false, $order=false, $keyed=false)
{
if (static::$_find_query) {
$sql = str_replace('[[WHERE]]', static::make_criteria($criteria), static::$_find_query);
if ($order) {
$sql .= ' ORDER BY '.$order;
} else if (static::$_find_order) {
$sql .= ' ORDER BY '.static::$_find_order;
}
} else {
$sql = 'SELECT * FROM '.static::$_table.' '.static::make_criteria($criteria).' ORDER BY id';
}
if ($limit) {
if (is_array($limit)) {
$sql .= ' LIMIT '.($limit['page']*$limit['page_size']).', '.$limit['page_size'];
} else if (is_numeric($limit)) {
$sql .= ' LIMIT '.$limit;
}
}
$rows = self::db()->get_rows($sql);
if (!$rows) {
return null;
}
$classname = get_called_class();
$outp = array();
foreach ($rows as $row) {
$obj = new $classname();
$obj->load($row);
if ($keyed) {
$outp[$obj->id] = $obj;
} else {
$outp[] = $obj;
}
}
return $outp;
}
/**
* Get a row count for a particular criteria
* @param array $criteria
* @return int
*/
public static function find_count($criteria)
{
if (static::$_group_by) {
$group = static::$_group_by;
static::$_group_by = null;
}
if (isset(static::$_count_query)) {
$sql = str_replace('[[WHERE]]', static::make_criteria($criteria), static::$_count_query);
} else {
$sql = 'SELECT COUNT(*) FROM '.static::$_table.' '.static::make_criteria($criteria);
}
if ($group) {
static::$_group_by = $group;
}
return self::db()->get_row($sql);
}
/**
* Retrieve the data array for 1 object from MySQL
* @param array $criteria
* @return array
*/
protected static function find_one($criteria)
{
if (static::$_find_query) {
$sql = str_replace('[[WHERE]]', static::make_criteria($criteria), static::$_find_query);
} else {
$sql = 'SELECT * FROM '.static::$_table.' '.static::make_criteria($criteria);
}
$sql .= ' LIMIT 1';
return self::db()->get_row($sql);
}
/**
* Return an array of all obbjects in MySQL
* @param string $order This is unquoted SQL. It is up to the caller to protected agains SQL injection.
* @return array of model subclass
*/
public static function find_all($order=false)
{
if (static::$_find_query) {
$sql = str_replace('[[WHERE]]', static::make_criteria($criteria), static::$_find_query);
if ($order) {
$sql .= ' ORDER BY '.$order;
} else if (static::$_find_order) {
$sql .= ' ORDER BY '.static::$_find_order;
}
} else {
$sql = 'SELECT * FROM '.static::$_table.' ORDER BY id';
}
$rows = self::db()->get_rows($sql);
if (!$rows) return null;
$classname = get_called_class();
$outp = array();
foreach ($rows as $row) {
$obj = new $classname();
$obj->load($row);
$outp[] = $obj;
}
return $outp;
}
/**
* Get the total number of objects in the database.
* @return int
*/
public static function find_all_count()
{
return self::db()->get_row('SELECT COUNT(*) FROM '.static::$_table);
}
/**
* MySQL quote a value to protect against SQL injection. Wraps \jmvc\Db::quote().
* @param string $value
* @return string
*/
protected static function quote($value)
{
return self::db()->quote($value);
}
/**
* MySQL quote a datetime. Wraps \jmvc\Db::quote_date().
* @param string $value
* @return int
*/
protected static function quote_date($value)
{
return self::db()->quote_date($value);
}
/**
* Check if the object has data that has not been saved to MySQL
* @return bool
*/
public function is_dirty()
{
return (!empty($this->_dirty_values));
}
/**
* Save object to MySQL
* @param bool $reload Reload the object data after saving. May be necessary to
* get correct join or MySQL generated data, but causes extra
* DB hit. Defaults to true.
* @return void
*/
public function save($reload=true)
{
if (method_exists($this, 'before_save')) {
$before_out = $this->before_save();
}
if (!$this->is_dirty()) {
return;
}
foreach (array_keys($this->_dirty_values) as $key) {
if (isset(static::$_field_types[$key])) {
if (static::$_field_types[$key] == 'array' && is_array($this->_dirty_values[$key])) {
$this->_dirty_values[$key] = serialize($this->_dirty_values[$key]);
} else if (static::$_field_types[$key] == 'json' && is_array($this->_dirty_values[$key])) {
$this->_dirty_values[$key] = json_encode($this->_dirty_values[$key]);
}
}
}
if ($this->_obj_id) {
// update
$sql = self::db()->make_update(static::$_table, $this->_dirty_values, array('id'=>$this->_obj_id), static::$_field_types);
self::db()->update($sql);
} else {
// insert
$sql = self::db()->make_insert(static::$_table, $this->_dirty_values, static::$_field_types);
$insert_id = self::db()->insert($sql);
$this->_obj_id = ($this->_dirty_values['id']) ?: $insert_id;
}
if ($reload) {
// refresh the data next time we need to access it
$this->_loaded = false;
} else {
array_merge($this->_values, $this->_dirty_values);
}
if (method_exists($this, 'after_save')) {
$this->after_save($before_out);
}
$this->_dirty_values = array();
}
/**
* Delete the object from MySQL
* @return void
*/
public function delete()
{
$sql = 'DELETE FROM '.static::$_table.' WHERE id="'.$this->_obj_id.'"';
self::db()->delete($sql);
}
/**
* Check if the object corresponds to a MySQL row.
* @return bool
*/
public function valid()
{
if (!$this->_loaded) {
$this->load();
}
if ($this->_obj_id) {
return true;
} else {
return false;
}
}
}