-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy patheasyCRUD.class.php
187 lines (148 loc) · 4.71 KB
/
easyCRUD.class.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
<?php
/**
* Easy Crud - This class kinda works like ORM. Just created for fun :)
*
* @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal)
* @version 0.1a
*/
require_once(__DIR__ . '/../Db.class.php');
class Crud {
private $db;
public $variables;
public function __construct($data = array()) {
$this->db = new DB();
$this->variables = $data;
}
public function __set($name,$value){
if(strtolower($name) === $this->pk) {
$this->variables[$this->pk] = $value;
}
else {
$this->variables[$name] = $value;
}
}
public function __get($name)
{
if(is_array($this->variables)) {
if(array_key_exists($name,$this->variables)) {
return $this->variables[$name];
}
}
return null;
}
public function save($id = "0") {
$this->variables[$this->pk] = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk];
$fieldsvals = '';
$columns = array_keys($this->variables);
foreach($columns as $column)
{
if($column !== $this->pk)
$fieldsvals .= $column . " = :". $column . ",";
}
$fieldsvals = substr_replace($fieldsvals , '', -1);
if(count($columns) > 1 ) {
$sql = "UPDATE " . $this->table . " SET " . $fieldsvals . " WHERE " . $this->pk . "= :" . $this->pk;
if($id === "0" && $this->variables[$this->pk] === "0") {
unset($this->variables[$this->pk]);
$sql = "UPDATE " . $this->table . " SET " . $fieldsvals;
}
return $this->exec($sql);
}
return null;
}
public function create() {
$bindings = $this->variables;
if(!empty($bindings)) {
$fields = array_keys($bindings);
$fieldsvals = array(implode(",",$fields),":" . implode(",:",$fields));
$sql = "INSERT INTO ".$this->table." (".$fieldsvals[0].") VALUES (".$fieldsvals[1].")";
}
else {
$sql = "INSERT INTO ".$this->table." () VALUES ()";
}
return $this->exec($sql);
}
public function delete($id = "") {
$id = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk];
if(!empty($id)) {
$sql = "DELETE FROM " . $this->table . " WHERE " . $this->pk . "= :" . $this->pk. " LIMIT 1" ;
}
return $this->exec($sql, array($this->pk=>$id));
}
public function find($id = "") {
$id = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk];
if(!empty($id)) {
$sql = "SELECT * FROM " . $this->table ." WHERE " . $this->pk . "= :" . $this->pk . " LIMIT 1";
$result = $this->db->row($sql, array($this->pk=>$id));
$this->variables = ($result != false) ? $result : null;
}
}
/**
* @param array $fields.
* @param array $sort.
* @return array of Collection.
* Example: $user = new User;
* $found_user_array = $user->search(array('sex' => 'Male', 'age' => '18'), array('dob' => 'DESC'));
* // Will produce: SELECT * FROM {$this->table_name} WHERE sex = :sex AND age = :age ORDER BY dob DESC;
* // And rest is binding those params with the Query. Which will return an array.
* // Now we can use for each on $found_user_array.
* Other functionalities ex: Support for LIKE, >, <, >=, <= ... Are not yet supported.
*/
public function search($fields = array(), $sort = array()) {
$bindings = empty($fields) ? $this->variables : $fields;
$sql = "SELECT * FROM " . $this->table;
if (!empty($bindings)) {
$fieldsvals = array();
$columns = array_keys($bindings);
foreach($columns as $column) {
$fieldsvals [] = $column . " = :". $column;
}
$sql .= " WHERE " . implode(" AND ", $fieldsvals);
}
if (!empty($sort)) {
$sortvals = array();
foreach ($sort as $key => $value) {
$sortvals[] = $key . " " . $value;
}
$sql .= " ORDER BY " . implode(", ", $sortvals);
}
return $this->exec($sql, $bindings);
}
public function all(){
return $this->db->query("SELECT * FROM " . $this->table);
}
public function min($field) {
if($field)
return $this->db->single("SELECT min(" . $field . ")" . " FROM " . $this->table);
}
public function max($field) {
if($field)
return $this->db->single("SELECT max(" . $field . ")" . " FROM " . $this->table);
}
public function avg($field) {
if($field)
return $this->db->single("SELECT avg(" . $field . ")" . " FROM " . $this->table);
}
public function sum($field) {
if($field)
return $this->db->single("SELECT sum(" . $field . ")" . " FROM " . $this->table);
}
public function count($field) {
if($field)
return $this->db->single("SELECT count(" . $field . ")" . " FROM " . $this->table);
}
private function exec($sql, $array = null) {
if($array !== null) {
// Get result with the DB object
$result = $this->db->query($sql, $array);
}
else {
// Get result with the DB object
$result = $this->db->query($sql, $this->variables);
}
// Empty bindings
$this->variables = array();
return $result;
}
}
?>