-
Notifications
You must be signed in to change notification settings - Fork 0
/
MY_Model.php
92 lines (78 loc) · 2.26 KB
/
MY_Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class MY_Model extends CI_Model{
public $tablename;
public $id_field;
function __construct(){
}
// get table name
function get_table(){
$table = $this->tablename;
return $table;
}
// get all records
function get($order_by, $order){
$table = $this->get_table();
$this->db->order_by($order_by, $order);
$query=$this->db->get($table);
return $query;
}
// get all records with limit and offset
function get_with_limit($limit, $offset, $order_by, $order){
$table = $this->get_table();
$this->db->limit($limit, $offset);
$this->db->order_by($order_by, $order);
$query=$this->db->get($table);
return $query;
}
// get single record from id
function get_where_id($id){
$table = $this->get_table();
$this->db->where($this->id_field, $id);
$query=$this->db->get($table);
return $query;
}
// get records from specific field value
function get_where_custom($col, $value){
$table = $this->get_table();
$this->db->where($col, $value);
$query=$this->db->get($table);
return $query;
}
// insert record
function _insert($data){
$table = $this->get_table();
$this->db->insert($table, $data);
$insert_id = $this->db->insert_id();
return $insert_id;
}
// update record
function _update($id, $data){
$table = $this->get_table();
$this->db->where($this->id_field, $id);
$this->db->update($table, $data);
return $this->db->affected_rows();
}
// delete record
function _delete($id){
$table = $this->get_table();
$this->db->where($this->id_field, $id);
$this->db->delete($table);
return $this->db->affected_rows();
}
// Get max id from table
function get_max(){
$table = $this->get_table();
$this->db->select_max($this->id_field);
$query = $this->db->get($table);
$row=$query->row();
$id=$row->{$this->id_field};
return $id;
}
// Run custom query
function _custom_query($mysql_query, $args = array()){
$query = $this->db->query($mysql_query, $args);
return $query;
}
}