Skip to content

Instantly share code, notes, and snippets.

@as3eem
Created August 23, 2017 12:21
Show Gist options
  • Save as3eem/06beaee525099f889f55ba179c4a237a to your computer and use it in GitHub Desktop.
Save as3eem/06beaee525099f889f55ba179c4a237a to your computer and use it in GitHub Desktop.
Ultimate model class to use in codeigniter with all mysql functions (including CRUD) defined
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model
{
function __construct() {
parent::__construct();
}
function get_table() {
$table = "service"; //your table name here
return $table;
}
function get($order_by){
$table = $this->get_table();
$this->db->order_by($order_by);
$query=$this->db->get($table);
return $query;
}
function get_with_limit($limit, $offset, $order_by) {
$table = $this->get_table();
$this->db->limit($limit, $offset);
$this->db->order_by($order_by);
$query=$this->db->get($table);
return $query;
}
function get_where($id){
$table = $this->get_table();
$this->db->where('id', $id);
$query=$this->db->get($table);
return $query;
}
function get_where_custom($col, $value) {
$table = $this->get_table();
$this->db->where($col, $value);
$query=$this->db->get($table);
return $query;
}
function _insert($data){
$table = $this->get_table();
$this->db->insert($table, $data);
}
function _update($id, $data){
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->update($table, $data);
}
function _delete($id){
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}
function count_where($column, $value) {
$table = $this->get_table();
$this->db->where($column, $value);
$query=$this->db->get($table);
$num_rows = $query->num_rows();
return $num_rows;
}
function count_all() {
$table = $this->get_table();
$query=$this->db->get($table);
$num_rows = $query->num_rows();
return $num_rows;
}
function get_max() {
$table = $this->get_table();
$this->db->select_max('id');
$query = $this->db->get($table);
$row=$query->row();
$id=$row->batch_id;
return $id;
}
function _custom_query($mysql_query) {
$query = $this->db->query($mysql_query);
return $query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment