-
Notifications
You must be signed in to change notification settings - Fork 2
/
MY_Model.php
766 lines (658 loc) · 19.4 KB
/
MY_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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
<?php
require_once 'Pagination.php';
/**
* This class must be extended by model classes to inherit the functionalities
*/
class MY_Model extends Pagination {
/**
* Name of the database table
* @var string
*/
protected $table = '';
/**
* primary key of database table
* @var string
*/
protected $primaryKey = 'id';
/**
* Database table column names
* @var array
*/
protected $fillable = array();
/**
* Database table column names
* @var array
*/
protected $hidden = array();
/**
* Temporary array for returning
* @var array
*/
protected $data = array();
/**
* True, if relational data is being called
* @var boolean
*/
protected $getRelation = false;
/**
* For checking result type fetched by queries
* @var string
*/
protected $resultType = 'object';
/**
* For checking result fetched is single row or multiple
* @var string
*/
protected $rowType = 'multiple';
/**
* For created_at/updated_at timestamps
* @var string
*/
protected $timestamps = false;
/**
* This will set automatic table name based on name of model class
*/
function __construct(){
parent::__construct();
if($this->table == '') {
$this->table = $this->classToTable(get_called_class());
}
}
/**
* convert class name to assuming database table name
* @param string $string
* @return string
*/
private function classToTable($string) {
return strtolower(preg_replace(['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', $string));
}
/**
* For getting the name of table
* @return string
*/
public function getTable() {
return $this->table;
}
/**
* It will simply send the result of raw query passed
* @param string $query
* @param string $type
* @return array
*/
public function rawQuery($query, $type = 'object') {
if($type == 'array') {
return $this->db->query($query)->result_array();
}
return $this->db->query($query)->result();
}
/**
* This will return last query
* @return string
*/
public function lastQuery() {
return $this->db->last_query();
}
/**
* count of all rows present in table
* @return integer
*/
public function countAll() {
return $this->db->count_all($this->table);
}
/**
* count of rows with/without conditions
* @return integer
*/
public function count() {
return $this->db->from($this->table)->count_all_results();
}
/**
* get all rows present in table
* @return array
*/
public function getAll() {
$result = $this->db->select('*')->from($this->table)->get()->result();
return $this->filterResults($result);
}
/**
* Get rows based on array of conditions passed
* @param array $conditions
* @return aray or boolean
*/
public function getWhere($conditions) {
if(is_array($conditions)) {
$result = $this->db->get_where($this->table, $conditions)->result();
return $this->filterResults($result);
}
return false;
}
/**
* Get rows based on multiple Ids passed with column name
* @param array $IDsArray
* @param string $column
* @param string $type
* @return array
*/
public function getWhereIDs($IDsArray, $column = 'id', $type = 'object') {
if($IDsArray) {
if(is_array($IDsArray)) {
$query = $this->db->from($this->table)->where_in($column, $IDsArray)->get();
if($type == 'array') {
if($this->rowType == 'single')
return $this->filterResults($query->row_array());
else
return $this->filterResults($query->result_array());
} else {
if($this->rowType == 'single')
return $this->filterResults($query->row());
else
return $this->filterResults($query->result());
}
}
}
return $this->data;
}
/**
* Get single row based on id or condition is passed
* @param integer $id
* @return array
*/
public function find($id) {
if($id) {
if(is_array($id)) {
return $this->filterResults($this->db->get_where($this->table, $id)->row());
} else {
return $this->filterResults($this->db->get_where($this->table, array($this->primaryKey => $id))->row());
}
}
return $this->data;
}
/**
* return maximum of column values
* @param $string $column
* @return integer
*/
public function max($column) {
return $this->db->select_max($column)->get($this->table)->result()[0]->$column;
}
/**
* return minimum of column values
* @param $string $column
* @return integer
*/
public function min($column) {
return $this->db->select_min($column)->get($this->table)->result()[0]->$column;
}
/**
* return average of column values
* @param $string $column
* @return integer
*/
public function avg($column) {
return $this->db->select_avg($column)->get($this->table)->result()[0]->$column;
}
/**
* return sum of column values
* @param $string $column
* @return integer
*/
public function sum($column) {
return $this->db->select_sum($column)->get($this->table)->result()[0]->$column;
}
/**
* For inserting single row in table
* @param array $data [description]
* @return integer or boolean
*/
public function insert($data = array()) {
if(sizeof($data) > 0) {
$data = $this->filterFillable($data);
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
return false;
}
/**
* For inserting multiple rows in table
* @param array $data [description]
* @return boolean
*/
public function insertBatch($data = array()) {
if(sizeof($data) > 0) {
$data = $this->filterFillable($data, 'batch');
return $this->db->insert_batch($this->table, $data);
}
return false;
}
/**
* For updating a row in table by id or condition
* @param integer or array $id
* @param array $data
* @return boolean
*/
public function update($id, $data = array()) {
if($id && sizeof($data) > 0) {
$data = $this->filterFillable($data, false, 'update');
if(is_array($id)) {
return $this->db->where($id)->update($this->table, $data);
} else {
return $this->db->where($this->primaryKey, $id)->update($this->table, $data);
}
}
return false;
}
/**
* For deleting single row by id or conditions
* @param integer or array $id
* @return boolean
*/
public function delete($id) {
if($id) {
if(is_array($id)) {
return $this->db->where($id)->delete($this->table);
} else {
return $this->db->where($this->primaryKey, $id)->delete($this->table);
}
}
return false;
}
/**
* For deleting multiple rows by conditions
* @param array $conditions
* @return boolean
*/
public function deleteBatch($conditions) {
if($conditions) {
if(is_array($conditions)) {
$IDsArray = $this->getIDsArray($conditions);
$this->db->where_in($this->primaryKey, $IDsArray);
return $this->db->delete($this->table);
}
}
return false;
}
/**
* For deleting multiple rows by Ids
* @param array $conditions
* @return boolean
*/
public function deleteIDs($IDs) {
if($IDs) {
if(is_array($IDs)) {
$this->db->where_in($this->primaryKey, $IDs);
return $this->db->delete($this->table);
}
}
return false;
}
/**
* For deleting all rows from the table
* @return boolean
*/
public function truncate() {
return $this->db->truncate($this->table);
}
/**
* For getting all the Ids of array of result
* @param array $conditions
* @return array
*/
public function getIDsArray($conditions) {
$result = $this->db->select($this->primaryKey)->get_where($this->table, $conditions)->result_array();
$IDs = array();
if(sizeof($result)> 0) {
foreach ($result as $res) {
$IDs[] = $res[$this->primaryKey];
}
}
return $IDs;
}
/**
* Check if row existed with column value
* @param string $column
* @param any $value
* @param integer $id
* @return boolean
*/
public function checkUnique($column, $value, $id = 0) {
$row = array();
if($id == 0) {
$row = $this->db->get_where($this->table, array($column => $value))->num_rows();
} else {
$row = $this->db->get_where($this->table, array($this->primaryKey.' !=' => $id, $column => $value))->num_rows();
}
if($row > 0) {
return true;
}
return false;
}
/**
* For converting data set into fillable column names
* @param array $data
* @param boolean $batch
* @param string $type
* @return array
*/
private function filterFillable($data, $batch = false, $type = 'save') {
if(sizeof($this->fillable) > 0) {
if($batch) {
foreach($data as $key => $row) {
foreach($row as $col => $val) {
if(!in_array($col, $this->fillable)) {
unset($data[$key][$col]);
}
}
}
} else {
foreach($data as $col => $val) {
if(!in_array($col, $this->fillable)) {
unset($data[$col]);
}
}
}
}
return $this->checkTimestamps($data, $batch, $type);
}
/**
* For adding timestamps to row during insert/update
* @param array $data
* @param boolean $batch
* @param string $type
* @return array
*/
public function checkTimestamps($data, $batch, $type) {
if($this->timestamps) {
$timestamp = date('Y-m-d H:i:s');
if($batch) {
foreach($data as $key => $row) {
if($type == 'save') {
$data[$key]['created_at'] = $timestamp;
}
$data[$key]['updated_at'] = $timestamp;
}
} else {
if($type == 'save') {
$data['created_at'] = $timestamp;
}
$data['updated_at'] = $timestamp;
}
}
return $data;
}
/**
* For setting relations as true for fetching relational result
* @return object
*/
public function relate() {
$this->getRelation = true;
return $this;
}
/**
* For unsetting relations as true for fetching relational result
* @return object
*/
public function unrelate() {
$this->getRelation = false;
return $this;
}
/**
* For getting all the Ids of the result
* @param array $result
* @param string $id
* @param string $column
* @return array
*/
public function getResultIDsArray($result, $id = 'id', $column = '') {
$IDs = array();
if(sizeof($result)> 0) {
foreach($result as $res) {
if(is_object($res)) {
if(isset($res->$column)) {
return $IDs;
}
$this->resultType = 'object';
$IDs[] = $res->$id;
}
else if(is_array($res)) {
if(isset($res[$column])) {
return $IDs;
}
$this->resultType = 'array';
$IDs[] = $res[$id];
}
else {
$this->rowType = 'single';
if(isset($result->$id)) {
$IDs[] = $result->$id;
break;
}
else if(isset($result[$id])) {
$IDs[] = $result[$id];
break;
}
}
}
}
return $IDs;
}
/**
* For adding relation to database table
* @param array $relation
*/
protected function addRelation($relation = array()) {
if(sizeof($relation) > 0) {
$this->relations[] = $relation;
}
}
/**
* For adding multiple relations to database table
* @param array $relations
*/
protected function addRelations($relations = array()) {
if(sizeof($relations) > 0) {
foreach($relations as $relation) {
$this->relations[] = $relation;
}
}
}
/**
* This filtering will check all the settings did in model class
* @param array $result
* @return array
*/
protected function filterResults($result) {
if(!$this->getRelation && sizeof($this->hidden) == 0) {
return $result;
}
if(method_exists($this, 'setRelations') && is_callable(array($this, 'setRelations'))) {
$this->setRelations();
}
if(sizeof($result) > 0) {
if(sizeof($this->hidden) > 0) {
$result = $this->filterHide($result);
}
if(sizeof($this->relations) > 0) {
$result = $this->processRelations($result);
}
}
return $result;
}
/**
* Hide result data with column names against setting did in model class
* @param array $result
* @return array
*/
protected function filterHide($result) {
$type = '';
foreach($result as $res) {
if($type == '') {
if(is_object($res)) {
$type = 'object';
}
else if(is_array($res)) {
$type = 'array';
}
}
foreach($this->hidden as $hide) {
if($type == 'object') {
if(isset($res->$hide)) {
unset($res->$hide);
}
}
else if($type == 'array') {
if(isset($res[$hide])) {
unset($res[$hide]);
}
}
}
}
return $result;
}
/**
* Process and find relations to the result data
* @param array $result
* @return array
*/
protected function processRelations($result) {
if(sizeof($this->relations) > 0) {
foreach($this->relations as $relation) {
$primary = isset($relation['primary'])? $relation['primary'] : $this->primaryKey;
$foreign = isset($relation['foreign'])? $relation['foreign'] : $this->table.'_id';
$variable = isset($relation['variable'])? $relation['variable'] : '';
$single = isset($relation['single'])? $relation['single'] : false;
$column = isset($relation['column'])? $relation['column'] : '';
if($variable == '' && isset($relation['table'])) {
$variable = strtolower($relation['table']);
}
if(isset($relation['table'])) {
$result = $this->attachRelation($result, $primary, $foreign, $variable, $relation['table'], 'table', $column, $single);
}
else if(isset($relation['model'])) {
$result = $this->attachRelation($result, $primary, $foreign, $variable, $relation['model'], 'model', $column, $single);
}
}
}
return $result;
}
/**
* Function will add relational data to result data
* @param array $result
* @param string $primary
* @param string $foreign
* @param string $variable
* @param string $table
* @param string $type
* @param string $column
* @param boolean $single
* @return array
*/
protected function attachRelation($result, $primary, $foreign, $variable, $table, $type = 'table', $column = '', $single = false) {
$IDs = $this->getResultIDsArray($result, $primary, $variable);
if(sizeof($IDs) > 0) {
if($type == 'model') {
$this->load->model($table);
$rows = $this->$table->getWhereIDs($IDs, $foreign, $this->resultType);
} else {
$IDS = implode(',', $IDs);
$rows = $this->rawQuery("SELECT * FROM {$table} WHERE {$foreign} IN({$IDS})", $this->resultType);
}
foreach($result as $key => $res) {
if($this->resultType == 'object') {
if($this->rowType == 'single') {
$result->{$variable} = $this->extractRelatedRows($rows, $result->$primary, $foreign, $column, $single);
} else {
$result[$key]->{$variable} = $this->extractRelatedRows($rows, $res->$primary, $foreign, $column, $single);
}
}
else if($this->resultType == 'array') {
if($this->rowType == 'single') {
$result[$variable] = $this->extractRelatedRows($rows, $result[$primary], $foreign, $column, $single);
} else {
$result[$key][$variable] = $this->extractRelatedRows($rows, $res[$primary], $foreign, $column, $single);
}
}
}
}
return $result;
}
/**
* Find and get related rows from other tables
* @param array or object $rows
* @param integer $primaryID
* @param string $foreign
* @param string $column
* @param boolean $single
* @return array or object
*/
protected function extractRelatedRows($rows, $primaryID, $foreign, $column = '', $single = false) {
$result = new stdClass();
if($this->resultType == 'array') {
$result = array();
}
if($column != '') {
$result = '';
}
if(sizeof($rows) > 0) {
$i = 0;
foreach($rows as $row) {
if($single) {
return $row;
}
if($this->resultType == 'object') {
if($row->$foreign == $primaryID) {
if($column == '') {
$result->{$i} = $row;
} else {
return $this->getColumnValue($row, $column);
}
$i++;
}
}
else if($this->resultType == 'array') {
if($row[$foreign] == $primaryID) {
if($column == '') {
$result[$i] = $row;
} else {
return $this->getColumnValue($row, $column);
}
$i++;
}
}
}
return $result;
}
return NULL;
}
/**
* Get the column value based on type
* @param object $row
* @param string $column
* @return string $column
*/
protected function getColumnValue($row, $column) {
$value = '';
if($this->resultType == 'object') {
if(is_array($column)) {
if(isset($column['modify'])) {
$value = str_replace("_COL_", $row->$column['name'], $column['modify']);
} else {
$value = $row->$column['name'];
}
} else {
$value = $row->$column;
}
}
else if($this->resultType == 'array') {
if(is_array($column)) {
if(isset($column['modify'])) {
$value = str_replace("_COL_", $row[$column['name']], $column['modify']);
} else {
$value = $row[$column['name']];
}
} else {
$value = $row[$column];
}
}
return $value;
}
}