MY_Model v2.0 For CodeIgniter
This post was originally published in 2010
The tips and techniques explained may be outdated.
Update: This post is very old. I’ve created new model class for my CodeIgniter projects, you can find it here: Updated MY_Model for CodeIgniter 2.0
1 2 3 4 5 6 7 8 | /** * This is the basic model class. * * @package - Infrastructure * @category - Model * @author - Udi Mosayev @ umNet * @property CI_DB_active_record $db */ |
class MY_Model extends CI_Model {
// The main table name
private $_table;
public function __construct() {
parent::__construct();
}
/**
* Setter/Getters for the table prop
* @param String $table
*/
public function setTable($table) {
$this->_table = $table;
}
public function getTable() {
return $this->_table;
}
// Log as Error each time nonexisting method called.
public function __call($name, $arguments) {
$args = implode(‘,’,$arguments);
log_message(‘error’, $name.’(‘.$args.’) Not exists.’);
return FALSE;
}
/**
* Checks if certain field in any row has a certain value.
* Its used to have a unique data like username, email etc.
* @param String $field Name of the field in the table to check the data in
* @param String $value The value to check if exists in that field
* @return Bool TRUE if this data exists, FALSE if unique
*/
public function is_duplicate($fieldName, $value) {
if(empty($fieldName) OR empty($value)) {
log_message(‘error’, ‘Got some empty param. field: ‘.$fieldName.’ | value: ‘.$value);
exit(0);
} else {
$this->db->select($fieldName);
$query = $this->db->get_where($this->getTable(), array($fieldName => $value));
if($query->num_rows > 0) {
return TRUE;
} else {
return FALSE;
}
}
return FALSE;
}
/**
* This method inserts some array of data into the db
* @param Array $data
* @return Int Insert ID
*/
public function add($data) {
if(is_array($data)) {
// $data['Reorder'] = $this->getLastOrder()+1;
$this->db->insert($this->getTable(), $data);
return $this->db->insert_id();
} else {
log_message(‘error’, ‘Got non-array param.’);
return FALSE;
}
}
/**
* This method updates fields in my table.
* @param String $fieldName
* @param String $value
* @param Integer $id
*/
public function update_field($fieldName, $fieldValue, $id) {
if(empty($fieldName)) {
log_message(‘error’, ‘Got empty fieldName: ‘.$fieldName);
return FALSE;
}
else if(!is_numeric($id)) {
log_message(‘error’, ‘Got non-numeric id: ‘.$id);
return FALSE;
} else {
$this->db->where(‘id’, $id);
$query = $this->db->update($this->getTable(), array($fieldName => $fieldValue));
}
}
/**
* Updates whole row [unlike update_field()]
* @param Array $data
* @param Integer $id
*/
public function update($data, $id) {
if(!is_array($data)) {
log_message(‘error’, ‘Supposed to get an array!’);
return FALSE;
} else if(!is_numeric($id)) {
log_message(‘error’, ‘Got non-numeric id: ‘.$id);
return FALSE;
} else {
/* write the old&new data to history
foreach($data as $fieldName=>$fieldValue) {
$this->history->write($this->getTable(), $id, $fieldName, $fieldValue);
}
*/
$this->db->where(‘id’, $id);
$this->db->update($this->getTable(), $data);
}
}
/**
* This method returns all the rows of this model
* @param Array $where Array of field=>value
* @param Array $orderby field to order by and side – desc/asc.
* @return CI_Query_Resource the $query object
*/
public function get_where($where = array(‘is_deleted’ => 0), $orderBy = null) {
if($orderBy!=null)
$this->db->order_by($orderBy[1], $orderBy[2]);
$query = $this->db->get_where($this->getTable(), $where);
return $query;
}
/**
* This method gets 1 row from a table and returns it.
* @param Integer $id
* @return DB_Res $query CodeIgniter’s db resource
*/
public function get($id) {
if(!is_numeric($id)) {
log_message(‘error’, ‘Got not numeric id: ‘.$id);
return FALSE;
} else {
$query = $this->db->get_where($this->getTable(), array(‘id’ => $id));
return $query;
}
}
/**
* This method logically deletes a row.
* @param Integer $id
* @return Boolean
*/
public function delete($id) {
if(is_numeric($id)) {
$this->update_field(‘is_deleted’, 1, $id);
return TRUE;
} else {
return FALSE;
}
}
}
/* End of file MY_Model.php*/
/* Location: ./application/model/MY_Model.php */



