Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 435 lines (367 sloc) 14.726 kB
c7dbad9 @manavo First major commit. Still needs documentation!
authored
1 <?php
2
3 class MY_Model extends CI_Model {
4
326cfd8 @manavo allow table name to be overwritten by extending classes
authored
5 protected $_table = null;
c7dbad9 @manavo First major commit. Still needs documentation!
authored
6 protected $_primary_key = 'id';
7 protected $_loaded_from_db;
8 protected $_magic_timestamps = false;
9 protected $_magic_timestamp_created = 'created';
10 protected $_magic_timestamp_updated = 'updated';
11
12 protected $_attributes;
13
14 protected $_CI;
15
1f0e579 @manavo Work with one to many and many to many relationships (and updated doc…
authored
16 public static $ONE_TO_MANY = 'otm';
17 public static $MANY_TO_MANY = 'mtm';
18
c7dbad9 @manavo First major commit. Still needs documentation!
authored
19 function __construct() {
20 parent::__construct();
21
326cfd8 @manavo allow table name to be overwritten by extending classes
authored
22 // if no table has been explicitly set (from the overriden class),
23 // we default to the plural of the classname
24 if (!$this->_table) {
25 $this->_table = strtolower($this->_pluralize(get_class($this)));
26 }
c7dbad9 @manavo First major commit. Still needs documentation!
authored
27
28 $this->_CI =& get_instance();
29 $this->reset();
30 }
31
45d05ab @manavo change the access of the pluralize/singularize functions
authored
32 protected function _pluralize($word) {
c7dbad9 @manavo First major commit. Still needs documentation!
authored
33 $plural = array(
34 '/(quiz)$/i' => '$1zes',
35 '/^(ox)$/i' => '$1en',
36 '/([m|l])ouse$/i' => '$1ice',
37 '/(matr|vert|ind)ix|ex$/i' => '$1ices',
38 '/(x|ch|ss|sh)$/i' => '$1es',
39 '/([^aeiouy]|qu)ies$/i' => '$1y',
40 '/([^aeiouy]|qu)y$/i' => '$1ies',
41 '/(hive)$/i' => '$1s',
42 '/(?:([^f])fe|([lr])f)$/i' => '$1$2ves',
43 '/sis$/i' => 'ses',
44 '/([ti])um$/i' => '$1a',
45 '/(buffal|tomat)o$/i' => '$1oes',
46 '/(bu)s$/i' => '$1ses',
47 '/(alias|status)/i'=> '$1es',
48 '/(octop|vir)us$/i'=> '$1i',
49 '/(ax|test)is$/i'=> '$1es',
50 '/s$/i'=> 's',
51 '/$/'=> 's');
52
53 $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
54
55 $irregular = array(
56 'person' => 'people',
57 'man' => 'men',
58 'child' => 'children',
59 'sex' => 'sexes',
60 'move' => 'moves');
61
62 $lowercased_word = strtolower($word);
63
64 foreach ($uncountable as $_uncountable){
65 if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
66 return $word;
67 }
68 }
69
70 foreach ($irregular as $_plural=> $_singular){
71 if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
72 return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);
73 }
74 }
75
76 foreach ($plural as $rule => $replacement) {
77 if (preg_match($rule, $word)) {
78 return preg_replace($rule, $replacement, $word);
79 }
80 }
81 return false;
82 }
83
45d05ab @manavo change the access of the pluralize/singularize functions
authored
84 protected function _singularize($word) {
c7dbad9 @manavo First major commit. Still needs documentation!
authored
85 $singular = array (
86 '/(quiz)zes$/i' => '\1',
87 '/(matr)ices$/i' => '\1ix',
88 '/(vert|ind)ices$/i' => '\1ex',
89 '/^(ox)en/i' => '\1',
90 '/(alias|status)es$/i' => '\1',
91 '/([octop|vir])i$/i' => '\1us',
92 '/(cris|ax|test)es$/i' => '\1is',
93 '/(shoe)s$/i' => '\1',
94 '/(o)es$/i' => '\1',
95 '/(bus)es$/i' => '\1',
96 '/([m|l])ice$/i' => '\1ouse',
97 '/(x|ch|ss|sh)es$/i' => '\1',
98 '/(m)ovies$/i' => '\1ovie',
99 '/(s)eries$/i' => '\1eries',
100 '/([^aeiouy]|qu)ies$/i' => '\1y',
101 '/([lr])ves$/i' => '\1f',
102 '/(tive)s$/i' => '\1',
103 '/(hive)s$/i' => '\1',
104 '/([^f])ves$/i' => '\1fe',
105 '/(^analy)ses$/i' => '\1sis',
106 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
107 '/([ti])a$/i' => '\1um',
108 '/(n)ews$/i' => '\1ews',
109 '/s$/i' => '',
110 );
111
112 $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
113
114 $irregular = array(
115 'person' => 'people',
116 'man' => 'men',
117 'child' => 'children',
118 'sex' => 'sexes',
119 'move' => 'moves');
120
121 $lowercased_word = strtolower($word);
122 foreach ($uncountable as $_uncountable){
123 if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
124 return $word;
125 }
126 }
127
128 foreach ($irregular as $_plural=> $_singular){
129 if (preg_match('/('.$_singular.')$/i', $word, $arr)) {
130 return preg_replace('/('.$_singular.')$/i', substr($arr[0],0,1).substr($_plural,1), $word);
131 }
132 }
133
134 foreach ($singular as $rule => $replacement) {
135 if (preg_match($rule, $word)) {
136 return preg_replace($rule, $replacement, $word);
137 }
138 }
139
140 return $word;
141 }
142
554a2fd @manavo optionally read variables that have been posted and save their values…
authored
143 public function create($fields = null) {
c7dbad9 @manavo First major commit. Still needs documentation!
authored
144 $this->reset();
554a2fd @manavo optionally read variables that have been posted and save their values…
authored
145
146 // for each of the fields we want to read it, read it from the post data
147 // and set the value of the attribute on this object
148 if (is_array($fields)) {
149 foreach ($fields as $field) {
150 $this->$field = $this->input->post($field);
151 }
152 }
c7dbad9 @manavo First major commit. Still needs documentation!
authored
153 }
154
155 public function reset() {
156 $this->_attributes = array();
157 $this->_loaded_from_db = false;
158 }
159
160 function __get($key) {
161 if (isset($this->_attributes[$key])) {
162 return $this->_attributes[$key];
163 } else {
164 return parent::__get($key);
165 }
166 }
167
168 function __set($key, $value) {
169 if (substr($key, 0, 1) != '_') {
170 $this->_attributes[$key] = $value;
171 } else {
172 $this->$key = $value;
173 }
174 }
175
176 function __call($name, $arguments) {
177 if (substr($name, 0, 8) == 'load_by_') {
178 $field = substr($name, 8);
179 return $this->_load_by_something($field, $arguments[0]);
180 } else if (substr($name, 0, 4) == 'add_') {
181 $other_model = strtolower(substr($name, 4));
182
183 if (is_array($arguments) && count($arguments) > 0) {
184 $other_id = array_shift($arguments);
185 return $this->_add_relationship($other_model, $other_id, $arguments);
186 } else {
187 return false;
188 }
189 } else if (substr($name, 0, 7) == 'update_') {
190 $other_model = strtolower(substr($name, 7));
191
192 if (is_array($arguments) && count($arguments) > 0) {
193 $other_id = array_shift($arguments);
194 return $this->_update_relationship($other_model, $other_id, $arguments);
195 } else {
196 return false;
197 }
198 } else if (substr($name, 0, 7) == 'remove_') {
199 $other_model = strtolower(substr($name, 7));
200 if (is_array($arguments) && count($arguments) > 0) {
201 $other_id = array_shift($arguments);
202 return $this->_remove_relationship($other_model, $other_id);
203 } else {
204 return false;
205 }
206 } else if (substr($name, 0, 4) == 'get_') {
207 $other_table = strtolower(substr($name, 4));
1f0e579 @manavo Work with one to many and many to many relationships (and updated doc…
authored
208 $relationship_type = self::$ONE_TO_MANY;
209 if (count($arguments) > 0) {
210 $relationship_type = array_shift($arguments);
211 }
0db0459 @manavo when getting a relationship, allow to specify extra SQL conditions wh…
authored
212 if (count($arguments) > 0) {
213 $where = array_shift($arguments);
214 }
215 return $this->_get_relationship($other_table, $relationship_type, $where);
c7dbad9 @manavo First major commit. Still needs documentation!
authored
216 } else if (substr($name, 0, 4) == 'has_') {
217 $other_model = strtolower(substr($name, 4));
218 if (is_array($arguments) && count($arguments) > 0) {
219 $other_id = array_shift($arguments);
220 return $this->_has_relationship($other_model, $other_id);
221 } else {
222 return false;
223 }
224 }
225 }
226
227 private function _get_relationship_table($other_table) {
228 $tables = array($other_table, $this->_table);
229 sort($tables);
230
231 return implode('_', $tables);
232 }
233
234 private function _add_relationship($other_model, $other_id, $params = array()) {
235 $_magic_timestamps = false;
236
237 $other_table = strtolower($this->_pluralize($other_model));
238 $relationship_table = $this->_get_relationship_table($other_table);
239
240 if (count($params) > 0) {
241 // see if extra args are given in
242 $attributes = array_shift($params);
243 if (!is_array($attributes)) {
244 $attributes = array();
245 }
246
247 if (count($params) > 0) {
248 $_magic_timestamps = $params[0];
249 if (is_bool($_magic_timestamps) == false) {
250 $_magic_timestamps = false;
251 }
252 }
253 } else {
254 $attributes = array();
255 }
256
257 $attributes[strtolower(get_class($this)).'_id'] = $this->{$this->_primary_key};
258 $attributes[$other_model.'_id'] = $other_id;
259
260 if ($_magic_timestamps) {
261 $time = time();
262 $attributes[$this->_magic_timestamp_created] = $time;
263 $attributes[$this->_magic_timestamp_updated] = $time;
264 }
265
266 $this->_CI->db->insert($relationship_table, $attributes);
267 if ($this->_CI->db->affected_rows() == 1) {
268 return true;
269 } else {
270 return false;
271 }
272 }
273
274 private function _update_relationship($other_model, $other_id, $params = array()) {
275 $_magic_timestamps = false;
276
277 $other_table = strtolower($this->_pluralize($other_model));
278 $relationship_table = $this->_get_relationship_table($other_table);
279
280 if (count($params) > 0) {
281 // see if extra args are given in
282 $attributes = array_shift($params);
283 if (!is_array($attributes)) {
284 $attributes = array();
285 }
286
287 if (count($params) > 0) {
288 $_magic_timestamps = $params[0];
289 if (is_bool($_magic_timestamps) == false) {
290 $_magic_timestamps = false;
291 }
292 }
293 } else {
294 $attributes = array();
295 }
296
297 if ($_magic_timestamps) {
298 $time = time();
299 $attributes[$this->_magic_timestamp_updated] = $time;
300 }
301
302 $where = array(
303 strtolower(get_class($this)).'_id' => $this->{$this->_primary_key},
304 $other_model.'_id' => $other_id
305 );
306
307 $this->_CI->db->update($relationship_table, $attributes, $where);
308 if ($this->_CI->db->affected_rows() == 1) {
309 return true;
310 } else {
311 return false;
312 }
313 }
314
315 private function _has_relationship($other_model, $other_id) {
316 $other_table = strtolower($this->_pluralize($other_model));
317 $relationship_table = $this->_get_relationship_table($other_table);
318 $query = $this->_CI->db->get_where($relationship_table, array(strtolower(get_class($this)).'_id' => $this->{$this->_primary_key}, $other_model.'_id' => $other_id), 1, 0);
319 if ($query->num_rows() == 1) {
85016e0 @manavo when checking a relationship, instead of returning TRUE/FALSE, we ret…
authored
320 return $query->row();
c7dbad9 @manavo First major commit. Still needs documentation!
authored
321 } else {
322 return false;
323 }
324 }
325
0db0459 @manavo when getting a relationship, allow to specify extra SQL conditions wh…
authored
326 private function _get_relationship($other_table, $relationship_type, $where) {
1f0e579 @manavo Work with one to many and many to many relationships (and updated doc…
authored
327 if ($relationship_type == self::$MANY_TO_MANY) {
328 $relationship_table = $this->_get_relationship_table($other_table);
329 $query_table = $relationship_table;
330 } else {
331 $query_table = $other_table;
332 }
0db0459 @manavo when getting a relationship, allow to specify extra SQL conditions wh…
authored
333
334 $where = array_merge(array(strtolower(get_class($this)).'_id' => $this->{$this->_primary_key}), $where);
335
336 $query = $this->_CI->db->get_where($query_table, $where);
c7dbad9 @manavo First major commit. Still needs documentation!
authored
337 return $query->result();
338 }
339
340 private function _remove_relationship($other_model, $other_id) {
341 $other_table = strtolower($this->_pluralize($other_model));
342 $relationship_table = $this->_get_relationship_table($other_table);
343
721d139 @manavo fix bug which would crash CI when removing a relationship
authored
344 $this->_CI->db->delete($relationship_table, array(strtolower(get_class($this)).'_id' => $this->{$this->_primary_key}, $other_model.'_id' => $other_id));
345 if ($this->_CI->db->affected_rows() == 1) {
346 return true;
347 } else {
348 return false;
349 }
c7dbad9 @manavo First major commit. Still needs documentation!
authored
350 }
351
352 public function load($param = null) {
353 if (is_array($param) || is_object($param)) {
8068336 @manavo fix bug of model not being correctly flagged as loaded when loading f…
authored
354 $this->_loaded_from_db = true;
355
c7dbad9 @manavo First major commit. Still needs documentation!
authored
356 foreach ($param as $column => $value) {
357 $this->_attributes[$column] = $value;
358 }
359
360 return true;
361 } else {
362 return $this->_load_by_something($this->_primary_key, $param);
363 }
364 }
365
366 public function _load_by_something($field, $val) {
367 $this->reset();
368 if ($val) {
369 $query = $this->_CI->db->get_where($this->_table, array($field => $val), 1, 0);
370 if ($query->num_rows() == 1) {
371 $this->_loaded_from_db = true;
372
373 $row = $query->row();
374 foreach ($row as $column => $value) {
375 $this->_attributes[$column] = $value;
376 }
377
378 return true;
379 }
380 }
381
382 return false;
383 }
384
43d3912 @manavo a getAll() call, to just return all rows of a the specific table (wit…
authored
385 function getAll($where = null) {
386 if ($where) {
387 $this->_CI->db->where($where);
388 }
389 $query = $this->_CI->db->get($this->_table);
390 return $query->result();
391 }
392
c7dbad9 @manavo First major commit. Still needs documentation!
authored
393 function save() {
394 if ($this->_magic_timestamps == true) {
395 $this->{$this->_magic_timestamp_updated} = time();
396 }
397
398 if ($this->_loaded_from_db == true) {
399 //update
400 $this->_CI->db->where($this->_primary_key, $this->{$this->_primary_key});
401 $this->_CI->db->update($this->_table, $this->_attributes);
402 } else {
403 //create
404 if ($this->_magic_timestamps == true) {
405 $this->{$this->_magic_timestamp_created} = time();
406 }
407
408 $this->_CI->db->insert($this->_table, $this->_attributes);
409
410 if (!array_key_exists($this->_primary_key, $this->_attributes) || !$this->_attributes[$this->_primary_key]) {
411 $new_id = $this->_CI->db->insert_id();
412 $this->{$this->_primary_key} = $new_id;
413 }
414 $this->_loaded_from_db = true;
415 }
416
417 if ($this->_CI->db->affected_rows() == 1) {
418 return true;
419 } else {
420 return false;
421 }
422 }
423
424 public function delete() {
425 $this->_CI->db->delete($this->_table, array($this->_primary_key => $this->{$this->_primary_key}));
426 if ($this->_CI->db->affected_rows() == 1) {
427 return true;
428 } else {
429 return false;
430 }
431 }
432
433 }
434
435 ?>
Something went wrong with that request. Please try again.