1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jovis\DatabaseModel; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Model for databasecommunication. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class CDatabaseModel implements \Anax\DI\IInjectionAware |
10
|
|
|
{ |
11
|
|
|
use \Anax\DI\TInjectable; |
12
|
|
|
|
13
|
|
|
public $id; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get the table name. |
18
|
|
|
* |
19
|
|
|
* @return string with the table name. |
20
|
|
|
*/ |
21
|
5 |
|
public function getSource() |
22
|
|
|
{ |
23
|
5 |
|
return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1))); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Find and return all. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
1 |
|
public function findAll() |
32
|
|
|
{ |
33
|
1 |
|
$this->db->select() |
|
|
|
|
34
|
1 |
|
->from($this->getSource()); |
35
|
|
|
|
36
|
1 |
|
$this->db->execute(); |
|
|
|
|
37
|
1 |
|
$this->db->setFetchModeClass(__CLASS__); |
|
|
|
|
38
|
1 |
|
return $this->db->fetchAll(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get object properties. |
43
|
|
|
* |
44
|
|
|
* @return array with object properties. |
45
|
|
|
*/ |
46
|
2 |
|
public function getProperties() |
47
|
|
|
{ |
48
|
2 |
|
$properties = get_object_vars($this); |
49
|
2 |
|
unset($properties['di']); |
50
|
2 |
|
unset($properties['db']); |
51
|
|
|
|
52
|
2 |
|
return $properties; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Find and return specific. |
58
|
|
|
* |
59
|
|
|
* @return this |
60
|
|
|
*/ |
61
|
1 |
|
public function find($id) |
62
|
|
|
{ |
63
|
1 |
|
$this->db->select() |
|
|
|
|
64
|
1 |
|
->from($this->getSource()) |
65
|
1 |
|
->where("id = ?"); |
66
|
|
|
|
67
|
1 |
|
$this->db->execute([$id]); |
|
|
|
|
68
|
1 |
|
return $this->db->fetchInto($this); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Save current object/row. |
73
|
|
|
* |
74
|
|
|
* @param array $values key/values to save or empty to use object properties. |
75
|
|
|
* |
76
|
|
|
* @return boolean true or false if saving went okey. |
77
|
|
|
*/ |
78
|
1 |
|
public function save($values = []) |
79
|
|
|
{ |
80
|
|
|
|
81
|
1 |
|
$this->setProperties($values); |
82
|
1 |
|
$values = $this->getProperties(); |
83
|
|
|
|
84
|
1 |
|
if (isset($values['id'])) { |
85
|
1 |
|
return $this->update($values); |
86
|
|
|
} else { |
87
|
|
|
return $this->create($values); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set object properties. |
93
|
|
|
* |
94
|
|
|
* @param array $properties with properties to set. |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
2 |
|
public function setProperties($properties) |
99
|
|
|
{ |
100
|
|
|
// Update object with incoming values, if any |
101
|
2 |
|
if (!empty($properties)) { |
102
|
2 |
|
foreach ($properties as $key => $val) { |
103
|
2 |
|
$this->$key = $val; |
104
|
2 |
|
} |
105
|
2 |
|
} |
106
|
2 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Create new row. |
110
|
|
|
* |
111
|
|
|
* @param array $values key/values to save. |
112
|
|
|
* |
113
|
|
|
* @return boolean true or false if saving went okey. |
114
|
|
|
*/ |
115
|
|
|
public function create($values) |
116
|
|
|
{ |
117
|
|
|
$keys = array_keys($values); |
118
|
|
|
$values = array_values($values); |
119
|
|
|
|
120
|
|
|
$this->db->insert( |
|
|
|
|
121
|
|
|
$this->getSource(), |
122
|
|
|
$keys |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$res = $this->db->execute($values); |
|
|
|
|
126
|
|
|
|
127
|
|
|
$this->id = $this->db->lastInsertId(); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $res; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Update row. |
135
|
|
|
* |
136
|
|
|
* @param array $values key/values to save. |
137
|
|
|
* |
138
|
|
|
* @return boolean true or false if saving went okey. |
139
|
|
|
*/ |
140
|
1 |
|
public function update($values) |
141
|
|
|
{ |
142
|
|
|
|
143
|
1 |
|
$keys = array_keys($values); |
144
|
1 |
|
$values = array_values($values); |
145
|
|
|
|
146
|
|
|
// Its update, remove id and use as where-clause |
147
|
1 |
|
unset($keys['id']); |
148
|
|
|
|
149
|
1 |
|
$values[] = $this->id; |
150
|
|
|
|
151
|
1 |
|
$this->db->update( |
|
|
|
|
152
|
1 |
|
$this->getSource(), |
153
|
1 |
|
$keys, |
154
|
|
|
"id = ?" |
155
|
1 |
|
); |
156
|
|
|
|
157
|
1 |
|
return $this->db->execute($values); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Delete row. |
162
|
|
|
* |
163
|
|
|
* @param integer $id to delete. |
164
|
|
|
* |
165
|
|
|
* @return boolean true or false if deleting went okey. |
166
|
|
|
*/ |
167
|
1 |
|
public function delete($id) |
168
|
|
|
{ |
169
|
1 |
|
$this->db->delete( |
|
|
|
|
170
|
1 |
|
$this->getSource(), |
171
|
|
|
'id = ?' |
172
|
1 |
|
); |
173
|
|
|
|
174
|
1 |
|
return $this->db->execute([$id]); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Build a select-query. |
180
|
|
|
* |
181
|
|
|
* @param string $columns which columns to select. |
182
|
|
|
* |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
1 |
|
public function query($columns = '*') |
186
|
|
|
{ |
187
|
1 |
|
$this->db->select($columns) |
|
|
|
|
188
|
1 |
|
->from($this->getSource()); |
189
|
|
|
|
190
|
1 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Build the where part. |
195
|
|
|
* |
196
|
|
|
* @param string $condition for building the where part of the query. |
197
|
|
|
* |
198
|
|
|
* @return $this |
199
|
|
|
*/ |
200
|
1 |
|
public function where($condition) |
201
|
|
|
{ |
202
|
1 |
|
$this->db->where($condition); |
|
|
|
|
203
|
|
|
|
204
|
1 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Build the where part. |
209
|
|
|
* |
210
|
|
|
* @param string $condition for building the where part of the query. |
211
|
|
|
* |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
1 |
|
public function andWhere($condition) |
215
|
|
|
{ |
216
|
1 |
|
$this->db->andWhere($condition); |
|
|
|
|
217
|
|
|
|
218
|
1 |
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Execute the query built. |
223
|
|
|
* |
224
|
|
|
* @param array $params |
225
|
|
|
* |
226
|
|
|
* @return $this |
227
|
|
|
*/ |
228
|
1 |
|
public function execute($params = []) |
229
|
|
|
{ |
230
|
1 |
|
$this->db->execute($this->db->getSQL(), $params); |
|
|
|
|
231
|
1 |
|
$this->db->setFetchModeClass(__CLASS__); |
|
|
|
|
232
|
|
|
|
233
|
1 |
|
return $this->db->fetchAll(); |
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.