1
|
|
|
<?php |
2
|
|
|
namespace puck; |
3
|
|
|
|
4
|
|
|
use puck\tools\Str; |
5
|
|
|
|
6
|
|
|
class Model { |
7
|
|
|
/** |
8
|
|
|
* Per page limit for pagination |
9
|
|
|
* |
10
|
|
|
* @var int |
11
|
|
|
*/ |
12
|
|
|
public static $pageLimit = 20; |
13
|
|
|
/** |
14
|
|
|
* Variable that holds total pages count of last paginate() query |
15
|
|
|
* |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
public static $totalPages = 0; |
19
|
|
|
/** |
20
|
|
|
* Models path |
21
|
|
|
* |
22
|
|
|
* @var modelPath |
23
|
|
|
*/ |
24
|
|
|
protected static $modelPath; |
25
|
|
|
/** |
26
|
|
|
* An array that holds object data |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public $data; |
31
|
|
|
/** |
32
|
|
|
* Flag to define is object is new or loaded from database |
33
|
|
|
* |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
public $isNew = true; |
37
|
|
|
/** |
38
|
|
|
* Return type: 'Array' to return results as array, 'Object' as object |
39
|
|
|
* 'Json' as json string |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $returnType = 'Object'; |
44
|
|
|
/** |
45
|
|
|
* An array that holds insert/update/select errors |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
public $errors = null; |
50
|
|
|
/** |
51
|
|
|
* Primary key for an object. 'id' is a default value. |
52
|
|
|
* |
53
|
|
|
* @var stating |
54
|
|
|
*/ |
55
|
|
|
protected $primaryKey = 'id'; |
56
|
|
|
/** |
57
|
|
|
* Table name for an object. Class name will be used by default |
58
|
|
|
* |
59
|
|
|
* @var stating |
60
|
|
|
*/ |
61
|
|
|
protected $dbTable; |
62
|
|
|
protected $dbConn = null; |
63
|
|
|
protected $prefix; |
64
|
|
|
/** |
65
|
|
|
* Working instance of MysqliDb created earlier |
66
|
|
|
* |
67
|
|
|
* @var Mysql |
68
|
|
|
*/ |
69
|
|
|
private $db; |
70
|
|
|
/** |
71
|
|
|
* An array that holds has* objects which should be loaded togeather with main |
72
|
|
|
* object togeather with main object |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
private $_with = Array(); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $data Data to preload on object creation |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function __construct() { |
82
|
|
|
$this->db = app('db')->connect($this->dbConn); |
83
|
|
|
if ($this->prefix) { |
84
|
|
|
$this->db = $this->db->setPrefix($this->prefix); |
85
|
|
|
} |
86
|
|
|
if (!$this->dbTable) { |
87
|
|
|
$classFull = get_class($this); |
88
|
|
|
$classArray = explode("\\", $classFull); |
89
|
|
|
$classSelf = array_pop($classArray); |
90
|
|
|
$classSelf = Str::snake($classSelf); |
91
|
|
|
$this->dbTable = $classSelf; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
$this->db = $this->db->table($this->dbTable); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Helper function to create a virtual table class |
98
|
|
|
* |
99
|
|
|
* @param string tableName Table name |
100
|
|
|
* @return dbObject |
101
|
|
|
*/ |
102
|
|
|
public static function table($tableName) { |
103
|
|
|
$tableName = preg_replace("/[^-a-z0-9_]+/i", '', $tableName); |
104
|
|
|
if (!class_exists($tableName)) |
105
|
|
|
eval ("class $tableName extends dbObject {}"); |
|
|
|
|
106
|
|
|
return new $tableName (); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Catches calls to undefined static methods. |
111
|
|
|
* |
112
|
|
|
* Transparently creating dbObject class to provide smooth API like name::get() name::orderBy()->get() |
113
|
|
|
* |
114
|
|
|
* @param string $method |
115
|
|
|
* @param mixed $arg |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public static function __callStatic($method, $arg) { |
120
|
|
|
$obj = new static; |
121
|
|
|
$result = call_user_func_array(array($obj, $method), $arg); |
122
|
|
|
if (method_exists($obj, $method)) |
123
|
|
|
return $result; |
124
|
|
|
return $obj; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function autoload($path = null) { |
128
|
|
|
if ($path) |
129
|
|
|
static::$modelPath = $path . "/"; |
|
|
|
|
130
|
|
|
else |
131
|
|
|
static::$modelPath = __DIR__ . "/models/"; |
|
|
|
|
132
|
|
|
spl_autoload_register("dbObject::dbObjectAutoload"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private static function dbObjectAutoload($classname) { |
|
|
|
|
136
|
|
|
$filename = static::$modelPath . $classname . ".php"; |
137
|
|
|
if (file_exists($filename)) |
138
|
|
|
include($filename); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Magic getter function |
143
|
|
|
* |
144
|
|
|
* @param $name Variable name |
145
|
|
|
* |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public function __get($name) { |
149
|
|
|
if (property_exists($this, 'hidden') && array_search($name, $this->hidden) !== false) |
|
|
|
|
150
|
|
|
return null; |
151
|
|
|
|
152
|
|
|
if (isset ($this->data[$name]) && $this->data[$name] instanceof dbObject) |
|
|
|
|
153
|
|
|
return $this->data[$name]; |
154
|
|
|
|
155
|
|
|
if (property_exists($this, 'relations') && isset ($this->relations[$name])) { |
|
|
|
|
156
|
|
|
$relationType = strtolower($this->relations[$name][0]); |
|
|
|
|
157
|
|
|
$modelName = $this->relations[$name][1]; |
|
|
|
|
158
|
|
|
switch ($relationType) { |
159
|
|
|
case 'hasone': |
160
|
|
|
$key = isset ($this->relations[$name][2]) ? $this->relations[$name][2] : $name; |
|
|
|
|
161
|
|
|
$obj = new $modelName; |
162
|
|
|
$obj->returnType = $this->returnType; |
163
|
|
|
return $this->data[$name] = $obj->byId($this->data[$key]); |
164
|
|
|
break; |
|
|
|
|
165
|
|
|
case 'hasmany': |
166
|
|
|
$key = $this->relations[$name][2]; |
|
|
|
|
167
|
|
|
$obj = new $modelName; |
168
|
|
|
$obj->returnType = $this->returnType; |
169
|
|
|
return $this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get(); |
170
|
|
|
break; |
|
|
|
|
171
|
|
|
default: |
172
|
|
|
break; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (isset ($this->data[$name])) |
177
|
|
|
return $this->data[$name]; |
178
|
|
|
|
179
|
|
|
if (property_exists($this->db, $name)) |
180
|
|
|
return $this->db->$name; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Magic setter function |
185
|
|
|
* |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
public function __set($name, $value) { |
189
|
|
|
if (property_exists($this, 'hidden') && array_search($name, $this->hidden) !== false) |
|
|
|
|
190
|
|
|
return; |
191
|
|
|
|
192
|
|
|
$this->data[$name] = $value; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function __isset($name) { |
196
|
|
|
if (isset ($this->data[$name])) |
197
|
|
|
return isset ($this->data[$name]); |
198
|
|
|
|
199
|
|
|
if (property_exists($this->db, $name)) |
200
|
|
|
return isset ($this->db->$name); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function __unset($name) { |
204
|
|
|
unset ($this->data[$name]); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Save or Update object |
209
|
|
|
* |
210
|
|
|
* @return mixed insert id or false in case of failure |
211
|
|
|
*/ |
212
|
|
|
public function save($data = null) { |
213
|
|
|
if ($this->isNew) |
214
|
|
|
return $this->insert(); |
215
|
|
|
return $this->update($data); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return mixed insert id or false in case of failure |
220
|
|
|
*/ |
221
|
|
|
public function insert() { |
222
|
|
|
if (!empty ($this->timestamps) && in_array("createdAt", $this->timestamps)) |
|
|
|
|
223
|
|
|
$this->createdAt = date("Y-m-d H:i:s"); |
|
|
|
|
224
|
|
|
$sqlData = $this->prepareData(); |
225
|
|
|
if (!$this->validate($sqlData)) |
226
|
|
|
return false; |
227
|
|
|
|
228
|
|
|
$id = $this->db->insert($this->dbTable, $sqlData); |
229
|
|
|
if (!empty ($this->primaryKey) && empty ($this->data[$this->primaryKey])) |
230
|
|
|
$this->data[$this->primaryKey] = $id; |
231
|
|
|
$this->isNew = false; |
232
|
|
|
|
233
|
|
|
return $id; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
private function prepareData() { |
237
|
|
|
$this->errors = Array(); |
238
|
|
|
$sqlData = Array(); |
239
|
|
|
if (count($this->data) == 0) |
240
|
|
|
return Array(); |
241
|
|
|
|
242
|
|
|
if (method_exists($this, "preLoad")) |
243
|
|
|
$this->preLoad($this->data); |
|
|
|
|
244
|
|
|
|
245
|
|
|
if (!$this->dbFields) |
|
|
|
|
246
|
|
|
return $this->data; |
247
|
|
|
|
248
|
|
|
foreach ($this->data as $key => &$value) { |
249
|
|
|
if ($value instanceof dbObject && $value->isNew == true) { |
|
|
|
|
250
|
|
|
$id = $value->save(); |
251
|
|
|
if ($id) |
252
|
|
|
$value = $id; |
253
|
|
|
else |
254
|
|
|
$this->errors = array_merge($this->errors, $value->errors); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (!in_array($key, array_keys($this->dbFields))) |
|
|
|
|
258
|
|
|
continue; |
259
|
|
|
|
260
|
|
|
if (!is_array($value)) { |
261
|
|
|
$sqlData[$key] = $value; |
262
|
|
|
continue; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if (isset ($this->jsonFields) && in_array($key, $this->jsonFields)) |
|
|
|
|
266
|
|
|
$sqlData[$key] = json_encode($value); |
267
|
|
|
else if (isset ($this->arrayFields) && in_array($key, $this->arrayFields)) |
|
|
|
|
268
|
|
|
$sqlData[$key] = implode("|", $value); |
269
|
|
|
else |
270
|
|
|
$sqlData[$key] = $value; |
271
|
|
|
} |
272
|
|
|
return $sqlData; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param array $data |
277
|
|
|
*/ |
278
|
|
|
private function validate($data) { |
279
|
|
|
if (!$this->dbFields) |
|
|
|
|
280
|
|
|
return true; |
281
|
|
|
|
282
|
|
|
foreach ($this->dbFields as $key => $desc) { |
|
|
|
|
283
|
|
|
$type = null; |
284
|
|
|
$required = false; |
285
|
|
|
if (isset ($data[$key])) |
286
|
|
|
$value = $data[$key]; |
287
|
|
|
else |
288
|
|
|
$value = null; |
289
|
|
|
|
290
|
|
|
if (is_array($value)) |
291
|
|
|
continue; |
292
|
|
|
|
293
|
|
|
if (isset ($desc[0])) |
294
|
|
|
$type = $desc[0]; |
295
|
|
|
if (isset ($desc[1]) && ($desc[1] == 'required')) |
296
|
|
|
$required = true; |
297
|
|
|
|
298
|
|
|
if ($required && strlen($value) == 0) { |
299
|
|
|
$this->errors[] = Array($this->dbTable . "." . $key => "is required"); |
300
|
|
|
continue; |
301
|
|
|
} |
302
|
|
|
if ($value == null) |
303
|
|
|
continue; |
304
|
|
|
|
305
|
|
|
switch ($type) { |
306
|
|
|
case "text"; |
|
|
|
|
307
|
|
|
$regexp = null; |
308
|
|
|
break; |
309
|
|
|
case "int": |
310
|
|
|
$regexp = "/^[0-9]*$/"; |
311
|
|
|
break; |
312
|
|
|
case "double": |
313
|
|
|
$regexp = "/^[0-9\.]*$/"; |
314
|
|
|
break; |
315
|
|
|
case "bool": |
316
|
|
|
$regexp = '/^[yes|no|0|1|true|false]$/i'; |
317
|
|
|
break; |
318
|
|
|
case "datetime": |
319
|
|
|
$regexp = "/^[0-9a-zA-Z -:]*$/"; |
320
|
|
|
break; |
321
|
|
|
default: |
322
|
|
|
$regexp = $type; |
323
|
|
|
break; |
324
|
|
|
} |
325
|
|
|
if (!$regexp) |
326
|
|
|
continue; |
327
|
|
|
|
328
|
|
|
if (!preg_match($regexp, $value)) { |
329
|
|
|
$this->errors[] = Array($this->dbTable . "." . $key => "$type validation failed"); |
330
|
|
|
continue; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
return !count($this->errors) > 0; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param array $data Optional update data to apply to the object |
338
|
|
|
*/ |
339
|
|
|
public function update($data = null) { |
340
|
|
|
if (empty ($this->dbFields)) |
|
|
|
|
341
|
|
|
return false; |
342
|
|
|
|
343
|
|
|
if (empty ($this->data[$this->primaryKey])) |
344
|
|
|
return false; |
345
|
|
|
|
346
|
|
|
if ($data) { |
347
|
|
|
foreach ($data as $k => $v) |
348
|
|
|
$this->$k = $v; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
if (!empty ($this->timestamps) && in_array("updatedAt", $this->timestamps)) |
|
|
|
|
352
|
|
|
$this->updatedAt = date("Y-m-d H:i:s"); |
|
|
|
|
353
|
|
|
|
354
|
|
|
$sqlData = $this->prepareData(); |
355
|
|
|
if (!$this->validate($sqlData)) |
356
|
|
|
return false; |
357
|
|
|
|
358
|
|
|
$this->db->where($this->primaryKey, $this->data[$this->primaryKey]); |
359
|
|
|
return $this->db->update($this->dbTable, $sqlData); |
|
|
|
|
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Delete method. Works only if object primaryKey is defined |
364
|
|
|
* |
365
|
|
|
* @return boolean Indicates success. 0 or 1. |
366
|
|
|
*/ |
367
|
|
|
public function delete() { |
368
|
|
|
if (empty ($this->data[$this->primaryKey])) |
369
|
|
|
return false; |
370
|
|
|
|
371
|
|
|
$this->db->where($this->primaryKey, $this->data[$this->primaryKey]); |
372
|
|
|
return $this->db->delete($this->dbTable); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* 当执行一个自身不存在的方法时,重定向到mysql类中去 |
377
|
|
|
* |
378
|
|
|
* @param string $method |
379
|
|
|
* @param mixed $arg |
380
|
|
|
* |
381
|
|
|
* @return mixed |
382
|
|
|
*/ |
383
|
|
|
public function __call($method, $arg) { |
384
|
|
|
if (method_exists($this, $method)) |
385
|
|
|
return call_user_func_array(array($this, $method), $arg); |
386
|
|
|
return call_user_func_array(array($this->db, $method), $arg);; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Converts object data to a JSON string. |
391
|
|
|
* |
392
|
|
|
* @return string Converted data |
393
|
|
|
*/ |
394
|
|
|
public function __toString() { |
395
|
|
|
return $this->toJson(); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Converts object data to a JSON string. |
400
|
|
|
* |
401
|
|
|
* @return string Converted data |
402
|
|
|
*/ |
403
|
|
|
public function toJson() { |
404
|
|
|
return json_encode($this->toArray()); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Converts object data to an associative array. |
409
|
|
|
* |
410
|
|
|
* @return array Converted data |
411
|
|
|
*/ |
412
|
|
|
public function toArray() { |
413
|
|
|
$data = $this->data; |
414
|
|
|
$this->processAllWith($data); |
415
|
|
|
foreach ($data as &$d) { |
416
|
|
|
if ($d instanceof dbObject) |
|
|
|
|
417
|
|
|
$d = $d->data; |
418
|
|
|
} |
419
|
|
|
return $data; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Function queries hasMany relations if needed and also converts hasOne object names |
424
|
|
|
* |
425
|
|
|
* @param array $data |
426
|
|
|
*/ |
427
|
|
|
private function processAllWith(&$data, $shouldReset = true) { |
428
|
|
|
if (count($this->_with) == 0) |
429
|
|
|
return; |
430
|
|
|
|
431
|
|
|
foreach ($this->_with as $name => $opts) { |
|
|
|
|
432
|
|
|
$relationType = strtolower($opts[0]); |
433
|
|
|
$modelName = $opts[1]; |
434
|
|
|
if ($relationType == 'hasone') { |
435
|
|
|
$obj = new $modelName; |
436
|
|
|
$table = $obj->dbTable; |
437
|
|
|
$primaryKey = $obj->primaryKey; |
438
|
|
|
|
439
|
|
|
if (!isset ($data[$table])) { |
440
|
|
|
$data[$name] = $this->$name; |
441
|
|
|
continue; |
442
|
|
|
} |
443
|
|
|
if ($data[$table][$primaryKey] === null) { |
444
|
|
|
$data[$name] = null; |
445
|
|
|
} else { |
446
|
|
|
if ($this->returnType == 'Object') { |
447
|
|
|
$item = new $modelName ($data[$table]); |
448
|
|
|
$item->returnType = $this->returnType; |
449
|
|
|
$item->isNew = false; |
450
|
|
|
$data[$name] = $item; |
451
|
|
|
} else { |
452
|
|
|
$data[$name] = $data[$table]; |
453
|
|
|
} |
454
|
|
|
} |
455
|
|
|
unset ($data[$table]); |
456
|
|
|
} else |
457
|
|
|
$data[$name] = $this->$name; |
458
|
|
|
} |
459
|
|
|
if ($shouldReset) |
460
|
|
|
$this->_with = Array(); |
|
|
|
|
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* Fetch all objects |
465
|
|
|
* |
466
|
|
|
* @access public |
467
|
|
|
* @param integer|array $limit Array to define SQL limit in format Array ($count, $offset) |
468
|
|
|
* or only $count |
469
|
|
|
* @param array|string $fields Array or coma separated list of fields to fetch |
470
|
|
|
* |
471
|
|
|
* @return array Array of dbObjects |
472
|
|
|
*/ |
473
|
|
|
protected function get($limit = null, $fields = null) { |
474
|
|
|
$objects = Array(); |
475
|
|
|
$this->processHasOneWith(); |
476
|
|
|
$results = $this->db->ArrayBuilder()->get($this->dbTable, $limit, $fields); |
|
|
|
|
477
|
|
|
if ($this->db->count == 0) |
478
|
|
|
return null; |
479
|
|
|
|
480
|
|
|
foreach ($results as &$r) { |
|
|
|
|
481
|
|
|
$this->processArrays($r); |
482
|
|
|
$this->data = $r; |
483
|
|
|
$this->processAllWith($r, false); |
484
|
|
|
if ($this->returnType == 'Object') { |
485
|
|
|
$item = new static ($r); |
|
|
|
|
486
|
|
|
$item->isNew = false; |
487
|
|
|
$objects[] = $item; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
$this->_with = Array(); |
|
|
|
|
491
|
|
|
if ($this->returnType == 'Object') |
492
|
|
|
return $objects; |
493
|
|
|
|
494
|
|
|
if ($this->returnType == 'Json') |
495
|
|
|
return json_encode($results); |
496
|
|
|
|
497
|
|
|
return $results; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
private function processHasOneWith() { |
501
|
|
|
if (count($this->_with) == 0) |
502
|
|
|
return; |
503
|
|
|
foreach ($this->_with as $name => $opts) { |
|
|
|
|
504
|
|
|
$relationType = strtolower($opts[0]); |
505
|
|
|
$modelName = $opts[1]; |
506
|
|
|
$key = null; |
507
|
|
|
if (isset ($opts[2])) |
508
|
|
|
$key = $opts[2]; |
509
|
|
|
if ($relationType == 'hasone') { |
510
|
|
|
$this->db->setQueryOption("MYSQLI_NESTJOIN"); |
511
|
|
|
$this->join($modelName, $key); |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Function to join object with another object. |
518
|
|
|
* |
519
|
|
|
* @access public |
520
|
|
|
* @param string $objectName Object Name |
521
|
|
|
* @param string $key Key for a join from primary object |
522
|
|
|
* @param string $joinType SQL join type: LEFT, RIGHT, INNER, OUTER |
523
|
|
|
* @param string $primaryKey SQL join On Second primaryKey |
524
|
|
|
* |
525
|
|
|
* @return dbObject |
526
|
|
|
*/ |
527
|
|
|
private function join($objectName, $key = null, $joinType = 'LEFT', $primaryKey = null) { |
528
|
|
|
$joinObj = new $objectName; |
529
|
|
|
if (!$key) |
|
|
|
|
530
|
|
|
$key = $objectName . "id"; |
531
|
|
|
|
532
|
|
|
if (!$primaryKey) |
|
|
|
|
533
|
|
|
$primaryKey = $this->db->getPrefix() . $joinObj->dbTable . "." . $joinObj->primaryKey; |
534
|
|
|
|
535
|
|
|
if (!strchr($key, '.')) |
536
|
|
|
$joinStr = $this->db->getPrefix() . $this->dbTable . ".{$key} = " . $primaryKey; |
537
|
|
|
else |
538
|
|
|
$joinStr = $this->db->getPrefix() . "{$key} = " . $primaryKey; |
539
|
|
|
|
540
|
|
|
$this->db->join($joinObj->dbTable, $joinStr, $joinType); |
541
|
|
|
return $this; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* @param array $data |
546
|
|
|
*/ |
547
|
|
|
private function processArrays(&$data) { |
548
|
|
|
if (isset ($this->jsonFields) && is_array($this->jsonFields)) { |
|
|
|
|
549
|
|
|
foreach ($this->jsonFields as $key) |
|
|
|
|
550
|
|
|
$data[$key] = json_decode($data[$key]); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
if (isset ($this->arrayFields) && is_array($this->arrayFields)) { |
|
|
|
|
554
|
|
|
foreach ($this->arrayFields as $key) |
|
|
|
|
555
|
|
|
$data[$key] = explode("|", $data[$key]); |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Function to get a total records count |
561
|
|
|
* |
562
|
|
|
* @return int |
563
|
|
|
*/ |
564
|
|
|
protected function count() { |
565
|
|
|
$res = $this->db->ArrayBuilder()->getValue($this->dbTable, "count(*)"); |
566
|
|
|
if (!$res) |
567
|
|
|
return 0; |
568
|
|
|
return $res; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Helper function to create dbObject with Json return type |
573
|
|
|
* |
574
|
|
|
* @return dbObject |
575
|
|
|
*/ |
576
|
|
|
private function JsonBuilder() { |
577
|
|
|
$this->returnType = 'Json'; |
578
|
|
|
return $this; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/* |
582
|
|
|
* Function building hasOne joins for get/getOne method |
583
|
|
|
*/ |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* Helper function to create dbObject with Array return type |
587
|
|
|
* |
588
|
|
|
* @return dbObject |
589
|
|
|
*/ |
590
|
|
|
private function ArrayBuilder() { |
591
|
|
|
$this->returnType = 'Array'; |
592
|
|
|
return $this; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Helper function to create dbObject with Object return type. |
597
|
|
|
* Added for consistency. Works same way as new $objname () |
598
|
|
|
* |
599
|
|
|
* @return dbObject |
600
|
|
|
*/ |
601
|
|
|
private function ObjectBuilder() { |
602
|
|
|
$this->returnType = 'Object'; |
603
|
|
|
return $this; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Get object by primary key. |
608
|
|
|
* |
609
|
|
|
* @access public |
610
|
|
|
* @param $id Primary Key |
611
|
|
|
* @param array|string $fields Array or coma separated list of fields to fetch |
612
|
|
|
* |
613
|
|
|
* @return dbObject|array |
614
|
|
|
*/ |
615
|
|
|
private function byId($id, $fields = null) { |
616
|
|
|
$this->db->where($this->db->getPrefix() . $this->dbTable . '.' . $this->primaryKey, $id); |
617
|
|
|
return $this->getOne($fields); |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* Convinient function to fetch one object. Mostly will be togeather with where() |
622
|
|
|
* |
623
|
|
|
* @access public |
624
|
|
|
* @param array|string $fields Array or coma separated list of fields to fetch |
625
|
|
|
* |
626
|
|
|
* @return dbObject |
627
|
|
|
*/ |
628
|
|
|
protected function getOne($fields = null) { |
629
|
|
|
$this->processHasOneWith(); |
630
|
|
|
$results = $this->db->ArrayBuilder()->getOne($this->dbTable, $fields); |
|
|
|
|
631
|
|
|
if ($this->db->count == 0) |
632
|
|
|
return null; |
633
|
|
|
|
634
|
|
|
$this->processArrays($results); |
635
|
|
|
$this->data = $results; |
636
|
|
|
$this->processAllWith($results); |
637
|
|
|
if ($this->returnType == 'Json') |
638
|
|
|
return json_encode($results); |
639
|
|
|
if ($this->returnType == 'Array') |
640
|
|
|
return $results; |
|
|
|
|
641
|
|
|
|
642
|
|
|
$item = new static ($results); |
|
|
|
|
643
|
|
|
$item->isNew = false; |
644
|
|
|
|
645
|
|
|
return $item; |
|
|
|
|
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Function to set witch hasOne or hasMany objects should be loaded togeather with a main object |
650
|
|
|
* |
651
|
|
|
* @access public |
652
|
|
|
* @param string $objectName Object Name |
653
|
|
|
* |
654
|
|
|
* @return dbObject |
655
|
|
|
*/ |
656
|
|
|
private function with($objectName) { |
657
|
|
|
if (!property_exists($this, 'relations') && !isset ($this->relations[$name])) |
|
|
|
|
658
|
|
|
die ("No relation with name $objectName found"); |
|
|
|
|
659
|
|
|
|
660
|
|
|
$this->_with[$objectName] = $this->relations[$objectName]; |
|
|
|
|
661
|
|
|
|
662
|
|
|
return $this; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/* |
666
|
|
|
* Enable models autoload from a specified path |
667
|
|
|
* |
668
|
|
|
* Calling autoload() without path will set path to dbObjectPath/models/ directory |
669
|
|
|
* |
670
|
|
|
* @param string $path |
671
|
|
|
*/ |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Pagination wraper to get() |
675
|
|
|
* |
676
|
|
|
* @access public |
677
|
|
|
* @param int $page Page number |
678
|
|
|
* @param array|string $fields Array or coma separated list of fields to fetch |
679
|
|
|
* @return array |
680
|
|
|
*/ |
681
|
|
|
private function paginate($page, $fields = null) { |
682
|
|
|
$this->db->pageLimit = self::$pageLimit; |
683
|
|
|
$res = $this->db->paginate($this->dbTable, $page, $fields); |
|
|
|
|
684
|
|
|
self::$totalPages = $this->db->totalPages; |
685
|
|
|
if ($this->db->count == 0) return null; |
686
|
|
|
|
687
|
|
|
foreach ($res as &$r) { |
|
|
|
|
688
|
|
|
$this->processArrays($r); |
689
|
|
|
$this->data = $r; |
690
|
|
|
$this->processAllWith($r, false); |
691
|
|
|
if ($this->returnType == 'Object') { |
692
|
|
|
$item = new static ($r); |
|
|
|
|
693
|
|
|
$item->isNew = false; |
694
|
|
|
$objects[] = $item; |
|
|
|
|
695
|
|
|
} |
696
|
|
|
} |
697
|
|
|
$this->_with = Array(); |
|
|
|
|
698
|
|
|
if ($this->returnType == 'Object') |
699
|
|
|
return $objects; |
|
|
|
|
700
|
|
|
|
701
|
|
|
if ($this->returnType == 'Json') |
702
|
|
|
return json_encode($res); |
703
|
|
|
|
704
|
|
|
return $res; |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.