1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Database; |
4
|
|
|
|
5
|
|
|
use \Anax\Database\DatabaseQueryBuilder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* An implementation of the Active Record pattern to be used as |
9
|
|
|
* base class for database driven models. |
10
|
|
|
*/ |
11
|
|
|
class ActiveRecordModel |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string TABLE_NAME name of the database table. |
15
|
|
|
*/ |
16
|
|
|
protected $tableName = null; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor. |
22
|
|
|
* |
23
|
|
|
* @param DatabaseQueryBuilder $db as database access object. |
24
|
|
|
*/ |
25
|
|
|
public function __construct(DatabaseQueryBuilder $db) |
26
|
|
|
{ |
27
|
|
|
$this->db = $db; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get essential object properties. |
34
|
|
|
* |
35
|
|
|
* @return array with object properties. |
36
|
|
|
*/ |
37
|
|
|
private function getProperties() |
38
|
|
|
{ |
39
|
|
|
$properties = get_object_vars($this); |
40
|
|
|
unset($properties['tableName']); |
41
|
|
|
unset($properties['db']); |
42
|
|
|
unset($properties['di']); |
43
|
|
|
return $properties; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set object properties. |
50
|
|
|
* |
51
|
|
|
* @param array $properties with properties to set. |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
// private function setProperties($properties) |
56
|
|
|
// { |
57
|
|
|
// if (!empty($properties)) { |
58
|
|
|
// foreach ($properties as $key => $val) { |
59
|
|
|
// $this->$key = $val; |
60
|
|
|
// } |
61
|
|
|
// } |
62
|
|
|
// } |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Find and return first object found by search criteria and use |
68
|
|
|
* its data to populate this instance. |
69
|
|
|
* |
70
|
|
|
* @param string $column to use in where statement. |
71
|
|
|
* @param mixed $value to use in where statement. |
72
|
|
|
* |
73
|
|
|
* @return this |
74
|
|
|
*/ |
75
|
|
|
public function find($column, $value) |
76
|
|
|
{ |
77
|
|
|
return $this->db->connect() |
78
|
|
|
->select() |
79
|
|
|
->from($this->tableName) |
80
|
|
|
->where("$column = ?") |
81
|
|
|
->execute([$value]) |
82
|
|
|
->fetchInto($this); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Find and return all. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function findAll() |
93
|
|
|
{ |
94
|
|
|
$db = $this->db; |
95
|
|
|
return $db->connect() |
96
|
|
|
->select() |
97
|
|
|
->from($this->tableName) |
98
|
|
|
->execute() |
99
|
|
|
->fetchAllClass(__CLASS__); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Save current object/row, insert if id is missing and do an |
106
|
|
|
* update if the id exists. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function save() |
111
|
|
|
{ |
112
|
|
|
if (isset($this->id)) { |
113
|
|
|
return $this->update(); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->create(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Create new row. |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
private function create() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$db = $this->db; |
129
|
|
|
$properties = $this->getProperties(); |
130
|
|
|
unset($properties['id']); |
131
|
|
|
$columns = array_keys($properties); |
132
|
|
|
$values = array_values($properties); |
133
|
|
|
|
134
|
|
|
$db->connect() |
135
|
|
|
->insert($this->tableName, $columns) |
136
|
|
|
->execute($values); |
137
|
|
|
|
138
|
|
|
$this->id = $this->db->lastInsertId(); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Update row. |
145
|
|
|
* |
146
|
|
|
* @param array $values key/values to save. |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
private function update($values) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$db = $this->db; |
153
|
|
|
$properties = $this->getProperties(); |
154
|
|
|
unset($properties['id']); |
155
|
|
|
$columns = array_keys($properties); |
156
|
|
|
$values = array_values($properties); |
157
|
|
|
$values[] = $this->id; |
158
|
|
|
|
159
|
|
|
$db->connect() |
160
|
|
|
->update($this->tableName, $columns) |
161
|
|
|
->where("id = ?") |
162
|
|
|
->execute($values); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Delete row. |
169
|
|
|
* |
170
|
|
|
* @param integer $id to delete or use $this->id as default. |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function delete($id = null) |
175
|
|
|
{ |
176
|
|
|
$db = $this->db; |
177
|
|
|
$id = $id ?: $this->id; |
178
|
|
|
|
179
|
|
|
$db->connect() |
180
|
|
|
->deleteFrom(self::CLASS_NAME) |
181
|
|
|
->where("id = ?") |
182
|
|
|
->execute([$id]); |
183
|
|
|
|
184
|
|
|
$this->id = null; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: