forked from NaszvadiG/my_model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMY_Model.php
More file actions
1420 lines (1181 loc) · 41.4 KB
/
MY_Model.php
File metadata and controls
1420 lines (1181 loc) · 41.4 KB
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 if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Model
*
* An extension of CodeIgniter's built-in model that provides a convenient
* base to quickly and easily build your database-backed models off of.
*
* Provides observers, soft-deletes, basic CRUD functions, helpful functions,
* and more.
*
* This pulls many ideas and inspiration from Jamie Rumbelow's excellent MY_Model
* and the ideas described in his CodeIgniter Handbook, as well as from Laravel
* and Rails.
*
* To help in master/slave scenarios where you might have a different database
* to read from then you do to write to. By default, the model will only load
* the 'default' database and use it form both read and write connections.
*
* @author Lonnie Ezell & the Bonfire Dev Team
* @copyright Copyright (c) 2011 - 2013, Lonnie Ezell and the Bonfire Dev Team
* @license MIT
* @link http://github.com/lonnieezell/my_model
*/
class MY_Model extends CI_Model {
/**
* The model's default table name.
*
* @var string;
*/
protected $table_name;
/**
* The model's default primary key.
*
* @var string
*/
protected $primary_key = 'id';
/**
* Field name to use to the created time column in the DB table.
*
* @var string
* @access protected
*/
protected $created_field = 'created_on';
/**
* Field name to use to the modified time column in the DB table.
*
* @var string
* @access protected
*/
protected $modified_field = 'modified_on';
/**
* Whether or not to auto-fill a 'created_on' field on inserts.
*
* @var boolean
* @access protected
*/
protected $set_created = TRUE;
/**
* Whether or not to auto-fill a 'modified_on' field on updates.
*
* @var boolean
* @access protected
*/
protected $set_modified = TRUE;
/*
Var: $log_user
If TRUE, will log user id for 'created_by', 'modified_by' and 'deleted_by'.
Access:
Protected
*/
protected $log_user = TRUE;
/*
Var: $created_by_field
Field name to use to the created by column in the DB table.
Access:
Protected
*/
protected $created_by_field = 'created_by';
/*
Var: $modified_by_field
Field name to use to the modified by column in the DB table.
Access:
Protected
*/
protected $modified_by_field = 'modified_by';
/*
Var: $deleted_by_field
Field name to use for the deleted by column in the DB table.
Access:
Protected
*/
protected $deleted_by_field = 'deleted_by';
/**
* The type of date/time field used for created_on and modified_on fields.
* Valid types are: 'int', 'datetime', 'date'
*
* @var string
* @access protected
*/
protected $date_format = 'int';
/**
* Support for soft_deletes.
*/
protected $soft_deletes = FALSE;
protected $soft_delete_key = 'deleted';
protected $temp_with_deleted = FALSE;
/**
* Various callbacks available to the class. They are simple lists of
* method names (methods will be ran on $this).
*/
protected $before_insert = array();
protected $after_insert = array();
protected $before_update = array();
protected $after_update = array();
protected $before_find = array();
protected $after_find = array();
protected $before_delete = array();
protected $after_delete = array();
protected $callback_parameters = array();
/**
* Protected, non-modifiable attributes
*/
protected $protected_attributes = array();
/**
* An array of validation rules. This needs to be the same format
* as validation rules passed to the Form_validation library.
*
* You can override this value into a string that is the name
* of a rule group, if you've saved the rules array into
* a config file as described in the CodeIgniter User Guide.
* http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#savingtoconfig
*/
protected $validation_rules = array();
/**
* An array of rules to be added to the validation rules during
* insert type methods. This is commonly used to add a required rule
* that is only needed during inserts and not updates. The array
* requires the field_name as the key and the additional rules
* as the value string.
*
* array(
* 'password' => 'required|matches[password]',
* 'username' => 'required'
* )
*/
protected $insert_validation_rules = array();
/**
* Optionally skip the validation. Used in conjunction with
* skip_validation() to skip data validation for any future calls.
*/
protected $skip_validation = FALSE;
/**
* By default, we return items as objects. You can change this for the
* entire class by setting this value to 'array' instead of 'object'.
* Alternatively, you can do it on a per-instance basis using the
* 'as_array()' and 'as_object()' methods.
*/
protected $return_type = 'object';
protected $temp_return_type = NULL;
/*
If TRUE, inserts will return the last_insert_id. However,
this can potentially slow down large imports drastically
so you can turn it off with the return_insert_id(false) method.
This will simply return TRUE, instead.
IMPORTANT: Turning this to false will bypass any after_insert
trigger events.
*/
protected $return_insert_id = true;
/**
* The database connection to use for all write-type calls.
*/
protected $dbw;
/**
* The database connection to use for all read-type calls.
*/
protected $dbr;
//--------------------------------------------------------------------
/**
* Gets our model up and running.
*
* You can provide your own connections for read and/or write databases
* by passing them in the constructor.
*
* @param DB object $write_db A CI_DB connection.
* @param DB object $read_db A CI_DB connection.
*/
public function __construct(&$write_db=null, &$read_db=null)
{
// Always protect our attributes
array_unshift($this->before_insert, 'protect_attributes');
array_unshift($this->before_update, 'protect_attributes');
// Check our auto-set features and make sure they are part of
// our observer system.
if ($this->set_created === true) array_unshift($this->before_insert, 'created_on');
if ($this->set_modified === true) array_unshift($this->before_update, 'modified_on');
// Make sure our temp return type is correct.
$this->temp_return_type = $this->return_type;
/*
Passed DB connections?
*/
if (is_object($write_db))
{
$this->dbw = $write_db;
}
if (is_object($read_db))
{
$this->dbr = $read_db;
}
/*
Make sure we have a dbw and a dbr to use.
Start with the writeable db. If we don't have
one (passed in) then try to use the global $this->db
object, if exists. Otherwise, load the database
and then use $this->db
*/
if (!isset($this->db))
{
$this->load->database();
}
if ( ! is_object($this->dbw))
{
$this->dbw = $this->db;
}
// If there's no read db, use the write db.
if ( ! is_object($this->dbr))
{
$this->dbr = $this->dbw;
}
log_message('debug', 'BF_Model Class Initialized');
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// CRUD Methods
//--------------------------------------------------------------------
/**
* A simple way to grab the first result of a search only.
*/
public function first()
{
$rows = $this->limit(1)->find_all();
if (is_array($rows) && count($rows) == 1)
{
return $rows[0];
}
return $rows;
}
//--------------------------------------------------------------------
/**
* Finds a single record based on it's primary key. Will ignore deleted rows.
*
* @param mixed $id The primary_key value of the object to retrieve.
* @return object
*/
public function find($id)
{
$this->trigger('before_find');
// Ignore any soft-deleted rows
if ($this->soft_deletes && $this->temp_with_deleted !== TRUE)
{
$this->dbr->where($this->table_name.'.'.$this->soft_delete_key, FALSE);
}
$this->dbr->where($this->primary_key, $id);
$row = $this->dbr->get($this->table_name);
$row = $row->{$this->_return_type()}();
$row = $this->trigger('after_find', $row);
if ($this->temp_return_type == 'json')
{
$row = json_encode($row);
}
// Reset our return type
$this->temp_return_type = $this->return_type;
return $row;
}
//--------------------------------------------------------------------
/**
* Fetch a single record based on an arbitrary WHERE call. Can be
* any valid value to $this->db->where(). Will not pull in deleted rows
* if using soft deletes.
*
* @return object
*/
public function find_by()
{
$where = func_get_args();
$this->_set_where($where, 'dbr');
// Ignore any soft-deleted rows
if ($this->soft_deletes && $this->temp_with_deleted !== TRUE)
{
$this->dbr->where($this->soft_delete_key, FALSE);
}
$this->trigger('before_find');
$row = $this->dbr->get($this->table_name);
$row = $row->{$this->_return_type()}();
$row = $this->trigger('after_find', $row);
if ($this->temp_return_type == 'json')
{
$row = json_encode($row);
}
// Reset our return type
$this->temp_return_type = $this->return_type;
return $row;
}
//--------------------------------------------------------------------
/**
* Retrieves a number of items based on an array of primary_values passed in.
*
* @param array $values An array of primary key values to find.
*
* @return object or FALSE
*/
public function find_many($values)
{
$this->dbr->where_in($this->primary_key, $values);
return $this->find_all();
}
//--------------------------------------------------------------------
/**
* Retrieves a number of items based on an arbitrary WHERE call. Can be
* any set of parameters valid to $db->where.
*
* @return object or FALSE
*/
public function find_many_by()
{
$where = func_get_args();
$this->_set_where($where, 'dbr');
return $this->find_all();
}
//--------------------------------------------------------------------
/**
* Fetch all of the records in the table. Can be used with scoped calls
* to restrict the results.
*
* @return object or FALSE
*/
public function find_all()
{
$this->trigger('before_find');
// Ignore any soft-deleted rows
if ($this->soft_deletes && $this->temp_with_deleted !== TRUE)
{
$this->dbr->where($this->table_name.'.'.$this->soft_delete_key, FALSE);
}
$rows = $this->db->get($this->table_name);
$rows = $rows->{$this->_return_type(true)}();
if (is_array($rows))
{
foreach ($rows as $key => &$row)
{
$row = $this->trigger('after_find', $row, ($key == count($rows) - 1));
}
}
if ($this->temp_return_type == 'json')
{
$rows = json_encode($rows);
}
// Reset our return type
$this->temp_return_type = $this->return_type;
return $rows;
}
//--------------------------------------------------------------------
/**
* Inserts data into the database.
*
* @param array $data An array of key/value pairs to insert to database.
*
* @return mixed The primary_key value of the inserted record, or FALSE.
*/
public function insert($data)
{
$data = $this->trigger('before_insert', $data);
if ($this->skip_validation === FALSE)
{
$data = $this->validate($data, 'insert');
// If $data is false, we didn't validate
// so we need to get out of here.
if ( ! $data)
{
return FALSE;
}
}
if($this->set_created and empty($data[$this->created_field]))
{
$data[$this->created_field] = $this->set_date();
}
if($this->log_user)
{
$data[$this->created_by_field] = $this->auth->user_id();
}
$this->dbw->insert($this->table_name, $data);
if ($this->return_insert_id)
{
$id = $this->dbw->insert_id();
$this->trigger('after_insert', $id);
}
else
{
$id = true;
}
return $id;
}
//--------------------------------------------------------------------
/**
* Inserts multiple rows into the database at once. Takes an associative
* array of value pairs.
*
* $data = array(
* array(
* 'title' => 'My title'
* ),
* array(
* 'title' => 'My Other Title'
* )
* );
*
* @param array $data An associate array of rows to insert
*
* @return bool
*/
public function insert_batch($data)
{
if ($this->skip_validation === FALSE)
{
$data = $this->validate($data, 'insert');
if ( ! $data)
{
return FALSE;
}
}
$data['batch'] = true;
$data = $this->trigger('before_insert', $data);
unset($data['batch']);
return $this->dbw->insert_batch($this->table_name, $data);
}
//--------------------------------------------------------------------
/**
* Updates an existing record in the database.
*
* @param mixed $id The primary_key value of the record to update.
* @param array $data An array of value pairs to update in the record.
* @return bool
*/
public function update($id, $data)
{
$data = $this->trigger('before_update', $data);
if ($this->skip_validation === FALSE)
{
$data = $this->validate($data);
if ( ! $data)
{
return FALSE;
}
}
$this->dbw->where($this->primary_key, $id);
if ($this->log_user)
{
$data[$this->modified_by_field] = $this->auth->user_id();
}
$this->dbw->set($data);
$result = $this->dbw->update($this->table_name);
$this->trigger('after_update', array($data, $result));
return $result;
}
//--------------------------------------------------------------------
/**
* Updates multiple records in the database at once.
*
* $data = array(
* array(
* 'title' => 'My title',
* 'body' => 'body 1'
* ),
* array(
* 'title' => 'Another Title',
* 'body' => 'body 2'
* )
* );
*
* The $where_key should be the name of the column to match the record on.
* If $where_key == 'title', then each record would be matched on that
* 'title' value of the array. This does mean that the array key needs
* to be provided with each row's data.
*
* @param array $data An associate array of row data to update.
* @param string $where_key The column name to match on.
* @return bool
*/
public function update_batch($data, $where_key)
{
foreach ($data as &$row)
{
$row = $this->trigger('before_update', $row);
}
$result = $this->dbw->update_batch($this->table_name, $data, $where_key);
foreach ($data as &$row)
{
$this->trigger('after_update', array($row, $result));
}
return $result;
}
//--------------------------------------------------------------------
/**
* Updates many records by an array of ids.
*
* While update_batch() allows modifying multiple, arbitrary rows of data
* on each row, update_many() sets the same values for each row.
*
* $ids = array(1, 2, 3, 5, 12);
* $data = array(
* 'deleted_by' => 1
* );
*
* $this->model->update_many($ids, $data);
*
* @param array $ids An array of primary_key values to update.
* @param array $data An array of value pairs to modify in each row.
* @return bool
*/
public function update_many($ids, $data)
{
$data = $this->trigger('before_update', $data);
if ($this->skip_validation === FALSE)
{
$data = $this->validate($data);
if ( ! $data)
{
return FALSE;
}
}
$this->dbw->where_in($this->primary_key, $ids);
$this->dbw->set($data);
$result = $this->dbw->update($this->table_name);
$this->trigger('after_update', array($data, $result));
return $result;
}
//--------------------------------------------------------------------
/**
* Update records in the database using a standard WHERE clause.
*
* Your last parameter should be the $data array with values to update
* on the rows. Any additional parameters should be provided to make up
* a typical WHERE clause. This could be a single array, or a column name
* and a value.
*
* $data = array('deleted_by' => 1);
* $wheres = array('user_id' => 15);
*
* $this->update_by($wheres, $data);
* $this->update_by('user_id', 15, $data);
*
* @param array $data An array of data pairs to update
* @param one or more WHERE-acceptable entries.
* @return bool
*/
public function update_by()
{
$args = func_get_args();
$data = array_pop($args);
$this->_set_where($args, 'dbw');
$data = $this->trigger('before_update', $data);
if ($this->skip_validation === FALSE)
{
$data = $this->validate($data);
if ( ! $data)
{
return FALSE;
}
}
$this->dbw->set($data);
$result = $this->dbw->update($this->table_name);
$this->trigger('after_update', array($data, $result));
return $result;
}
//--------------------------------------------------------------------
/**
* Updates all records and sets the value pairs passed in the array.
*
* @param array $data An array of value pairs with the data to change.
* @return bool
*/
public function update_all($data)
{
$data = $this->trigger('before_update', $data);
if ($skip_validation === FALSE)
{
$data = $this->validate($data);
if ( ! $data)
{
return FALSE;
}
}
$this->dbw->set($data);
$result = $this->dbw->update($this->table_name);
$this->trigger('after_update', array($data, $result));
return $result;
}
//--------------------------------------------------------------------
/**
* Deletes a row by it's primary key value.
*
* @param mixed $id The primary key value of the row to delete.
* @return bool
*/
public function delete($id)
{
$this->trigger('before_delete', $id);
$this->dbw->where($this->primary_key, $id);
if ($this->soft_deletes)
{
$sets = $this->log_user ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->auth->user_id()) : array($this->soft_delete_key => 1);
$result = $this->dbw->update($this->table_name, $sets);
}
// Hard Delete
else {
$result = $this->dbw->delete($this->table_name);
}
$this->trigger('after_delete', $result);
return $result;
}
//--------------------------------------------------------------------
public function delete_by()
{
$where = func_get_args();
$this->_set_where($where, 'dbw');
$where = $this->trigger('before_delete', $where);
if ($this->soft_deletes)
{
$sets = $this->log_user ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->auth->user_id()) : array($this->soft_delete_key => 1);
$result = $this->dbw->update($this->table_name, $sets);
}
else
{
$result = $this->dbw->delete($this->table_name);
}
$this->trigger('after_delete', $result);
return $result;
}
//--------------------------------------------------------------------
/**
* A convenience method to delete many rows of data when you have an
* array of id's to delete. The same thing as:
*
* $this->model->where_in($ids)->delete();
*
* @param array $ids An array of primary keys to be deleted.
*/
public function delete_many($ids)
{
$ids = $this->trigger('before_delete', $ids);
$this->dbw->where_in($ids);
if ($this->soft_deletes)
{
$sets = $this->log_user ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->auth->user_id()) : array($this->soft_delete_key => 1);
$result = $this->dbw->update($this->table_name, $sets);
}
else
{
$result = $this->dbw->delete($this->table_name);
}
$this->trigger('after_delete', $result);
return $result;
}
//--------------------------------------------------------------------
/**
* Empty a table.
*
* @param $table String Table name.
* @return mixed
*/
public function empty_table($table = NULL)
{
return $this->db->empty_table($table);
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Scope Methods
//--------------------------------------------------------------------
/**
* Sets the value of the soft deletes flag.
*
* @param boolean $val If TRUE, should perform a soft delete. If FALSE, a hard delete.
*/
public function soft_delete($val=TRUE)
{
$this->soft_deletes = $val;
return $this;
}
//--------------------------------------------------------------------
/**
* Temporarily sets our return type to an array.
*/
public function as_array()
{
$this->temp_return_type = 'array';
return $this;
}
//--------------------------------------------------------------------
/**
* Temporarily sets our return type to an object.
*/
public function as_object()
{
$this->temp_return_type = 'object';
return $this;
}
//--------------------------------------------------------------------
/**
* Temporarily sets our object return to a json object.
*/
public function as_json()
{
$this->temp_return_type = 'json';
return $this;
}
//--------------------------------------------------------------------
/**
* Also fetches deleted items for this request only.
*/
public function with_deleted()
{
$this->temp_with_deleted = TRUE;
return $this;
}
//--------------------------------------------------------------------
/**
* Sets the value of the skip_validation flag
*
* @param Bool $skip (optional) whether to skip validation in the model
*
* @return Object returns $this to allow method chaining
*/
public function skip_validation($skip=TRUE)
{
$this->skip_validation = $skip;
return $this;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Utility Methods
//--------------------------------------------------------------------
/**
* Counts number of rows modified by an arbitrary WHERE call.
* @return INT
*/
public function count_by()
{
$where = func_get_args();
$this->_set_where($where, 'dbr');
return $this->dbr->count_all_results($this->table_name);
}
//--------------------------------------------------------------------
/**
* Counts total number of records, disregarding any previous conditions.
*
* @return int
*/
public function count_all()
{
return $this->dbr->count_all($this->table_name);
}
//--------------------------------------------------------------------
/**
* Getter for the table name.
*
* @return string The name of the table used by this class.
*/
public function table()
{
return $this->table_name;
}
//--------------------------------------------------------------------
/**
* A convenience method to return options for form dropdown menus.
*
* Can pass either Key ID and Label Table names or Just Label Table name.
*
* @return array The options for the dropdown.
*/
function format_dropdown()
{
$args = & func_get_args();
if (count($args) == 2)
{
list($key, $value) = $args;
}
else
{
$key = $this->primary_key;
$value = $args[0];
}
$query = $this->dbr->select(array($key, $value))->get($this->table_name);
$options = array();
foreach ($query->result() as $row)
{
$options[$row->{$key}] = $row->{$value};
}
return $options;
}
//--------------------------------------------------------------------
/**
* A convenience method to return only a single field of the specified row.
*
* @param mixed $id The primary_key value to match against.
* @param string $field The field to search for.
*
* @return bool|mixed The value of the field.
*/
public function get_field($id=NULL, $field='')
{
$this->dbr->select($field);
$this->dbr->where($this->primary_key, $id);
$query = $this->dbr->get($this->table);
if ($query && $query->num_rows() > 0)