CodeIgniter Common Model

edited January 2021 in PHP

Include the below CommonModel in new projects, use the following methods wherever needed, avoid creating custom model functions as much as possible, all common operations have been included in this model, however, if you find some common operation is missing in this please get in touch with Dev Admin so that it can incorporate that operation.

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Commonmodel extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    //Insert Data
    function insert_data($table, $data)
    {
        $this->db->insert($table, $data);
        $response = $this->db->insert_id();
        return $response;
    }
    //Update Data
    function update_data($table, $updatedata, $wheredata)
    {
        $this->db->where($wheredata);
        $this->db->update($table, $updatedata);
        $response = $this->db->affected_rows();
        return $response;
    }
    //Select Data Single Row
    function select_data_single_row($table, $selectdata, $wheredata)
    {
        $this->db->select($selectdata);
        $this->db->from($table);
        $this->db->where($wheredata);
        $query = $this->db->get();
        $response = $query->row_array();
        return $response;
    }
    //Select Data Array
    function select_data_array($table, $selectdata, $wheredata, $orderbycoloumn = null, $orderbydirection = null, $limit = null, $groupby = null)
    {
        $this->db->select($selectdata);
        $this->db->from($table);
        $this->db->where($wheredata);
        if ($orderbycoloumn <> null && $orderbydirection <> null) {
            $this->db->order_by($orderbycoloumn, $orderbydirection);
        }
        if ($limit <> null) {
            $this->db->limit($limit);
        }
        if ($groupby <> null) {
            $this->db->group_by($groupby);
        }
        $query = $this->db->get();
        $response = $query->result_array();
        return $response;
    }
    //Get last executed query using $this->db->last_query();
}


Sign In or Register to comment.