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 */

Related Posts

Tags: CodeIgniter, PHP, Web Development

  • http://www.thefreshuk.com/blog James W. Dunne

    Generally good and very useful!

    However, I noticed you recommend switching the parent class name depending on the CI version. I would minimise the need for this and use something like this at the top:

    if (! class_exists(‘Model’)) { class Model extends CI_Model { } }

    I wouldn’t directly copy and paste this since I’ve not tested it but the concept still applies.

    There’s probably some hackery for the PHP4/PHP5 constructor issue too, but it’s late and I am in need of sleep!

    • http://udiudi.com Udi Mosayev

      You are probably right.
      This library is for my project, I guess if someone needs to use it he’ll modify it the way its best suits him.

      Thank you for you comment!

  • panega

    hey Udi, i’m newbies..

    can you give examples of the use for this model “MY_Model” v2.0 For CodeIgniter

    ———————

    whether its use same way with this tutorial?:
    http://maestric.com/doc/php/codeigniter_models

    thanks…

    -panega-

    • http://udiudi.com Udi Mosayev

      Hi,

      Basically the usage is the same, you load it the same way in the controllers..

  • Christher

    How can I extend My_model like this.

    class User extends My_Model {…}

    • http://twitter.com/udiudi Udi Mosayev

      Hi,

      Here’s a really good explanation: http://codeigniter.com/user_guide/general/core_classes.html

      Basically – you need to create class called MY_Model in application/core folder, and than create a model that extends it, just the way you wrote in you comment:
      class User extends My_Model {….}

WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera