Hi,
I wanted to share with you MY_Model I’ve written and use for most of my CodeIgniter apps.
/**
* This is the basic model class.
*
* @package - Infrastructure
* @category - Model
* @author - Udi Mosayev @ umNet
* @link - udi.mosayev@gmail.com
* @since - Version 1.0
*/
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__.' :: '.$msg.' | 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.');
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');
return FALSE;
}
else if(empty($fieldValue)) {
$this->log('error','got empty fieldValue');
return FALSE;
}
else if(!is_numeric($rowID)) {
$this->log('error', 'got non-numeric RowID: '.$rowID);
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);
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:
$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!