-
Notifications
You must be signed in to change notification settings - Fork 65
/
DatabaseObject.php
310 lines (271 loc) · 10.4 KB
/
DatabaseObject.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/*
**************************************************************************************************************************
** CORAL Licensing Module v. 1.0
**
** Copyright (c) 2010 University of Notre Dame
**
** This file is part of CORAL.
**
** CORAL is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
**
** CORAL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along with CORAL. If not, see <http://www.gnu.org/licenses/>.
**
**************************************************************************************************************************
*/
class DatabaseObject extends DynamicObject {
protected $db;
protected $tableName;
protected $collectiveName;
protected $primaryKeyName;
protected $primaryKey;
public $attributeNames = array();
protected $attributes = array();
protected $childNames = array();
protected $children = array();
protected $parentNames = array();
protected $parents = array();
protected $peerNames = array();
protected $peers = array();
protected function init(NamedArguments $arguments) {
$arguments->setDefaultValueForArgumentName('tableName', get_class($this));
$this->tableName = $arguments->tableName;
$defaultCollectiveName = lcfirst($arguments->tableName) . 's';
$arguments->setDefaultValueForArgumentName('collectiveName', $defaultCollectiveName);
$this->collectiveName = $arguments->collectiveName;
$defaultPrimaryKeyName = lcfirst($arguments->tableName) . 'ID';
$arguments->setDefaultValueForArgumentName('primaryKeyName', $defaultPrimaryKeyName);
$this->primaryKeyName = $arguments->primaryKeyName;
$this->primaryKey = $arguments->primaryKey;
$this->db = DBService::getInstance();
$this->defineRelationships();
//$this->defineAttributes();
$this->overridePrimaryKeyName();
$this->load();
}
protected function defineRelationships() {}
protected function overridePrimaryKeyName() {}
protected function asArray() {
$aarray = array();
foreach (array_keys($this->attributeNames) as $attributeName) {
if ($this->$attributeName != null) {
$aarray[$attributeName] = $this->$attributeName;
}
}
return $aarray;
}
protected function defineAttributes() {
// Figure out attributes from existing database
$query = "SELECT COLUMN_NAME FROM information_schema.`COLUMNS` WHERE table_schema = '";
$query .= $this->db->config->database->name . "' AND table_name = '$this->tableName'";// MySQL-specific
foreach ($this->db->processQuery($query) as $result) {
$attributeName = $result[0];
if ($attributeName != $this->primaryKeyName) {
$this->addAttribute($attributeName);
}
}
}
protected function addAttribute($attributeName, $attributeType = NULL) {
$this->attributeNames[$attributeName] = $attributeType;
}
protected function hasMany($relatedClassName) {
$exampleRelatedObject = new $relatedClassName;
$nameArrayName = 'childNames';
// Check for many-to-many relationship.
if (array_key_exists($this->collectiveName, $exampleRelatedObject->childNames)) {
$nameArrayName = 'peerNames';
}
$this->$nameArrayName[$exampleRelatedObject->collectiveName] = $relatedClassName;
}
protected function hasOne($parentClassName, $parentName = NULL) {
if (!isset($parentName)) {
$parentName = lcfirst($parentClassName);
}
$this->parentNames[$parentName] = $parentClassName;
}
public function valueForKey($key) {
if (array_key_exists($key, $this->attributeNames)) {
if (!array_key_exists($key, $this->attributes)) {
$query = "SELECT `$key` FROM `$this->tableName` WHERE `$this->primaryKeyName` = '$this->primaryKey' LIMIT 1";
$result = $this->db->processQuery($query);
if (isset($result[0])) $this->attributes[$key] = stripslashes($result[0]);
}
return $this->attributes[$key];
} else if (array_key_exists($key, $this->parentNames)) {
if (!array_key_exists($key, $this->parents)) {
$parentClassName = $this->parentNames[$key];
$exampleParent = new $parentClassName;
$parentPrimaryKey = $this->valueForKey($exampleParent->primaryKeyName);
$this->parents[$key] = new $parentClassName(new NamedArguments(array('primaryKey' => $parentPrimaryKey)));
}
return $this->parents[$key];
} else if (array_key_exists($key, $this->childNames)) {
if (!array_key_exists($key, $this->children)) {
$this->children[$key] = array();
$childClassName = $this->childNames[$key];
$exampleChild = new $childClassName;
$whereClause = "`$this->primaryKeyName`='$this->primaryKey'";
$query = "SELECT `$exampleChild->primaryKeyName` FROM `$exampleChild->tableName` WHERE $whereClause";
$results = $this->db->processQuery($query);
foreach ($results as $result) {
$id = $result[0];
$child = new $childClassName(new NamedArguments(array('primaryKey' => $id)));
array_push($this->children[$key], $child);
}
}
return $this->children[$key];
} else if (array_key_exists($key, $this->peerNames)) {
if (!array_key_exists($key, $this->peers)) {
$this->peers[$key] = array();
$peerClassName = $this->peerNames[$key];
$examplePeer = new $peerClassName;
$whereClause = "`$this->primaryKeyName`='$this->primaryKey'";
$tableNames = array($this->tableName, $examplePeer->tableName);
$sortedTableNames = sort($tableNames);
$joinTableName = $sortedTableNames[0] . 'To' . $sortedTableNames[1];
$query = "SELECT `$examplePeer->primaryKeyName` FROM `$joinTableName` WHERE $whereClause";
$results = $this->db->processQuery($query);
foreach ($results as $result) {
$id = $result[0];
$peer = new $peerClassName(new NamedArguments(array('primaryKey' => $id)));
array_push($this->peers[$key], $peer);
}
}
return $this->peers[$key];
} else {
return parent::valueForKey($key);
}
}
public function setValueForKey($key, $value) {
if (array_key_exists($key, $this->parentNames)) {
if (is_a($value, 'DatabaseObject')) {
$key = $value->primaryKeyName;
$value = $value->primaryKey;
}
}
if (array_key_exists($key, $this->attributeNames)) {
$this->attributes[$key] = $value;
} else if (array_key_exists($key, $this->childNames) && is_array($value)) {
if (!array_key_exists($key, $this->children)) {
$this->children[$key] = array();
}
//Add new children
foreach ($value as $child) {
if (is_a($child, $this->childNames[$key]) && is_a($child, 'DatabaseObject')) {
if (!array_key_exists($child->primaryKey, $this->children[$key])) {
$this->children[$key][$child->primaryKey] = $child;
}
}
}
//Remove old children
foreach (array_keys($this->children[$key]) as $childPrimaryKey) {
if (!array_key_exists($childPrimaryKey, $value)) {
unset($this->children[$key][$childPrimaryKey]);
}
}
} else if (array_key_exists($key, $this->peerNames) && is_array($value)) {
if (!array_key_exists($key, $this->peers)) {
$this->peers[$key] = array();
}
//Add new peers
foreach ($value as $peer) {
if (is_a($peer, $this->peerNames[$key]) && is_a($peer, 'DatabaseObject')) {
if (!array_key_exists($peer->primaryKey, $this->peers[$key])) {
$this->peers[$key][$peer->primaryKey] = $peer;
}
}
}
//Remove old peers
foreach (array_keys($this->peers[$key]) as $peerPrimaryKey) {
if (!array_key_exists($peerPrimaryKey, $value)) {
unset($this->peers[$key][$peerPrimaryKey]);
}
}
} else {
parent::setValueForKey($key, $value);
}
}
public function delete() {
$query = "DELETE FROM `$this->tableName` WHERE `$this->primaryKeyName` = '$this->primaryKey'";
return $this->db->processQuery($query);
}
public function save() {
$pairs = array();
foreach (array_keys($this->attributeNames) as $attributeName) {
$value = $this->attributes[$attributeName];
if ($value == '' || !isset($value)) {
$value = "NULL";
} else {
$value = $this->db->escapeString($value);
$value = "'$value'";
}
$pair = "`$attributeName`=$value";
array_push($pairs, $pair);
}
$set = implode(', ', $pairs);
if (isset($this->primaryKey)) {
// Update object
$query = "UPDATE `$this->tableName` SET $set WHERE `$this->primaryKeyName` = '$this->primaryKey'";
$this->db->processQuery($query);
} else {
// Insert object
$query = "INSERT INTO `$this->tableName` SET $set";
$this->primaryKey = $this->db->processQuery($query);
}
}
public function all() {
$query = "SELECT * FROM `$this->tableName` ORDER BY 2, 1";
$result = $this->db->processQuery($query);
$objects = array();
foreach ($result as $row) {
$className = get_class($this);
$object = new $className(new NamedArguments(array('primaryKey' => $row[0])));
array_push($objects, $object);
}
return $objects;
}
public function allAsArray() {
$query = "SELECT * FROM `$this->tableName` ORDER BY 2, 1";
$result = $this->db->processQuery($query, 'assoc');
$resultArray = array();
$rowArray = array();
if (isset($result[lcfirst($this->tableName) . 'ID'])){
foreach (array_keys($result) as $attributeName) {
$rowArray[$attributeName] = $result[$attributeName];
}
array_push($resultArray, $rowArray);
}else{
foreach ($result as $row) {
foreach (array_keys($this->attributeNames) as $attributeName) {
$rowArray[$attributeName] = $row[$attributeName];
}
array_push($resultArray, $rowArray);
}
}
return $resultArray;
}
public function load() {
//if exists in the database
if (isset($this->primaryKey)) {
$query = "SELECT * FROM `$this->tableName` WHERE `$this->primaryKeyName` = ?";
$result = $this->db->processPreparedQuery($query, "assoc",
"s",
$this->primaryKey);
foreach (array_keys($result) as $attributeName) {
$this->addAttribute($attributeName);
$this->attributes[$attributeName] = $result[$attributeName];
}
}else{
// Figure out attributes from existing database
$query = "SELECT COLUMN_NAME FROM information_schema.`COLUMNS` WHERE table_schema = '";
$query .= $this->db->config->database->name . "' AND table_name = '$this->tableName'";// MySQL-specific
foreach ($this->db->processQuery($query) as $result) {
$attributeName = $result[0];
$this->addAttribute($attributeName);
}
}
}
}
?>