forked from llebkered/ci-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MY_Model.php
executable file
·2087 lines (1932 loc) · 74.7 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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/*
* Copyright (C) 2014 @avenirer [avenir.ro@gmail.com]
* Everyone is permitted to copy and distribute verbatim or modified copies of this license document,
* and changing it is allowed as long as the name is changed.
* DON'T BE A DICK PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
***** Do whatever you like with the original work, just don't be a dick.
***** Being a dick includes - but is not limited to - the following instances:
********* 1a. Outright copyright infringement - Don't just copy this and change the name.
********* 1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
********* 1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.
***** If you become rich through modifications, related works/services, or supporting the original work, share the love. Only a dick would make loads off this work and not buy the original works creator(s) a pint.
***** Code is provided with no warranty.
*********** Using somebody else's code and bitching when it goes wrong makes you a DONKEY dick.
*********** Fix the problem yourself. A non-dick would submit the fix back.
*
*/
/** how to extend MY_Model:
* class User_model extends MY_Model
* {
* public $table = 'users'; // Set the name of the table for this model.
* public $primary_key = 'id'; // Set the primary key
* public $fillable = array(); // You can set an array with the fields that can be filled by insert/update
* public $protected = array(); // ...Or you can set an array with the fields that cannot be filled by insert/update
* public function __construct()
* {
* $this->_database_connection = group_name or array() | OPTIONAL
* Sets the connection preferences (group name) set up in the database.php. If not trset, it will use the
* 'default' (the $active_group) database connection.
* $this->timestamps = TRUE | array('made_at','modified_at','removed_at')
* If set to TRUE tells MY_Model that the table has 'created_at','updated_at' (and 'deleted_at' if $this->soft_delete is set to TRUE)
* If given an array as parameter, it tells MY_Model, that the first element is a created_at field type, the second element is a updated_at field type (and the third element is a deleted_at field type)
* $this->soft_deletes = FALSE;
* Enables (TRUE) or disables (FALSE) the "soft delete" on records. Default is FALSE
* $this->timestamps_format = 'Y-m-d H:i:s'
* You can at any time change the way the timestamp is created (the default is the MySQL standard datetime format) by modifying this variable. You can choose between whatever format is acceptable by the php function date() (default is 'Y-m-d H:i:s'), or 'timestamp' (UNIX timestamp)
* $this->return_as = 'object' | 'array'
* Allows the model to return the results as object or as array
* $this->has_one['phone'] = 'Phone_model' or $this->has_one['phone'] = array('Phone_model','foreign_key','local_key');
* $this->has_one['address'] = 'Address_model' or $this->has_one['address'] = array('Address_model','foreign_key','another_local_key');
* Allows establishing ONE TO ONE or more ONE TO ONE relationship(s) between models/tables
* $this->has_many['posts'] = 'Post_model' or $this->has_many['posts'] = array('Posts_model','foreign_key','another_local_key');
* Allows establishing ONE TO MANY or more ONE TO MANY relationship(s) between models/tables
* $this->has_many_pivot['posts'] = 'Post_model' or $this->has_many_pivot['posts'] = array('Posts_model','foreign_primary_key','local_primary_key');
* Allows establishing MANY TO MANY or more MANY TO MANY relationship(s) between models/tables with the use of a PIVOT TABLE
* !ATTENTION: The pivot table name must be composed of the two table names separated by "_" the table names having to to be alphabetically ordered (NOT users_posts, but posts_users).
* Also the pivot table must contain as identifying columns the columns named by convention as follows: table_name_singular + _ + foreign_table_primary_key.
* For example: considering that a post can have multiple authors, a pivot table that connects two tables (users and posts) must be named posts_users and must have post_id and user_id as identifying columns for the posts.id and users.id tables.
* $this->cache_driver = 'file'
* $this->cache_prefix = 'mm'
* If you know you will do some caching of results without the native caching solution, you can at any time use the MY_Model's caching.
* By default, MY_Model uses the files to cache result.
* If you want to change the way it stores the cache, you can change the $cache_driver property to whatever CodeIgniter cache driver you want to use.
* Also, with $cache_prefix, you can prefix the name of the caches. by default any cache made by MY_Model starts with 'mm' + _ + "name chosen for cache"
* $this->delete_cache_on_save = FALSE
* If you use caching often and you don't want to be forced to delete cache manually, you can enable $this->delete_cache_on_save by setting it to TRUE. If set to TRUE the model will auto-delete all cache related to the model's table whenever you write/update/delete data from that table.
* $this->pagination_delimiters = array('<span>','</span>');
* If you know you will use the paginate() method, you can change the delimiters between the pages links
* $this->pagination_arrows = array('<','>');
* You can also change the way the previous and next arrows look like.
*
*
* parent::__construct();
* }
* }
*
**/
class MY_Model extends CI_Model
{
/**
* Select the database connection from the group names defined inside the database.php configuration file or an
* array.
*/
protected $_database_connection = NULL;
/** @var
* This one will hold the database connection object
*/
protected $_database;
/** @var null
* Sets table name
*/
public $table = NULL;
/**
* @var null
* Sets PRIMARY KEY
*/
public $primary_key = 'id';
/**
* @var null|array
* Sets fillable fields.
* If value is set as null, the $fillable property will be set as an array with all the table fields (except the primary key) as elements.
* If value is set as an array, there won't be any changes done to it (ie: no field of the table will be updated or inserted).
*/
public $fillable = null;
/**
* @var null|array
* Sets protected fields.
* If value is set as null, the $protected will be set as an array with the primary key as single element.
* If value is set as an array, there won't be any changes done to it (if set as empty array, the primary key won't be inserted here).
*/
public $protected = null;
/** @var bool | array
* Enables created_at and updated_at fields
*/
protected $timestamps = TRUE;
protected $timestamps_format = 'Y-m-d H:i:s';
protected $_created_at_field;
protected $_updated_at_field;
protected $_deleted_at_field;
/** @var bool
* Enables soft_deletes
*/
protected $soft_deletes = FALSE;
/** relationships variables */
private $_relationships = array();
public $has_one = array();
public $has_many = array();
public $has_many_pivot = array();
public $separate_subqueries = TRUE;
private $_requested = array();
/** end relationships variables */
/*caching*/
public $cache_driver = 'file';
public $cache_prefix = 'mm';
protected $_cache = array();
public $delete_cache_on_save = FALSE;
/*pagination*/
public $next_page;
public $previous_page;
public $all_pages;
public $pagination_delimiters;
public $pagination_arrows;
/* validation */
private $validated = TRUE;
private $row_fields_to_update = array();
/**
* The various callbacks available to the model. Each are
* simple lists of method names (methods will be run on $this).
*/
protected $before_create = array();
protected $after_create = array();
protected $before_update = array();
protected $after_update = array();
protected $before_get = array();
protected $after_get = array();
protected $before_delete = array();
protected $after_delete = array();
protected $before_soft_delete = array();
protected $after_soft_delete = array();
protected $callback_parameters = array();
protected $return_as = 'object';
protected $return_as_dropdown = NULL;
protected $_dropdown_field = '';
private $_trashed = 'without';
private $_select = '*';
public function __construct()
{
parent::__construct();
$this->_set_connection();
$this->_set_timestamps();
$this->_fetch_table();
$this->pagination_delimiters = (isset($this->pagination_delimiters)) ? $this->pagination_delimiters : array('<span>','</span>');
$this->pagination_arrows = (isset($this->pagination_arrows)) ? $this->pagination_arrows : array('<','>');
/* These below are implementation examples for before_create and before_update triggers.
Their respective functions - add_creator() and add_updater() - can be found at the end of the model.
They add user id on create and update. If you comment this out don't forget to do the same for the methods()
$this->before_create[]='add_creator';
$this->before_update[]='add_updater';
*/
}
/*
* public function _get_rules($action=NULL)
* This function returns the rules. If action is given and rules are
* stored in an associative array, only the rules for this action are
* returned, all otherwise.
* This should be used by any method utilizing the rules.
*/
public function _get_rules($action=NULL) {
if (isset($action) && $this->is_assoc($this->rules)) {
return $this->rules[$action];
}
return $this->rules;
}
public function _prep_before_write($data)
{
// Let's make sure we receive an array...
$data_as_array = (is_object($data)) ? (array)$data : $data;
$new_data = array();
$multi = $this->is_multidimensional($data);
if($multi===FALSE)
{
foreach ($data_as_array as $field => $value)
{
if (in_array($field, $this->fillable)) {
$new_data[$field] = $value;
}
else
{
show_error('MY_Model: Unknown column ('.$field.') in table: ('.$this->table.').');
}
}
}
else
{
foreach($data_as_array as $key => $row)
{
foreach ($row as $field => $value)
{
if (in_array($field, $this->fillable)) {
$new_data[$key][$field] = $value;
}
else
{
show_error('MY_Model: Unknown column '.$field.' in table: '.$this->table);
}
}
}
}
return $new_data;
}
/*
* public function _prep_after_write()
* this function simply deletes the cache related to the model's table if $this->delete_cache_on_save is set to TRUE
* It should be called by any "save" method
*/
public function _prep_after_write()
{
if($this->delete_cache_on_save===TRUE)
{
$this->delete_cache('*');
}
return TRUE;
}
public function _prep_before_read()
{
}
public function _prep_after_read($data, $multi = TRUE)
{
// let's join the subqueries...
$data = $this->join_temporary_results($data);
$this->_database->reset_query();
$this->_requested = array();
if(isset($this->return_as_dropdown) && $this->return_as_dropdown == 'dropdown')
{
foreach($data as $row)
{
$dropdown[$row[$this->primary_key]] = $row[$this->_dropdown_field];
}
$data = $dropdown;
$this->return_as_dropdown = NULL;
}
elseif($this->return_as == 'object')
{
$data = $this->array_to_object($data);
}
if(isset($this->_select))
{
$this->_select = '*';
}
return $data;
}
/**
* public function from_form($rules = NULL,$additional_values = array(), $row_fields_to_update = array())
* Gets data from form, after validating it and waits for an insert() or update() method in the query chain
* @param null $rules Gets the validation rules. If nothing is passed (NULL), will look for the validation rules
* inside the model $rules public property
* @param array $additional_values Accepts additional fields to be filled, fields that are not to be found inside
* the form. The values are inserted as an array with "field_name" => "field_value"
* @param array $row_fields_to_update You can mention the fields from the form that can be used to identify
* the row when doing an update
* @return $this
*/
public function from_form($rules = NULL,$additional_values = NULL, $row_fields_to_update = array())
{
$this->load->library('form_validation');
if(!isset($rules))
{
if(empty($row_fields_to_update))
{
$rules = $this->_get_rules('insert');
}
else
{
$rules = $this->_get_rules('update');
}
}
$this->form_validation->set_rules($rules);
if($this->form_validation->run())
{
$this->validated = array();
foreach($rules as $rule)
{
if(in_array($rule['field'],$this->fillable))
{
$this->validated[$rule['field']] = $this->input->post($rule['field']);
}
}
if(isset($additional_values) && is_array($additional_values) && !empty($additional_values))
{
foreach($additional_values as $field => $value)
{
if(in_array($field, $this->fillable))
{
$this->validated[$field] = $value;
}
}
}
if(!empty($row_fields_to_update))
{
foreach ($row_fields_to_update as $key => $field) {
if (in_array($field, $this->fillable)) {
$this->row_fields_to_update[$field] = $this->input->post($field);
}
else if (in_array($key, $this->fillable)){
$this->row_fields_to_update[$key] = $field;
}
else {
continue;
}
}
}
return $this;
}
else
{
$this->validated = FALSE;
return $this;
}
}
/**
* public function insert($data)
* Inserts data into table. Can receive an array or a multidimensional array depending on what kind of insert we're talking about.
* @param $data
* @return int/array Returns id/ids of inserted rows
*/
public function insert($data = NULL)
{
if(!isset($data) && $this->validated!=FALSE)
{
$data = $this->validated;
$this->validated = FALSE;
}
elseif(!isset($data))
{
return FALSE;
}
$data = $this->_prep_before_write($data);
//now let's see if the array is a multidimensional one (multiple rows insert)
$multi = $this->is_multidimensional($data);
// if the array is not a multidimensional one...
if($multi === FALSE)
{
if($this->timestamps !== FALSE)
{
$data[$this->_created_at_field] = $this->_the_timestamp();
}
$data = $this->trigger('before_create',$data);
if($this->_database->insert($this->table, $data))
{
$this->_prep_after_write();
$id = $this->_database->insert_id();
$return = $this->trigger('after_create',$id);
return $return;
}
return FALSE;
}
// else...
else
{
$return = array();
foreach($data as $row)
{
if($this->timestamps !== FALSE)
{
$row[$this->_created_at_field] = $this->_the_timestamp();
}
$row = $this->trigger('before_create',$row);
if($this->_database->insert($this->table,$row))
{
$return[] = $this->_database->insert_id();
}
}
$this->_prep_after_write();
$after_create = array();
foreach($return as $id)
{
$after_create[] = $this->trigger('after_create', $id);
}
return $after_create;
}
return FALSE;
}
/*
* public function is_multidimensional($array)
* Verifies if an array is multidimensional or not;
* @param array $array
* @return bool return TRUE if the array is a multidimensional one
*/
public function is_multidimensional($array)
{
if(is_array($array))
{
foreach($array as $element)
{
if(is_array($element))
{
return TRUE;
}
}
}
return FALSE;
}
/**
* public function update($data)
* Updates data into table. Can receive an array or a multidimensional array depending on what kind of update we're talking about.
* @param array $data
* @param array|int $column_name_where
* @param bool $escape should the values be escaped or not - defaults to true
* @return str/array Returns id/ids of inserted rows
*/
public function update($data = NULL, $column_name_where = NULL, $escape = TRUE)
{
if(!isset($data) && $this->validated!=FALSE)
{
$data = $this->validated;
$this->validated = FALSE;
}
elseif(!isset($data))
{
$this->_database->reset_query();
return FALSE;
}
// Prepare the data...
$data = $this->_prep_before_write($data);
//now let's see if the array is a multidimensional one (multiple rows insert)
$multi = $this->is_multidimensional($data);
// if the array is not a multidimensional one...
if($multi === FALSE)
{
if($this->timestamps !== FALSE)
{
$data[$this->_updated_at_field] = $this->_the_timestamp();
}
$data = $this->trigger('before_update',$data);
if($this->validated === FALSE && count($this->row_fields_to_update))
{
$this->where($this->row_fields_to_update);
$this->row_fields_to_update = array();
}
if(isset($column_name_where))
{
if (is_array($column_name_where))
{
$this->where($column_name_where);
} elseif (is_numeric($column_name_where)) {
$this->_database->where($this->primary_key, $column_name_where);
} else {
$column_value = (is_object($data)) ? $data->{$column_name_where} : $data[$column_name_where];
$this->_database->where($column_name_where, $column_value);
}
}
if($escape)
{
if($this->_database->update($this->table, $data))
{
$this->_prep_after_write();
$affected = $this->_database->affected_rows();
$return = $this->trigger('after_update',$affected);
return $return;
}
}
else
{
if($this->_database->set($data, null, FALSE)->update($this->table))
{
$this->_prep_after_write();
$affected = $this->_database->affected_rows();
$return = $this->trigger('after_update',$affected);
return $return;
}
}
return FALSE;
}
// else...
else
{
$rows = 0;
foreach($data as $row)
{
if($this->timestamps !== FALSE)
{
$row[$this->_updated_at_field] = $this->_the_timestamp();
}
$row = $this->trigger('before_update',$row);
if(is_array($column_name_where))
{
$this->_database->where($column_name_where[0], $column_name_where[1]);
}
else
{
$column_value = (is_object($row)) ? $row->{$column_name_where} : $row[$column_name_where];
$this->_database->where($column_name_where, $column_value);
}
if($escape)
{
if($this->_database->update($this->table,$row))
{
$rows++;
}
}
else
{
if($this->_database->set($row, null, FALSE)->update($this->table))
{
$rows++;
}
}
}
$affected = $rows;
$this->_prep_after_write();
$return = $this->trigger('after_update',$affected);
return $return;
}
return FALSE;
}
/**
* public function where($field_or_array = NULL, $operator_or_value = NULL, $value = NULL, $with_or = FALSE, $with_not = FALSE, $custom_string = FALSE)
* Sets a where method for the $this object
* @param null $field_or_array - can receive a field name or an array with more wheres...
* @param null $operator_or_value - can receive a database operator or, if it has a field, the value to equal with
* @param null $value - a value if it received a field name and an operator
* @param bool $with_or - if set to true will create a or_where query type pr a or_like query type, depending on the operator
* @param bool $with_not - if set to true will also add "NOT" in the where
* @param bool $custom_string - if set to true, will simply assume that $field_or_array is actually a string and pass it to the where query
* @return $this
*/
public function where($field_or_array = NULL, $operator_or_value = NULL, $value = NULL, $with_or = FALSE, $with_not = FALSE, $custom_string = FALSE)
{
if(is_array($field_or_array))
{
$multi = $this->is_multidimensional($field_or_array);
if($multi === TRUE)
{
foreach ($field_or_array as $where)
{
$field = $where[0];
$operator_or_value = isset($where[1]) ? $where[1] : NULL;
$value = isset($where[2]) ? $where[2] : NULL;
$with_or = (isset($where[3])) ? TRUE : FALSE;
$with_not = (isset($where[4])) ? TRUE : FALSE;
$this->where($field, $operator_or_value, $value, $with_or,$with_not);
}
return $this;
}
}
if($with_or === TRUE)
{
$where_or = 'or_where';
}
else
{
$where_or = 'where';
}
if($with_not === TRUE)
{
$not = '_not';
}
else
{
$not = '';
}
if($custom_string === TRUE)
{
$this->_database->{$where_or}($field_or_array, NULL, FALSE);
}
elseif(is_numeric($field_or_array))
{
$this->_database->{$where_or}(array($this->table.'.'.$this->primary_key => $field_or_array));
}
elseif(is_array($field_or_array) && !isset($operator_or_value))
{
$this->_database->where($field_or_array);
}
elseif(!isset($value) && isset($field_or_array) && isset($operator_or_value) && !is_array($operator_or_value))
{
$this->_database->{$where_or}(array($this->table.'.'.$field_or_array => $operator_or_value));
}
elseif(!isset($value) && isset($field_or_array) && isset($operator_or_value) && is_array($operator_or_value) && !is_array($field_or_array))
{
//echo $field_or_array;
//exit;
$this->_database->{$where_or.$not.'_in'}($this->table.'.'.$field_or_array, $operator_or_value);
}
elseif(isset($field_or_array) && isset($operator_or_value) && isset($value))
{
if(strtolower($operator_or_value) == 'like') {
if($with_not === TRUE)
{
$like = 'not_like';
}
else
{
$like = 'like';
}
if ($with_or === TRUE)
{
$like = 'or_'.$like;
}
$this->_database->{$like}($field_or_array, $value);
}
else
{
$this->_database->{$where_or}($field_or_array.' '.$operator_or_value, $value);
}
}
return $this;
}
/**
* public function limit($limit, $offset = 0)
* Sets a rows limit to the query
* @param $limit
* @param int $offset
* @return $this
*/
public function limit($limit, $offset = 0)
{
$this->_database->limit($limit, $offset);
return $this;
}
/**
* public function group_by($grouping_by)
* A wrapper to $this->_database->group_by()
* @param $grouping_by
* @return $this
*/
public function group_by($grouping_by)
{
$this->_database->group_by($grouping_by);
return $this;
}
/**
* public function delete($where)
* Deletes data from table.
* @param $where primary_key(s) Can receive the primary key value or a list of primary keys as array()
* @return Returns affected rows or false on failure
*/
public function delete($where = NULL)
{
if(!empty($this->before_delete) || !empty($this->before_soft_delete) || !empty($this->after_delete) || !empty($this->after_soft_delete) || ($this->soft_deletes === TRUE))
{
$to_update = array();
if(isset($where))
{
$this->where($where);
}
$query = $this->_database->get($this->table);
foreach($query->result() as $row)
{
$to_update[] = array($this->primary_key => $row->{$this->primary_key});
}
if(!empty($this->before_soft_delete))
{
foreach($to_update as &$row)
{
$row = $this->trigger('before_soft_delete',$row);
}
}
if(!empty($this->before_delete))
{
foreach($to_update as &$row)
{
$row = $this->trigger('before_delete',$row);
}
}
}
if(isset($where))
{
$this->where($where);
}
$affected_rows = 0;
if($this->soft_deletes === TRUE)
{
if(isset($to_update)&& count($to_update) > 0)
{
foreach($to_update as &$row)
{
//$row = $this->trigger('before_soft_delete',$row);
$row[$this->_deleted_at_field] = $this->_the_timestamp();
}
$affected_rows = $this->_database->update_batch($this->table, $to_update, $this->primary_key);
$to_update['affected_rows'] = $affected_rows;
$this->_prep_after_write();
$this->trigger('after_soft_delete',$to_update);
}
return $affected_rows;
}
else
{
if($this->_database->delete($this->table))
{
$affected_rows = $this->_database->affected_rows();
if(!empty($this->after_delete))
{
$to_update['affected_rows'] = $affected_rows;
$to_update = $this->trigger('after_delete',$to_update);
$affected_rows = $to_update;
}
$this->_prep_after_write();
return $affected_rows;
}
}
return FALSE;
}
/**
* public function force_delete($where = NULL)
* Forces the delete of a row if soft_deletes is enabled
* @param null $where
* @return bool
*/
public function force_delete($where = NULL)
{
if(isset($where))
{
$this->where($where);
}
if($this->_database->delete($this->table))
{
$this->_prep_after_write();
return $this->_database->affected_rows();
}
return FALSE;
}
/**
* public function restore($where = NULL)
* "Un-deletes" a row
* @param null $where
* @return bool
*/
public function restore($where = NULL)
{
$this->with_trashed();
if(isset($where))
{
$this->where($where);
}
if($affected_rows = $this->_database->update($this->table,array($this->_deleted_at_field=>NULL)))
{
$this->_prep_after_write();
return $affected_rows;
}
return FALSE;
}
/**
* public function trashed($where = NULL)
* Verifies if a record (row) is soft_deleted or not
* @param null $where
* @return bool
*/
public function trashed($where = NULL)
{
$this->only_trashed();
if(isset($where))
{
$this->where($where);
}
$this->limit(1);
$query = $this->_database->get($this->table);
if($query->num_rows() == 1)
{
return TRUE;
}
return FALSE;
}
public function _get_joined($requested)
{
$this->_database->join($this->_relationships[$requested['request']]['foreign_table'], $this->table.'.'.$this->_relationships[$requested['request']]['local_key'].' = '.$this->_relationships[$requested['request']]['foreign_table'].'.'.$this->_relationships[$requested['request']]['foreign_key']);
$the_select = '';
if(!empty($requested['parameters']))
{
if(array_key_exists('fields',$requested['parameters']))
{
$fields = explode(',', $requested['parameters']['fields']);
$sub_select = array();
foreach ($fields as $field)
{
$sub_select[] = ((strpos($field,'.')===FALSE) ? '`' . $this->_relationships[$requested['request']]['foreign_table'] . '`.`' . trim($field) . '`' : trim($field)).' AS '.$requested['request'].'_'.trim($field);
}
$the_select = implode(',', $sub_select);
}
else
{
$the_select = $this->_relationships[$requested['request']]['foreign_table'] . '.*';
}
}
$this->_database->select($the_select);
unset($this->_requested[$requested['request']]);
}
/**
* public function get()
* Retrieves one row from table.
* @param null $where
* @return mixed
*/
public function get($where = NULL)
{
$data = $this->_get_from_cache();
if(isset($data) && $data !== FALSE)
{
$this->_database->reset_query();
if(isset($this->_cache)) unset($this->_cache);
return $data;
}
else
{
$this->trigger('before_get');
if($this->_select)
{
$this->_database->select($this->_select);
}
if(!empty($this->_requested))
{
foreach($this->_requested as $requested)
{
if(isset($requested['parameters']['join']))
{
$this->_get_joined($requested);
}
else
{
$this->_database->select($this->table.'.'.$this->_relationships[$requested['request']]['local_key']);
}
}
}
if(isset($where))
{
$this->where($where);
}
if($this->soft_deletes===TRUE)
{
$this->_where_trashed();
}
$this->limit(1);
$query = $this->_database->get($this->table);
$this->_reset_trashed();
if ($query->num_rows() == 1)
{
$row = $query->row_array();
$row = $this->trigger('after_get', $row);
$row = $this->_prep_after_read(array($row),FALSE);
$row = is_array($row) ? $row[0] : $row->{0};
$this->_write_to_cache($row);
return $row;
}
else
{
return FALSE;
}
}
}
/**
* public function get_all()
* Retrieves rows from table.
* @param null $where
* @return mixed
*/
public function get_all($where = NULL)
{
$data = $this->_get_from_cache();
if(isset($data) && $data !== FALSE)
{
$this->_database->reset_query();
if(isset($this->_cache)) unset($this->_cache);
return $data;
}
else
{
$this->trigger('before_get');
if(isset($where))
{
$this->where($where);
}
if($this->soft_deletes===TRUE)
{
$this->_where_trashed();
}
if(isset($this->_select))
{
$this->_database->select($this->_select);
}
if(!empty($this->_requested))
{
foreach($this->_requested as $requested)
{
if(isset($requested['parameters']['join']))
{
$this->_get_joined($requested);
}
else
{
$this->_database->select($this->table.'.'.$this->_relationships[$requested['request']]['local_key']);
}
}
}
$query = $this->_database->get($this->table);
$this->_reset_trashed();
if($query->num_rows() > 0)
{
$data = $query->result_array();
$data = $this->trigger('after_get', $data);
$data = $this->_prep_after_read($data,TRUE);
$this->_write_to_cache($data);
return $data;
}
else
{
return FALSE;
}
}
}
/**
* public function count_rows()
* Retrieves number of rows from table.
* @param null $where
* @return integer
*/
public function count_rows($where = NULL)
{
if(isset($where))
{
$this->where($where);
}