Codeginator 4 Common Model
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 namespace App\Models; use CodeIgniter\Model; use CodeIgniter\Database\Config; class Commonmodel extends Model { var $db; function __construct() { parent::__construct(); $this->db = db_connect(); } //Insert Data function insert_data($table, $data) { foreach ($data as $key => $value) { if (is_null($value)) { $data[$key] = ""; } } if ($this->db->field_exists('created_datetime', $table)) { $data['created_datetime']=@date('Y-m-d H:i:s'); $data['updated_datetime']=@date('Y-m-d H:i:s'); } $this->db->table($table)->insert($data); $response = $this->db->insertID(); return $response; } //Update Data function update_data($table, $updatedata, $wheredata) { $query = $this->db->table($table)->where($wheredata)->set($updatedata)->update(); return $query; } //Select Data Single Row function select_data_single_row($table, $selectdata, $wheredata,$orderbycoloumn = null, $orderbydirection = null,$limit = null, $groupby = null) { $query = $this->db->table($table); $query->where($wheredata); $query->select($selectdata); if ($limit <> null) { $query->limit($limit); } if ($orderbycoloumn <> null && $orderbydirection <> null) { $query->order_by($orderbycoloumn, $orderbydirection); } if ($groupby <> null) { $query->group_by($groupby); } $response = (array)$query->get()->getRow(); return $response; } function select_raw_sql($sql, $type) { $query = $this->db->query($sql); if($type=="row"){ return $query->row_array(); }else{ return $query->result_array(); } } function query_raw_sql($sql) { return $this->db->query($sql); } //Select Data Array function select_data_array($table, $selectdata, $wheredata, $orderbycoloumn = null, $orderbydirection = null, $limit = null, $groupby = null,$having = null) { $query = $this->db->table($table); $query->select($selectdata); if(isset($wheredata) && !empty($wheredata)){ $query->where($wheredata); } if ($orderbycoloumn <> null && $orderbydirection <> null) { $query->order_by($orderbycoloumn, $orderbydirection); } if ($limit <> null) { $query->limit($limit); } if ($groupby <> null) { $query->group_by($groupby); } if ($having <> null) { $query->having($having); } $row = $query->get(); $response = $row->getResult(); return $response; } function select_pagination_data_array($table, $selectdata, $wheredata, $orderbycoloumn = null, $orderbydirection = null, $limit = null,$limit2 = null, $groupby = null,$having = null) { // with pagination $query = $this->db->table($table); $query->select($selectdata); if(isset($wheredata) && !empty($wheredata)){ $query->where($wheredata); } if ($orderbycoloumn <> null && $orderbydirection <> null) { $query->order_by($orderbycoloumn, $orderbydirection); } if ($limit <> null) { if ($limit2<>null) { if ($limit2<1) { $limit2=1; } $limit2=$limit*($limit2-1); $query->limit($limit,$limit2); }else{ $query->db->limit($limit); } } if ($groupby <> null) { $query->group_by($groupby); } if ($having <> null) { $query->having($having); } $row = $query->get(); $response = $row->getResult(); return $response; } }