-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMagic.php
More file actions
83 lines (66 loc) · 1.88 KB
/
Magic.php
File metadata and controls
83 lines (66 loc) · 1.88 KB
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
<?php
class Magic {
public $from;
protected $fields = "All";
protected $count = "Count";
public $where;
private $_ci;
public function __construct() {
$this->_ci = &get_instance();
$this->_ci->load->helper('inflector');
}
/**
* @set model name
* @param $name
* @return $this
*/
public function __get($name) {
$this->from = $name;
return $this;
}
/**
* @param $method
* @param $arguments
* @return bool
*/
public function __call($method, $arguments) {
return $this->_handleCalls($method, $arguments);
}
/**
* @param $filed
* @param $arguments
* @return bool
*/
private function _handleCalls($filed, $arguments) {
$method = substr($filed,4);
switch ($method) {
case $this->fields:
if(count($arguments) > 0)
$this->where = ($arguments[0]);
break;
case $this->count:
$this->_ci->db->select("count(*) as total");
if(count($arguments) > 0)
$this->where = ($arguments[0]);
break;
case (strpos($filed,'By') > 0) :
$filed = substr($method,(strpos(substr($method,4),'By')+2));
$this->where = array(from_camel_case($filed) => $arguments[0]);
break;
default:
trigger_error('Call to undefined method '.__CLASS__.'::'.$method.'()', E_USER_ERROR);
}
return $this->_setConditions();
}
/**
* @return bool
*/
private function _setConditions() {
$this->_ci->db->from(from_camel_case(plural($this->from)));
if($this->where) {
$this->_ci->db->where($this->where);
}
$res = $this->_ci->db->get();
return $res->result_array();
}
}