forked from Zachdidit/applicant_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractModel.php
160 lines (143 loc) · 4.41 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
<?php
abstract Class AbstractModel{
//Create vars to store contact info and connection info
protected $conn;
protected $contact;
/*
* Constructor
* Loads MySQL database connection
* Instantiates $contact
*/
public function __construct() {
//load the database
$this->conn = new mysqli("localhost", "root","root","Contacts");
//check for connection errors
if($this->conn->connect_errno){
echo $this->conn->connect_error;
}
//create null contact for setting and loading
$this->contact = array(
'id' => NULL,
'name' => NULL,
'email' => NULL
);
}
/*
* Destructor
* Frees result data
* Closes connection from MySQL database
*/
public function __destruct() {
//close database connection at end of program
$this->conn->close();
}
/*
* Saves current contact to database
*/
public function save() {
//Check to see if a database item already exists
if ($this->contact['id'] != NULL) {
$query = "UPDATE " . $this->_table . " SET ";
$query .= "name = '{$this->contact['name']}', ";
$query .= "email = '{$this->contact['email']}' ";
$query .= "WHERE " . $this->_pk . " = " . $this->contact['id'];
}
//if no database item exists
else{
$query = "INSERT INTO " . $this->_table . " (name, email) ";
$query .= "VALUES ('{$this->contact['name']}', '{$this->contact['email']}')";
}
//run respective query
$result = $this->conn->query($query);
//check query worked
if($result && $this->conn->affected_rows == 1){
//echo "SUCCESS!";
$this->contact['id'] = $this->conn->insert_id;
}
else{
echo "database save failed";
}
}
/*
* Loads contact info from database
* @param int $id
* @return AbstractModel
*/
public function load($id){
//create query for load
$query = "SELECT * ";
$query .= "FROM " . $this->_table . " ";
$query .= "WHERE " . $this->_pk . " = " . $id;
$result = $this->conn->query($query);
//Test for query error
if(!$result){
die("database query failed");
}
//assign loaded data to contacts
$row = $result->fetch_row();
$this->contact = array(
'id' => $row[0],
'name' => $row[1],
'email' => $row[2]
);
//return class
return $this;
}
/*
* Deletes current contact
* @param int $id
*/
public function delete($id = NULL) {
//if there is no argument of id
if ($id == NULL) {
$query = "DELETE FROM " . $this->_table . " ";
$query .= "WHERE " . $this->_pk . " = " . $this->contact['id'];
// if there is an id argument
} else {
$query = "DELETE FROM " . $this->_table . " ";
$query .= "WHERE " . $this->_pk . " = " . $id;
}
//execute query
$result = $this->conn->query($query);
//test execution
if ($result && $this->conn->affected_rows == 1) {
//echo "SUCCESS!";
} else {
echo "database delete failed";
}
}
/*
* Gets contact data from stored contact
* @param string $key
*/
public function getData($key=false){
//if no argument of key, return everything
if($key==false){
return $this->contact;
}
//key argument given, thus only return data associated with the key
else{
return $this->contact[$key];
}
}
/*
* Sets Contact Data
* @param (string, array) $arr
* @param string $value
* @return AbstractModel
*/
public function setData($arr, $value=false){
//if input is an array, add each value to contact
if (gettype($arr)== 'array'){
foreach ($arr as $key => $arrValue) {
$this->contact[$key] = $arrValue;
}
}
//if there is only a single value, set that value
else{
$this->contact[$arr] = $value;
}
//return class
return $this;
}
}