MY_Model in CodeIgniter
This post was originally published in 2010
The tips and techniques explained may be outdated.
Hi,
I wanted to share with you MY_Model I’ve written and use for most of my CodeIgniter apps.
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 | /** * This is the basic model class. * * @package - Infrastructure * @category - Model * @author - Udi Mosayev @ umNet */ class MY_Model extends CI_Model { // The main table name public $table; private $modelName; public function MY_Model() { parent::CI_Model(); $this->table = ''; $this->modelName = __CLASS__; } // Log as Error each time nonexisting method called. public function __call($name, $arguments) { $args = implode(',',$arguments); $this->log('error', $name.'('.$args.') Not exists'); return FALSE; } /** * Logs an error * @param String $level * @param String $msg */ protected function log($level, $msg) { log_message($level, __CLASS__.'->'.__METHOD__.' :: <strong>'.$msg.'</strong> | In: '.__FILE__.' Line: '.__LINE__); } /** * This method inserts some array of data into the db * @param Array $data */ public function save($data) { if(is_array($data)) { $this->db->insert($this->table, $data); return $this->db->insert_id(); } else { $this->log('error', 'got non-array param.', __METHOD__); return FALSE; } } /** * This method updates fields in my table. * @param String $fieldName * @param String $value * @param Integer $RowID */ public function updateField($fieldName, $fieldValue, $rowID) { if(empty($fieldName)) { $this->log('error', 'got empty fieldName', __METHOD__); return FALSE; } else if(empty($fieldValue)) { $this->log('error','got empty fieldValue', __METHOD__); return FALSE; } else if(!is_numeric($rowID)) { $this->log('error', 'got non-numeric RowID: '.$rowID, __METHOD__); return FALSE; } else { // Wrtite the old&new data to history. $this->history->write($this->table, $rowID, $fieldName, $fieldValue); $this->db->where('ID', $rowID); $this->db->update($this->table, array($fieldName => $fieldValue)); return TRUE; } } /** * Updates whole row [unlike updateFIeld()] * @param Array $RowData * @param Integer $RowID */ public function update($RowData, $RowID) { if(!is_array($RowData)) { $this->log('error', 'supposed to get an array!'); return FALSE; } else if(!is_numeric($RowID)) { $this->log('error', 'got non-numeric RowID: '.$RowID, __METHOD__); return FALSE; } else { // write the old&new data to history foreach($RowData as $fieldName=>$fieldValue) { log_message('debug', 'Running history->write() with: history->write('.$this->table.', '.$RowID.', '.$fieldName.', '.$fieldValue.')'); $this->history->write($this->table, $RowID, $fieldName, $fieldValue); } $this->db->where('ID', $RowID); $this->db->update($this->table, $RowData); } } /** * This method returns all the rows of this model * @param Array $where */ public function getWhere($where = "") { // if not array, then assign defaultive array for where clause if(!is_array($where)) $where = array('IsDeleted' => 0); $query = $this->db->get_where($this->table, $where); return $query; } /** * This method gets 1 row from a table and returns it. * @param Integer $RowID */ public function get($RowID) { if(!is_numeric($RowID)) { $this->log('error', 'Did NOT got a numeric RowID.'); return FALSE; } else { $query = $this->db->get_where($this->table, array('ID' => $RowID)); return $query; } } public function delete($RowID) { if(is_numeric($RowID)) { $this->updateField('IsDeleted', 1, $RowID); return TRUE; } else { return FALSE; } } } /* End of file MY_Model.php*/ /* Location: ./application/model/MY_Model.php */ |
I need to explain few things:
- I use logical delete, I have field called IsDeleted its TINYINT, and if I want to delete certain row I update its value to 1. This method has lots of benefits which I can’t discuss now.
- I use two “read” methods, get() and getWhere(), I KNOW that get() should use getWhere inside, but I don’t really have the time for this little fix
- This class rewritten for CodeIgniter 2.0, so that’s why I extend CI_Model, if you’re using CodeIgniter 1.7 just write Model instead.
- In each app I add more methods, methods that I need in all my models, or at least in some of them. For example a method that re-orders the item in the table.
Ask any thing you want, I’ll be happy to help.
UPDATE:
In line 98 you see this:
1 | $this->history->write($this->table, $RowID, $fieldName, $fieldValue); |
if you remember, I wrote about System Restore feature in Must Have Features in you CMS, this is my implementation for this feature. Thank you Zack for noticing!


