1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Models\Sql; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\Models\Sql\DataObjects\Result\Row; |
19
|
|
|
use O2System\Framework\Models\Sql\Traits\FinderTrait; |
20
|
|
|
use O2System\Framework\Models\Sql\Traits\ModifierTrait; |
21
|
|
|
use O2System\Framework\Models\Sql\Traits\RecordTrait; |
22
|
|
|
use O2System\Framework\Models\Sql\Traits\RelationTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Model |
26
|
|
|
* |
27
|
|
|
* @package O2System\Framework\Models\Sql |
28
|
|
|
*/ |
29
|
|
|
class Model |
30
|
|
|
{ |
31
|
|
|
use FinderTrait; |
|
|
|
|
32
|
|
|
use ModifierTrait; |
33
|
|
|
use RecordTrait; |
|
|
|
|
34
|
|
|
use RelationTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Model::$db |
38
|
|
|
* |
39
|
|
|
* Database connection instance. |
40
|
|
|
* |
41
|
|
|
* @var \O2System\Database\Sql\Abstracts\AbstractConnection |
42
|
|
|
*/ |
43
|
|
|
public $db; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Model::$qb |
47
|
|
|
* |
48
|
|
|
* Database query builder instance. |
49
|
|
|
* |
50
|
|
|
* @var \O2System\Database\Sql\Abstracts\AbstractQueryBuilder |
51
|
|
|
*/ |
52
|
|
|
public $qb; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Model::$table |
56
|
|
|
* |
57
|
|
|
* Database table name |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
public $table = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Model::$columns |
65
|
|
|
* |
66
|
|
|
* Database table columns. |
67
|
|
|
* |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
public $columns = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Model::$fillableColumns |
74
|
|
|
* |
75
|
|
|
* Database table fillable columns name. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
public $fillableColumns = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Model::$hideColumns |
83
|
|
|
* |
84
|
|
|
* Database table hide columns name. |
85
|
|
|
* |
86
|
|
|
* @var array |
87
|
|
|
*/ |
88
|
|
|
public $hideColumns = []; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Model::$visibleColumns |
92
|
|
|
* |
93
|
|
|
* Database table visible columns name. |
94
|
|
|
* |
95
|
|
|
* @var array |
96
|
|
|
*/ |
97
|
|
|
public $visibleColumns = []; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Model::$appendColumns |
101
|
|
|
* |
102
|
|
|
* Database table append columns name. |
103
|
|
|
* |
104
|
|
|
* @var array |
105
|
|
|
*/ |
106
|
|
|
public $appendColumns = []; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Model::$primaryKey |
110
|
|
|
* |
111
|
|
|
* Database table primary key field name. |
112
|
|
|
* |
113
|
|
|
* @var string |
114
|
|
|
*/ |
115
|
|
|
public $primaryKey = 'id'; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Model::$primaryKeys |
119
|
|
|
* |
120
|
|
|
* Database table primary key columns name. |
121
|
|
|
* |
122
|
|
|
* @var array |
123
|
|
|
*/ |
124
|
|
|
public $primaryKeys = []; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Model::$uploadedImageFilePath |
128
|
|
|
* |
129
|
|
|
* Storage uploaded image filePath. |
130
|
|
|
*/ |
131
|
|
|
public $uploadedImageFilePath = null; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Model::$uploadedImageKey |
135
|
|
|
* |
136
|
|
|
* Database table uploaded image key field name. |
137
|
|
|
* |
138
|
|
|
* @var string |
139
|
|
|
*/ |
140
|
|
|
public $uploadedImageKey = null; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Model::$uploadedImageKeys |
144
|
|
|
* |
145
|
|
|
* Database table uploaded image key columns name. |
146
|
|
|
* |
147
|
|
|
* @var array |
148
|
|
|
*/ |
149
|
|
|
public $uploadedImageKeys = []; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Model::$uploadedFileFilepath |
153
|
|
|
* |
154
|
|
|
* Storage uploaded file filePath. |
155
|
|
|
*/ |
156
|
|
|
public $uploadedFileFilepath = null; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Model::$uploadedFileKey |
160
|
|
|
* |
161
|
|
|
* Database table uploaded file key field name. |
162
|
|
|
* |
163
|
|
|
* @var string |
164
|
|
|
*/ |
165
|
|
|
public $uploadedFileKey = null; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Model::$uploadedFileKeys |
169
|
|
|
* |
170
|
|
|
* Database table uploaded file key columns name. |
171
|
|
|
* |
172
|
|
|
* @var array |
173
|
|
|
*/ |
174
|
|
|
public $uploadedFileKeys = []; |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Model::$result |
178
|
|
|
* |
179
|
|
|
* Model Result |
180
|
|
|
* |
181
|
|
|
* @var \O2System\Framework\Models\Sql\DataObjects\Result |
182
|
|
|
*/ |
183
|
|
|
public $result; |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Model::$row |
187
|
|
|
* |
188
|
|
|
* Model Result Row |
189
|
|
|
* |
190
|
|
|
* @var \O2System\Framework\Models\Sql\DataObjects\Result\Row |
191
|
|
|
*/ |
192
|
|
|
public $row; |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Result::$rebuildRowCallback |
196
|
|
|
* |
197
|
|
|
* @var \Closure |
198
|
|
|
*/ |
199
|
|
|
protected $rebuildRowCallback; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Model::$validSubModels |
203
|
|
|
* |
204
|
|
|
* List of library valid sub models |
205
|
|
|
* |
206
|
|
|
* @var array |
207
|
|
|
*/ |
208
|
|
|
protected $validSubModels = []; |
209
|
|
|
|
210
|
|
|
// ------------------------------------------------------------------------ |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Model::__construct |
214
|
|
|
* |
215
|
|
|
* @throws \ReflectionException |
216
|
|
|
*/ |
217
|
|
|
public function __construct() |
218
|
|
|
{ |
219
|
|
|
// Set database connection |
220
|
|
|
if (method_exists(database(), 'loadConnection')) { |
221
|
|
|
if ($this->db = database()->loadConnection('default')) { |
222
|
|
|
$this->qb = $this->db->getQueryBuilder(); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// Set database table |
227
|
|
|
if (empty($this->table)) { |
228
|
|
|
$modelClassName = get_called_class(); |
229
|
|
|
$modelClassName = get_class_name($modelClassName); |
230
|
|
|
$this->table = underscore($modelClassName); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// Fetch sub-models |
234
|
|
|
$this->fetchSubModels(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// ------------------------------------------------------------------------ |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Model::fetchSubModels |
241
|
|
|
* |
242
|
|
|
* @access protected |
243
|
|
|
* @final this method cannot be overwritten. |
244
|
|
|
* |
245
|
|
|
* @return void |
246
|
|
|
* @throws \ReflectionException |
247
|
|
|
*/ |
248
|
|
|
final protected function fetchSubModels() |
249
|
|
|
{ |
250
|
|
|
$reflection = new \ReflectionClass(get_called_class()); |
251
|
|
|
|
252
|
|
|
// Define called model class filepath |
253
|
|
|
$filePath = $reflection->getFileName(); |
254
|
|
|
|
255
|
|
|
// Define filename for used as subdirectory name |
256
|
|
|
$filename = pathinfo($filePath, PATHINFO_FILENAME); |
257
|
|
|
|
258
|
|
|
// Get model class directory name |
259
|
|
|
$dirName = dirname($filePath) . DIRECTORY_SEPARATOR; |
260
|
|
|
|
261
|
|
|
// Get sub models or siblings models |
262
|
|
|
if ($filename === 'Model' || $filename === modules()->top()->getDirName()) { |
|
|
|
|
263
|
|
|
$subModelsDirName = dirname($dirName) . DIRECTORY_SEPARATOR . 'Models' . DIRECTORY_SEPARATOR; |
264
|
|
|
|
265
|
|
|
if (is_dir($subModelsDirName)) { |
266
|
|
|
$subModelPath = $subModelsDirName; |
267
|
|
|
} |
268
|
|
|
} elseif (is_dir($subModelsDirName = $dirName . $filename . DIRECTORY_SEPARATOR)) { |
269
|
|
|
$subModelPath = $subModelsDirName; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if (isset($subModelPath)) { |
273
|
|
|
loader()->addNamespace($reflection->name, $subModelPath); |
274
|
|
|
|
275
|
|
|
foreach (glob($subModelPath . '*.php') as $filepath) { |
276
|
|
|
if ($filepath === $filePath) { |
277
|
|
|
continue; |
278
|
|
|
} |
279
|
|
|
$this->validSubModels[ camelcase(pathinfo($filepath, PATHINFO_FILENAME)) ] = $filepath; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
// ------------------------------------------------------------------------ |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Model::__callStatic |
288
|
|
|
* |
289
|
|
|
* @param string $method |
290
|
|
|
* @param array $arguments |
291
|
|
|
* |
292
|
|
|
* @return bool|mixed |
293
|
|
|
*/ |
294
|
|
|
final public static function __callStatic($method, array $arguments = []) |
295
|
|
|
{ |
296
|
|
|
$modelClassName = get_called_class(); |
297
|
|
|
|
298
|
|
|
if ( ! models()->has($modelClassName)) { |
|
|
|
|
299
|
|
|
models()->add(new $modelClassName(), $modelClassName); |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if (false !== ($modelInstance = models($modelClassName))) { |
|
|
|
|
303
|
|
|
if (method_exists($modelInstance, $method)) { |
304
|
|
|
return call_user_func_array([&$modelInstance, $method], $arguments); |
305
|
|
|
} elseif (method_exists($modelInstance->db, $method)) { |
|
|
|
|
306
|
|
|
return call_user_func_array([&$modelInstance->db, $method], $arguments); |
307
|
|
|
} elseif (method_exists($modelInstance->qb, $method)) { |
|
|
|
|
308
|
|
|
return call_user_func_array([&$modelInstance->qb, $method], $arguments); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return false; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
// ------------------------------------------------------------------------ |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Model::__call |
319
|
|
|
* |
320
|
|
|
* @param string $method |
321
|
|
|
* @param array $arguments |
322
|
|
|
* |
323
|
|
|
* @return bool|mixed |
324
|
|
|
*/ |
325
|
|
|
final public function __call($method, array $arguments = []) |
326
|
|
|
{ |
327
|
|
|
return static::__callStatic($method, $arguments); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// ------------------------------------------------------------------------ |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Model::__get |
334
|
|
|
* |
335
|
|
|
* @param string $property |
336
|
|
|
* |
337
|
|
|
* @return bool|mixed|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row |
338
|
|
|
*/ |
339
|
|
|
public function __get($property) |
340
|
|
|
{ |
341
|
|
|
if ($this->row instanceof Row) { |
|
|
|
|
342
|
|
|
if ($this->row->offsetExists($property)) { |
343
|
|
|
return $this->row->offsetGet($property); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
if (empty($get[ $property ])) { |
|
|
|
|
348
|
|
|
if (services()->has($property)) { |
349
|
|
|
return services()->get($property); |
350
|
|
|
} elseif ($this->hasSubModel($property)) { |
351
|
|
|
return $this->loadSubModel($property); |
352
|
|
|
} elseif (o2system()->__isset($property)) { |
353
|
|
|
return o2system()->__get($property); |
354
|
|
|
} elseif (models()->__isset($property)) { |
|
|
|
|
355
|
|
|
return models()->get($property); |
|
|
|
|
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return false; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
// ------------------------------------------------------------------------ |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Model::hasSubModel |
366
|
|
|
* |
367
|
|
|
* @param string $model |
368
|
|
|
* |
369
|
|
|
* @return bool |
370
|
|
|
*/ |
371
|
|
|
final protected function hasSubModel($model) |
372
|
|
|
{ |
373
|
|
|
if (array_key_exists($model, $this->validSubModels)) { |
374
|
|
|
return (bool)is_file($this->validSubModels[ $model ]); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
return false; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// ------------------------------------------------------------------------ |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Model::loadSubModel |
384
|
|
|
* |
385
|
|
|
* @param string $model |
386
|
|
|
* |
387
|
|
|
* @return bool|mixed|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row |
388
|
|
|
*/ |
389
|
|
|
final protected function loadSubModel($model) |
390
|
|
|
{ |
391
|
|
|
if ($this->hasSubModel($model)) { |
392
|
|
|
$classNames = [ |
393
|
|
|
'\\' . get_called_class() . '\\' . ucfirst($model), |
394
|
|
|
'\\' . get_namespace(get_called_class()) . ucfirst($model), |
395
|
|
|
]; |
396
|
|
|
|
397
|
|
|
foreach ($classNames as $className) { |
398
|
|
|
if (class_exists($className)) { |
399
|
|
|
$this->{$model} = models($className); |
400
|
|
|
break; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
if (property_exists($this, $model)) { |
406
|
|
|
return $this->{$model}; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return false; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
// ------------------------------------------------------------------------ |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Model::getSubModel |
416
|
|
|
* |
417
|
|
|
* @param string $model |
418
|
|
|
* |
419
|
|
|
* @return bool|mixed|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row |
420
|
|
|
*/ |
421
|
|
|
final protected function getSubModel($model) |
422
|
|
|
{ |
423
|
|
|
return $this->loadSubModel($model); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
// ------------------------------------------------------------------------ |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Model::setRebuildRowCallback |
430
|
|
|
* |
431
|
|
|
* @param \Closure $callback |
432
|
|
|
*/ |
433
|
|
|
final public function rebuildRowCallback(\Closure $callback) |
434
|
|
|
{ |
435
|
|
|
if(empty($this->rebuildRowCallback)) { |
436
|
|
|
$this->rebuildRowCallback = $callback; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
// ------------------------------------------------------------------------ |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Model::isCallableRebuildRowCallback |
444
|
|
|
* |
445
|
|
|
* @return void |
446
|
|
|
*/ |
447
|
|
|
final public function resetRebuildRowCallback() |
448
|
|
|
{ |
449
|
|
|
$this->rebuildRowCallback = null; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
// ------------------------------------------------------------------------ |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Model::isCallableRebuildRowCallback |
456
|
|
|
* |
457
|
|
|
* @return bool |
458
|
|
|
*/ |
459
|
|
|
final public function isCallableRebuildRowCallback() |
460
|
|
|
{ |
461
|
|
|
return is_callable($this->rebuildRowCallback); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// ------------------------------------------------------------------------ |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Model::rebuildRow |
468
|
|
|
* |
469
|
|
|
* @param Row $row |
470
|
|
|
* |
471
|
|
|
* @return void |
472
|
|
|
*/ |
473
|
|
|
public function rebuildRow($row) |
474
|
|
|
{ |
475
|
|
|
if(is_callable($this->rebuildRowCallback)) { |
476
|
|
|
call_user_func($this->rebuildRowCallback, $row); |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
} |