-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBObject.php
More file actions
147 lines (132 loc) · 4.73 KB
/
DBObject.php
File metadata and controls
147 lines (132 loc) · 4.73 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
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
<?php
abstract class DBObject {
abstract static function getColumns();
abstract static function getTableName();
protected $db, $values, $inDB, $syncedToDB;
function __construct($record=false,$fromDB=false) {
$this->db = DBConnectionFactory::Instance();
if ($record) {
$this->setValues($record, $fromDB);
}
}
static function getObjectById($id,$class) {
$db = DBConnectionFactory::Instance();
$empty_class = new $class();
$values = [$empty_class->getPrimaryKey() => $id];
$records = $db->select($empty_class->getTableName(),$values);
$obj = false;
if (count($records)>=1) {
$obj = new $class($records[0],true);
}
return $obj;
}
protected function getColumnNames() {
$names = [];
foreach ($this->getColumns() as $column) {
$names[] = $column['name'];
}
return $names;
}
protected function getPrimaryKey()
{
foreach ($this->getColumns() as $column) {
if ($column['primary_key']) {
return $column['name'];
}
}
return false;
}
public function setValues($values,$fromDB) {
$this->values = [];
foreach($this->getColumnNames() as $column) {
if (isset($values[$column])) {
$this->values[$column] = $values[$column];
} else {
$this->values[$column] = null;
}
}
$this->inDB = $fromDB;
$this->syncedToDB = $fromDB;
}
private function check_column_names($values) {
foreach($values as $key=>$val) {
if (!in_array($key,$this->getColumnNames())) {
return false;
}
}
return true;
}
public function getValues($expandForeignRecords=false) {
if ($expandForeignRecords) {
foreach ($this->getColumns() as $column) {
if (array_key_exists('foreign_key',$column) && !array_key_exists($column['name'].'_values',$this->values)) {
$foreign_obj = $column['foreign_table']::getObjectById($this->getValue($column['name']),$column['foreign_table']);
// This will break with circular references!!!
if ($foreign_obj) {
$this->values[$column['name'] . '_values'] = $foreign_obj->getValues(true);
}
}
}
}
return $this->values;
}
public function getValue($column) {
if (isset($this->values[$column])) {
return $this->values[$column];
} else if (in_array($column,$this->getColumnNames())) {
return null;
} else {
return false;
}
}
public function setValue($column,$value,$syncToDB=false) {
if (in_array($column,$this->getColumnNames())) {
$this->values[$column] = $value;
$this->syncedToDB = false;
}
if ($syncToDB) {
$this->updateDB();
}
}
public function saveToDB($skipUpdate=false) {
$primarykey = $this->getPrimaryKey();
if ($this->inDB) {
if (!$this->syncedToDB) {
return $this->updateDB();
}
} else if ($this->getValue($primarykey)==null) {
//error_log('Inserting into DB: '.json_encode($this->getValues()));
return $this->insertIntoDB($skipUpdate);
}
}
protected function insertIntoDB($skipUpdate=false) {
if ($this->check_column_names($this->values)) {
$this->db->insert($this->getTableName(),$this->values);
$last_id = $this->db->lastInsertId();
$primary_key = $this->getPrimaryKey();
$this->setValue($primary_key,$last_id);
if (!$skipUpdate===true) {
$records = $this->db->select($this->getTableName(),[$primary_key => $last_id]);
if (count($records)==1) {
$this->setValues($records[0],true);
return true;
} else {
error_log('Object not inserted into DB! LastID: '.$last_id);
}
}
return true;
} else {
error_log('Column names not set correctly in DBObject');
}
return false;
}
protected function updateDB() {
$primarykey = $this->getPrimaryKey();
if ($this->check_column_names($this->values) && $this->getValue($primarykey)) {
$primary = ['key'=>$primarykey, 'value'=>$this->getValue($primarykey)];
$this->db->update($this->getTableName(),$primary,$this->values);
$this->syncedToDB = true;
}
return $this->syncedToDB;
}
}