Today tip is about extending the Codeigniter Model core class by adding some smart functions to it. After extending it, you no longer required to write any code in your model, except extending your model from our extended Model class and everything will be handled by extended class.
Before continuing with this, I assume you have some experience working with Codeigniter framework and know the folder strucure in it. As the first step, create a file in application/core folder and name it as “MY_model.php”.
Define our new model class in it extending Codeigniter core Model class. The coding will be like the following in first stage.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Model extends CI_Model
{
public $table;
public function __construct()
{
parent::__construct();
$this->table = get_Class($this);
$this->load->database();
}
}
In the above part we have added a constructor and a variable named $table. We are initializing the $table variable with the class name. We will use the class name as the table name in our methods, when table name is not passed to the mthods.
We are going to have 4 methods insert, update, search and save. Method names self explains the functionality. Method save will update the record when a record already exists with same parimary and inserts if no such record exists.
public function save($data,$tablename="")
{
if($tablename=="")
{
$tablename = $this->table;
}
$op = 'update';
$keyExists = FALSE;
$fields = $this->db->field_data($tablename);
foreach ($fields as $field)
{
if($field->primary_key==1)
{
$keyExists = TRUE;
if(isset($data[$field->name]))
{
$this->db->where($field->name, $data[$field->name]);
}
else
{
$op = 'insert';
}
}
}
if($keyExists && $op=='update')
{
$this->db->set($data);
$this->db->update($tablename);
if($this->db->affected_rows()==1)
{
return $this->db->affected_rows();
}
}
$this->db->insert($tablename,$data);
return $this->db->affected_rows();
}
function search($conditions=NULL,$tablename="",$limit=500,$offset=0)
{
if($tablename=="")
{
$tablename = $this->table;
}
if($conditions != NULL)
$this->db->where($conditions);
$query = $this->db->get($tablename,$limit,$offset=0);
return $query->result();
}
function insert($data,$tablename="")
{
if($tablename=="")
$tablename = $this->table;
$this->db->insert($tablename,$data);
return $this->db->affected_rows();
}
function update($data,$conditions,$tablename="")
{
if($tablename=="")
$tablename = $this->table;
$this->db->where($conditions);
$this->db->update($tablename,$data);
return $this->db->affected_rows();
}
function delete($conditions,$tablename="")
{
if($tablename=="")
$tablename = $this->table;
$this->db->where($conditions);
$this->db->delete($tablename);
return $this->db->affected_rows();
}
After adding above 4 methods, using our Model class is easy. Just create a model by inheriting the new model class. Below is an example class and described the usage below the coding.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Samlpe extends MY_Model
{
public function __construct()
{
parent::__construct();
}
}
We have defined the Model class without any methods in it, and see below how we can use it in a controller.
$this->load->model(“Sample”);
$condition['id'] = ’1′;
$this->sample->delete($condition); //Delete from Sample table, if same as class name then no need to pass table name
$this->sample->delete($condition,”AnotherTable”); //If delete from some other table
$data['fieldName'] = ‘field value’;
$data['fieldName2'] = ‘field value 2′
$this->sample->save($data);
I think this helped you in speeder development of Codeigniter PHP coding, next time will come up with some other tricks.