1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Anax\Database;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* Base class for database models
|
7
|
|
|
*
|
8
|
|
|
*/
|
9
|
|
|
class CDatabaseModel implements \Anax\DI\IInjectionAware
|
10
|
|
|
{
|
11
|
|
|
use \Anax\DI\TInjectable;
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* Get the table name.
|
16
|
|
|
*
|
17
|
|
|
* @return string with the table name.
|
18
|
|
|
*/
|
19
|
5 |
|
public function getSource()
|
20
|
|
|
{
|
21
|
5 |
|
return strtolower(implode('', array_slice(explode('\\', get_class($this)), -1)));
|
22
|
|
|
}
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* Find and return specific.
|
26
|
|
|
*
|
27
|
|
|
* @return this
|
28
|
|
|
*/
|
29
|
1 |
|
public function find($id)
|
30
|
|
|
{
|
31
|
1 |
|
$this->db->select()
|
|
|
|
|
32
|
1 |
|
->from($this->getSource())
|
33
|
1 |
|
->where("id = ?");
|
34
|
|
|
|
35
|
1 |
|
$this->db->execute([$id]);
|
|
|
|
|
36
|
|
|
return $this->db->fetchInto($this);
|
|
|
|
|
37
|
|
|
}
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/**
|
41
|
|
|
* Find and return all.
|
42
|
|
|
*
|
43
|
|
|
* @return array
|
44
|
|
|
*/
|
45
|
|
|
public function findAll()
|
46
|
|
|
{
|
47
|
|
|
$this->db->select()
|
|
|
|
|
48
|
|
|
->from($this->getSource());
|
49
|
|
|
|
50
|
|
|
$this->db->execute();
|
|
|
|
|
51
|
|
|
$this->db->setFetchModeClass(__CLASS__);
|
|
|
|
|
52
|
|
|
return $this->db->fetchAll();
|
|
|
|
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Find and return rows based on condition.
|
57
|
|
|
*
|
58
|
|
|
* @param string $column as key for condition
|
59
|
|
|
* @param string $value as value for condition.
|
60
|
|
|
*
|
61
|
|
|
* @return array
|
62
|
|
|
*/
|
63
|
1 |
|
public function findWhere($column, $value) {
|
|
|
|
|
64
|
1 |
|
$this->db->select()
|
|
|
|
|
65
|
1 |
|
->from($this->getSource())
|
66
|
1 |
|
->where($column . ' = ?');
|
67
|
|
|
|
68
|
1 |
|
$this->db->execute([$value]);
|
|
|
|
|
69
|
|
|
$this->db->setFetchModeClass(__CLASS__);
|
|
|
|
|
70
|
|
|
return $this->db->fetchAll();
|
|
|
|
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* Find and return rows in specific order.
|
75
|
|
|
*
|
76
|
|
|
* @param string $column as key for condition
|
77
|
|
|
* @param string $order as order to sort in, ASC or DESC.
|
78
|
|
|
*
|
79
|
|
|
* @return array
|
80
|
|
|
*/
|
81
|
1 |
|
public function findAllOrder($column, $order) {
|
|
|
|
|
82
|
1 |
|
$this->db->select()
|
|
|
|
|
83
|
1 |
|
->from($this->getSource())
|
84
|
1 |
|
->orderBy($column . ' ' . $order);
|
85
|
|
|
|
86
|
1 |
|
$this->db->execute();
|
|
|
|
|
87
|
|
|
$this->db->setFetchModeClass(__CLASS__);
|
|
|
|
|
88
|
|
|
return $this->db->fetchAll();
|
|
|
|
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* Get object properties.
|
93
|
|
|
*
|
94
|
|
|
* @return array with object properties.
|
95
|
|
|
*/
|
96
|
|
|
public function getProperties()
|
97
|
|
|
{
|
98
|
|
|
$properties = get_object_vars($this);
|
99
|
|
|
unset($properties['di']);
|
100
|
|
|
unset($properties['db']);
|
101
|
|
|
|
102
|
|
|
return $properties;
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
/**
|
106
|
|
|
* Set object properties.
|
107
|
|
|
*
|
108
|
|
|
* @param array $properties with properties to set.
|
109
|
|
|
*
|
110
|
|
|
* @return void
|
111
|
|
|
*/
|
112
|
|
|
public function setProperties($properties)
|
113
|
|
|
{
|
114
|
|
|
// Update object with incoming values, if any
|
115
|
|
|
if (!empty($properties)) {
|
116
|
|
|
foreach ($properties as $key => $val) {
|
117
|
|
|
$this->$key = $val;
|
118
|
|
|
}
|
119
|
|
|
}
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
/**
|
123
|
|
|
* Save current object/row.
|
124
|
|
|
*
|
125
|
|
|
* @param array $values key/values to save or empty to use object properties.
|
126
|
|
|
*
|
127
|
|
|
* @return boolean true or false if saving went okey.
|
128
|
|
|
*/
|
129
|
|
|
public function save($values = [])
|
130
|
|
|
{
|
131
|
|
|
$this->setProperties($values);
|
132
|
|
|
$values = $this->getProperties();
|
133
|
|
|
|
134
|
|
|
if (isset($values['id'])) {
|
135
|
|
|
return $this->update($values);
|
136
|
|
|
} else {
|
137
|
|
|
return $this->create($values);
|
138
|
|
|
}
|
139
|
|
|
}
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/**
|
143
|
|
|
* Create new row.
|
144
|
|
|
*
|
145
|
|
|
* @param array $values key/values to save.
|
146
|
|
|
*
|
147
|
|
|
* @return boolean true or false if saving went okey.
|
148
|
|
|
*/
|
149
|
|
|
public function create($values)
|
150
|
|
|
{
|
151
|
|
|
$keys = array_keys($values);
|
152
|
|
|
$values = array_values($values);
|
153
|
|
|
|
154
|
|
|
$this->db->insert(
|
|
|
|
|
155
|
|
|
$this->getSource(),
|
156
|
|
|
$keys
|
157
|
|
|
);
|
158
|
|
|
|
159
|
|
|
$res = $this->db->execute($values);
|
|
|
|
|
160
|
|
|
|
161
|
|
|
$this->id = $this->db->lastInsertId();
|
|
|
|
|
162
|
|
|
|
163
|
|
|
return $res;
|
164
|
|
|
}
|
165
|
|
|
|
166
|
|
|
/**
|
167
|
|
|
* Update row.
|
168
|
|
|
*
|
169
|
|
|
* @param array $values key/values to save.
|
170
|
|
|
*
|
171
|
|
|
* @return boolean true or false if saving went okey.
|
172
|
|
|
*/
|
173
|
|
|
public function update($values)
|
174
|
|
|
{
|
175
|
|
|
// Its update, remove id
|
176
|
|
|
unset($values['id']);
|
177
|
|
|
|
178
|
|
|
$keys = array_keys($values);
|
179
|
|
|
$values = array_values($values);
|
180
|
|
|
|
181
|
|
|
// Use id as where-clause
|
182
|
|
|
$values[] = $this->id;
|
183
|
|
|
|
184
|
|
|
$this->db->update(
|
|
|
|
|
185
|
|
|
$this->getSource(),
|
186
|
|
|
$keys,
|
187
|
|
|
"id = ?"
|
188
|
|
|
);
|
189
|
|
|
|
190
|
|
|
return $this->db->execute($values);
|
|
|
|
|
191
|
|
|
}
|
192
|
|
|
|
193
|
|
|
/**
|
194
|
|
|
* Delete row by id.
|
195
|
|
|
*
|
196
|
|
|
* @param integer $id to delete.
|
197
|
|
|
*
|
198
|
|
|
* @return boolean true or false if deleting went okey.
|
199
|
|
|
*/
|
200
|
|
|
public function delete($id)
|
201
|
|
|
{
|
202
|
|
|
$this->db->delete(
|
|
|
|
|
203
|
|
|
$this->getSource(),
|
204
|
|
|
'id = ?'
|
205
|
|
|
);
|
206
|
|
|
|
207
|
|
|
return $this->db->execute([$id]);
|
|
|
|
|
208
|
|
|
}
|
209
|
|
|
|
210
|
|
|
/**
|
211
|
|
|
* Delete all rows with where condition.
|
212
|
|
|
*
|
213
|
|
|
* @param string $column as key for condition
|
214
|
|
|
* @param string $value as value for condition.
|
215
|
|
|
*
|
216
|
|
|
* @return boolean true or false if deleting went okey.
|
217
|
|
|
*/
|
218
|
|
|
public function deleteWhere($column, $value)
|
219
|
|
|
{
|
220
|
|
|
$this->db->delete(
|
|
|
|
|
221
|
|
|
$this->getSource(),
|
222
|
|
|
$column . ' = ?'
|
223
|
|
|
);
|
224
|
|
|
|
225
|
|
|
return $this->db->execute([$value]);
|
|
|
|
|
226
|
|
|
}
|
227
|
|
|
|
228
|
|
|
/**
|
229
|
|
|
* Delete all rows.
|
230
|
|
|
*
|
231
|
|
|
* @return boolean true or false if deleting went okey.
|
232
|
|
|
*/
|
233
|
|
|
public function deleteAll()
|
234
|
|
|
{
|
235
|
|
|
$this->db->delete(
|
|
|
|
|
236
|
|
|
$this->getSource()
|
237
|
|
|
);
|
238
|
|
|
|
239
|
|
|
return $this->db->execute();
|
|
|
|
|
240
|
|
|
}
|
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/**
|
244
|
|
|
* Build a select-query.
|
245
|
|
|
*
|
246
|
|
|
* @param string $columns which columns to select.
|
247
|
|
|
*
|
248
|
|
|
* @return $this
|
249
|
|
|
*/
|
250
|
|
|
public function query($columns = '*')
|
251
|
|
|
{
|
252
|
|
|
$this->db->select($columns)
|
|
|
|
|
253
|
|
|
->from($this->getSource());
|
254
|
|
|
|
255
|
|
|
return $this;
|
256
|
|
|
}
|
257
|
|
|
|
258
|
|
|
/**
|
259
|
|
|
* Build the where part.
|
260
|
|
|
*
|
261
|
|
|
* @param string $condition for building the where part of the query.
|
262
|
|
|
*
|
263
|
|
|
* @return $this
|
264
|
|
|
*/
|
265
|
|
|
public function where($condition)
|
266
|
|
|
{
|
267
|
|
|
$this->db->where($condition);
|
|
|
|
|
268
|
|
|
|
269
|
|
|
return $this;
|
270
|
|
|
}
|
271
|
|
|
|
272
|
|
|
/**
|
273
|
|
|
* Build the where part.
|
274
|
|
|
*
|
275
|
|
|
* @param string $condition for building the where part of the query.
|
276
|
|
|
*
|
277
|
|
|
* @return $this
|
278
|
|
|
*/
|
279
|
|
|
public function andWhere($condition)
|
280
|
|
|
{
|
281
|
|
|
$this->db->andWhere($condition);
|
|
|
|
|
282
|
|
|
|
283
|
|
|
return $this;
|
284
|
|
|
}
|
285
|
|
|
|
286
|
|
|
/**
|
287
|
|
|
* Execute the query built.
|
288
|
|
|
*
|
289
|
|
|
* @param array $params for custom query.
|
290
|
|
|
*
|
291
|
|
|
* @return $this
|
292
|
|
|
*/
|
293
|
|
|
public function execute($params = [])
|
294
|
|
|
{
|
295
|
|
|
$this->db->execute($this->db->getSQL(), $params);
|
|
|
|
|
296
|
|
|
$this->db->setFetchModeClass(__CLASS__);
|
|
|
|
|
297
|
|
|
|
298
|
|
|
return $this->db->fetchAll();
|
|
|
|
|
299
|
|
|
}
|
300
|
|
|
|
301
|
|
|
/**
|
302
|
|
|
* Create a slug of a string, to be used as url.
|
303
|
|
|
*
|
304
|
|
|
* @param string $str the string to format as slug.
|
305
|
|
|
* @returns str the formatted slug.
|
306
|
|
|
*/
|
307
|
|
|
function slugify($str) {
|
|
|
|
|
308
|
|
|
$str = mb_strtolower(trim($str));
|
309
|
|
|
$str = str_replace(array('å','ä','ö'), array('a','a','o'), $str);
|
310
|
|
|
$str = preg_replace('/[^a-z0-9-]/', '-', $str);
|
311
|
|
|
$str = trim(preg_replace('/-+/', '-', $str), '-');
|
312
|
|
|
return $str;
|
313
|
|
|
}
|
314
|
|
|
|
315
|
|
|
} |
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.