-
Notifications
You must be signed in to change notification settings - Fork 2
/
EXT_Model.php
199 lines (179 loc) · 4.46 KB
/
EXT_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Codeigniter Core Model Extension
*
* Adds methods for chaining and some common database queries.
*
* NOTICE OF LICENSE
*
* Licensed under the Creative Commons Attribution 3.0 Unported License.
*
* This source file is subject to the Creative Commons Attribution 3.0 Unported License
* that is available through http://creativecommons.org/licenses/by/3.0/legalcode. It is
* released to the public domain via http://github.com/AeroCross/ci-ext-model.
*
* You are free to share, modify, and profit from this source file as long as
* there is attribution to the author and this Notice of License is not removed.
*
* @package Codeigniter
* @subpackage Core Extensions
* @author Mario Cuba <mario@mariocuba.net>
* @license http://creativecommons.org/licenses/by/3.0
*/
class EXT_Model extends CI_Model {
// the database group name to load
private $database = 'DATABASE_GROUP';
/**
* The class constructor.
*
* @access public
*/
public function __construct() {
// references the global object
$ci =& get_instance();
// sets the active database to the system database
$this->cdb = $ci->load->database($this->database, TRUE);
}
/**
* Adds a where clause to the object if called.
*
* @param string - the name of the method (not accessible)
* @param array - an array of parameters
* @return object - the database object
* @access public
*/
public function __call($name, $parameter) {
$this->cdb->where($name, $parameter[0]);
return $this;
}
/**
* Selects table data.
*
* @param string - the table fields
* @return object - the database object
* @access public
*/
public function data($fields = '*') {
$this->cdb->select($fields);
return $this;
}
/**
* Fetches query for further processing.
*
* @return object - the database object
* @access public
*/
public function fetch($table = NULL) {
if (!empty($table)) {
return $this->cdb->get($table);
} else {
return $this->cdb->get($this->_table);
}
}
/**
* Finishes query and gets a result.
*
* @return object - the database object
* @access public
*/
public function get($table = NULL) {
if (!empty($table)) {
return $this->cdb->get($table)->row();
} else {
return $this->cdb->get($this->_table)->row();
}
}
/**
* Finishes query and gets all result.
*
* @return object - the database object
* @access public
*/
public function getAll($table = NULL) {
if (!empty($table)) {
return $this->cdb->get($table)->result();
} else {
return $this->cdb->get($this->_table)->result();
}
}
/**
* Orders the query by field.
*
* @param string - the database field to order by
* @param string - the ordering method
* @return object - the database object
* @access public
*/
public function by($field, $order) {
$this->cdb->order_by($field, $order);
return $this;
}
/**
* Limits and offsets the query to given numbers.
*
* @param int - the limit
* @param int - the offset
* @return object - the database object
* @access public
*/
public function limit($limit, $offset = NULL) {
if (empty($offset)) {
$this->cdb->limit($limit);
} else {
$this->cdb->limit($limit, $offset);
}
return $this;
}
/**
* Inserts data.
*
* @param array - an associative array of data to add
* @return object - the database object
* @access public
*/
public function insert($data) {
return $this->cdb->insert($this->_table, $data);
}
/**
* Updates rows.
*
* @param array - an associative array with all the where clauses
* @param array - the data to add
* @return object - the database object
* @access public
*/
public function update($where, $data) {
return $this->cdb->where($where)->update($this->_table, $data);
}
/**
* Deletes rows.
*
* @param array - an associative array with the where clauses
* @return object - the database object
* @access public
*/
public function delete($where) {
return $this->cdb->where($where)->delete($this->_table);
}
/**
* Checks if the the data provided exists in the database.
*
* @param string - the value to compare
* @param string - the database field to compare
* @return bool - TRUE if the data exists, FALSE otherwise
* @access public
*/
public function match($value, $field) {
$this->cdb
->select($field)
->where($field, $value);
$sql = $this->cdb->get($this->_table);
if ($sql->num_rows() > 0) {
return TRUE;
} else {
return FALSE;
}
}
}
/* End of file EXT_Model.php */
/* Location: ./application/core/EXT_Model.php */