Base Model: Create and Use Base Model in Codeigniter

Codeigniter Base Model

In this post, We will discuss about base model in Codeigniter. Purpose of uses of Base model is to put all common CRUD functions in one model and can be extended to each child model. In every CI project i uses this base model to increase more productivity.

1.What is Base model?

Base model is a model where we can put all our common function which may be used within entire system.
By extending this base model to all other model we can use functions easily and with less code.

2. Why Use base Model?

Purpose of uses of base model is to removing repetition of code,increasing productivity and coding in less amount of time.

3.Key Features of Codeigniter Base Model:

a.) CRUD and all common functionality in one place.
b.) Easy to use.

4. Need to know:

Before we creating CI base model we need to know few things.
a.) The class declaration of model must extend the parent class.
b.) As defined subclass_prefix in config file, name of new class for model must be prefixed with ‘MY_’.You can change this. This is configurable.

5. Implementation of Base Model:

Lets start with the example of base model. Follow these steps as i am showing below.

I.Create class named MY_Model.php inside application/core and put all common functions inside here. Here is how code looks.

<?php
class MY_Model extends CI_Model{
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;
function __construct()
{
parent::__construct();
}
public function array_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
} elseif ($single == TRUE) {
$method = 'row';
} else {
$method = 'result';
}
if (!count($this->db->order_by($this->_order_by))) {
$this->db->order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE)
{
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL)
{
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
} // Update
else {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
/* To delete single row */
public function delete($id)
{
$filter = $this->_primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->_primary_key, $id);
$this->db->limit(1);
$this->db->delete($this->_table_name);
}
/* To delete multiple row data */
public function delete_multiple($where)
{
$this->db->where($where);
$this->db->delete($this->_table_name);
}
}
?>

II. Now in other model we will extend base model to use all functions of base model as i am showing below.

<?php
class PayrollModel extends MY_Model{
public $_table_name;
public $_order_by;
public $_primary_key;
}
?>

III. Next create a controller where we can call model function and each model automatically loads base model by extending it.

<?php
class Payroll extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('PayrollModel');
}
public function payroll_function()
{
$id = 'Unique Id';

/* to get all input post data */
$data = $this->PayrollModel->array_post(array('bank', 'branch'));
$this->PayrollModel->_table_name = "table_name"; /* table name */
$this->PayrollModel->_primary_key = "id"; /* primary key */

/* For insert */
$this->PayrollModel->save($data); /* this will insert data */

/* For update */
$this->PayrollModel->save($data, $id); /* this will update data */

/* To get Data with filter */
$data['salary_allowance_info'] = $this->PayrollModel->get_by(array('id' => $id), FALSE); /* First parameter can contain multiple array, second is for result/row */

/* To delete row with unique id */
$this->PayrollModel->_table_name = "tbl_salary_template"; // table name
$this->PayrollModel->_primary_key = "id"; // $id
$this->PayrollModel->delete($id);

/* Delete multiple data */
$this->PayrollModel->_table_name = "table_name"; // table name
$this->PayrollModel->delete_multiple(array('ids' => $id));
}
}

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , ,