-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractModel.php
213 lines (197 loc) · 5.06 KB
/
AbstractModel.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
Class AbstractModel{
private $dbCon;
protected $arrData;
function __construct() {
$host = "localhost";
$password = "test";
$user = "testUser";
$database = "test";
$port = "3306";
if (!$this->dbCon){
try{
$this->dbCon = new PDO("mysql:host=$host;port=$port;dbname=$database", $user, $password);
echo 'Conencted to database';
$this->dbCon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo 'Connection Failure';
echo $e->getMessage();
}
try{
// Setup data array using the db schema
$query = $this->dbCon->query("DESCRIBE $this->_table");
foreach ($query->fetchAll() as $result){
$this->arrData[$result['Field']] = "";
echo ("<br>".$result['Type']);
}
}catch(PDOException $e){
echo 'Unable to obtain DB Schema';
echo $e->getMessage();
}
}
}
function __destruct() {
$this->dbCon = null;
}
// Commit changes
public function save(){
if($this->_pk == "id"){
// build our string for the insert value params and column names
$columnString;
$valueString;
$first = true;
foreach($this->arrData as $key=>$value){
if($first){
$columnString = $key;
$valueString = $key;
$first = false;
}else{
$columnString = $columnString.','.$key;
$valueString = $valueString.',:'.$key;
}
}
$columnString = '('.$columnString.')';
$valueString = '('.$valueString.')';
if(!($columnString || $valueString)){
return "Error during insert string creation";
}
try{
$query = $this->dbCon->prepare("INSERT INTO $this->_table (name, email) VALUES(:name,:email)");
foreach($this->arrData as $key=>$value){
if($key != 'id'){
$query->bindParam(':'.$key, $this->arrData[$key]);
}
}
$query->execute();
$this->_pk = $this->dbCon->lastInsertId();
$this->arrData['id'] = $this->_pk;
}catch(PDOException $e){
echo $e->getMessage();
return $this;
}
}else{
$updateString;
$first = true;
foreach($this->arrData as $key=>$value){
if($key != 'id'){
if($first){
$updateString = $key.'=:'.$key;
$first = false;
}else {
$updateString = $updateString.','.$key.'=:'.$key;
}
}
}
if(!$updateString){
return "Error during update string production";
}
try{
$query = $this->dbCon->prepare("UPDATE $this->_table SET $updateString WHERE ID = :id");
foreach($this->arrData as $key=>$value){
if($key != 'id'){
$query->bindParam(':'.$key, $this->arrData[$key]);
}
}
$query->bindParam(':id', $this->_pk, PDO::PARAM_INT);
$query->execute();
}catch(PDOException $e){
echo $e->getMessage();
return $this;
}
}
}
// Load a new primay key
public function load($id){
try{
$query = $this->dbCon->prepare("SELECT * FROM $this->_table WHERE ID = :id");
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
}catch(PDOException $e){
echo $e->getMessage();
return $this;
}
echo '<br />';
foreach ($this->arrData as $rowName => $value){
// step through data. Use data from the select to match to the shema already pulled.
foreach ($result as $qrowName => $qvalue){
// Step through the query results
if($qrowName == $rowName){
if($qrowName == 'id'){
$this->_pk = $qvalue;
}
$this->arrData[$rowName] = $qvalue;
break;
}
}
}
echo $this->_pk;
return $this;
}
// Delete function
public function delete($id=false){
print_r("\nDeleting");
if($id == false){
$id = $this->_pk;
}
try{
$query = $this->dbCon->prepare("DELETE FROM $this->_table WHERE id = :id");
$query->bindParam(':id', $id, PDO::PARAM_INT);
$query->execute();
}catch(PDOException $e){
echo $e->getMessage();
return $this;
}
return $this;
}
// Select Function also returns column result if key is provided
public function getData($key=false){
if (!$key){
return $this->arrData;
}else{
$result;
foreach ($this->arrData as $fieldName => $value){
if ($fieldName == $key ){
$result = $value;
}
}
if (!$result){
return "Invalid key provided";
}else {
return $result;
}
}
}
// Update function
public function setData($arr, $value=false){
if(is_array($arr)){
foreach ($this->arrData as $rowName => $mvalue){
foreach ($arr as $arowName => $avalue){
if($arowName == $rowName){
if($arowName == 'id'){
$this->_pk = $avalue;
}
$this->arrData[$rowName] = $avalue;
break;
}
}
}
}else{
$keyFound = false;
foreach ($this->arrData as $fieldName => $avalue){
if ($fieldName == $arr){
$keyFound = true;
$this->arrData[$fieldName] = $value;
if ($arr == 'id'){
$this->_pk = $value;
}
}
}
if (!$keyFound){
return "Invalid key provided";
}
}
return $this;
}
}
?>