|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asymptix\db; |
|
4
|
|
|
|
|
5
|
|
|
use Asymptix\core\Tools; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* DBObject class. Object oriented representation of DB record. |
|
9
|
|
|
* |
|
10
|
|
|
* @category Asymptix PHP Framework |
|
11
|
|
|
* @author Dmytro Zarezenko <[email protected]> |
|
12
|
|
|
* @copyright (c) 2009 - 2016, Dmytro Zarezenko |
|
13
|
|
|
* |
|
14
|
|
|
* @git https://github.com/Asymptix/Framework |
|
15
|
|
|
* @license http://opensource.org/licenses/MIT |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class DBObject extends \Asymptix\core\Object { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Status constants. |
|
21
|
|
|
*/ |
|
22
|
|
|
const STATUS_ACTIVATED = 1; |
|
23
|
|
|
const STATUS_DEACTIVATED = 0; |
|
24
|
|
|
|
|
25
|
|
|
const STATUS_REMOVED = 1; |
|
26
|
|
|
const STATUS_RESTORED = 0; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* DB Query object for Prepared Statement. |
|
30
|
|
|
* |
|
31
|
|
|
* @var DBPreparedQuery |
|
32
|
|
|
*/ |
|
33
|
|
|
private $dbQuery = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Creates new default object. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() {} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns primary key value. |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed. |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
|
|
public function getId() { |
|
46
|
|
|
if (is_null(static::ID_FIELD_NAME)) { |
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->getFieldValue(static::ID_FIELD_NAME); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets primary key value. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $recordId Key vaue. |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool Success flag. |
|
59
|
|
|
* @throws DBCoreException If object has no field with such name. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setId($recordId) { |
|
62
|
|
|
return $this->setFieldValue(static::ID_FIELD_NAME, $recordId); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns name of the primary key field. |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function getIdFieldName() { |
|
71
|
|
|
return static::ID_FIELD_NAME; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns DBObject table name. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function getTableName() { |
|
80
|
|
|
return static::TABLE_NAME; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Saves activation flag to the database. |
|
85
|
|
|
* |
|
86
|
|
|
* @return int Returns the number of affected rows on success, and -1 if |
|
87
|
|
|
* the last query failed. |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
public function saveActivationFlag() { |
|
|
|
|
|
|
90
|
|
|
return DBCore::doUpdateQuery( |
|
91
|
|
|
"UPDATE " . static::TABLE_NAME . " |
|
92
|
|
|
SET activation = ? |
|
93
|
|
|
WHERE " . static::ID_FIELD_NAME . " = ? |
|
94
|
|
|
LIMIT 1", |
|
95
|
|
|
"ii", |
|
96
|
|
|
[$this->activation, $this->id] |
|
|
|
|
|
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Detects if current record is activated. |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
* |
|
105
|
|
|
* @throws DBCoreException If record hos no 'activation' field. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function isActivated() { |
|
108
|
|
|
$activation = $this->getFieldValue('activation'); |
|
109
|
|
|
if (is_null($activation)) { |
|
110
|
|
|
throw new DBCoreException("This object has no parameter 'activation'"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return ($activation > 0); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Activates record and save changes into the database. |
|
118
|
|
|
* |
|
119
|
|
|
* @return int |
|
120
|
|
|
*/ |
|
121
|
|
|
public function activate() { |
|
122
|
|
|
$this->setFieldValue('activation', self::STATUS_ACTIVATED); |
|
123
|
|
|
|
|
124
|
|
|
return $this->saveActivationFlag(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Deactivates record and save changes into the database. |
|
129
|
|
|
* |
|
130
|
|
|
* @return int |
|
131
|
|
|
*/ |
|
132
|
|
|
public function deactivate() { |
|
133
|
|
|
$this->setFieldValue('activation', self::STATUS_DEACTIVATED); |
|
134
|
|
|
|
|
135
|
|
|
return $this->saveActivationFlag(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Changes record activation flag and save changes into the database. |
|
140
|
|
|
*/ |
|
141
|
|
|
public function changeActivation() { |
|
142
|
|
|
if ($this->isActivated()) { |
|
143
|
|
|
$this->deactivate(); |
|
144
|
|
|
} else { |
|
145
|
|
|
$this->activate(); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Saves removement flag to the database. |
|
151
|
|
|
* |
|
152
|
|
|
* @return int Returns the number of affected rows on success, and -1 if |
|
153
|
|
|
* the last query failed. |
|
154
|
|
|
*/ |
|
155
|
|
View Code Duplication |
public function saveRemovementFlag() { |
|
|
|
|
|
|
156
|
|
|
return DBCore::doUpdateQuery( |
|
157
|
|
|
"UPDATE " . static::TABLE_NAME . " |
|
158
|
|
|
SET removed = ? |
|
159
|
|
|
WHERE " . static::ID_FIELD_NAME . " = ? |
|
160
|
|
|
LIMIT 1", |
|
161
|
|
|
"ii", |
|
162
|
|
|
[$this->removed, $this->id] |
|
|
|
|
|
|
163
|
|
|
); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Detects if current record is removed. |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool |
|
170
|
|
|
* |
|
171
|
|
|
* @throws DBCoreException If record hos no 'removed' field. |
|
172
|
|
|
*/ |
|
173
|
|
|
public function isRemoved() { |
|
174
|
|
|
$isRemoved = $this->getFieldValue('removed'); |
|
175
|
|
|
if (is_null($isRemoved)) { |
|
176
|
|
|
throw new DBCoreException("This object has no parameter 'removed'"); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return ($isRemoved == self::STATUS_REMOVED); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Enable removed flag of the record and save changes into the database. |
|
184
|
|
|
* |
|
185
|
|
|
* @return int |
|
186
|
|
|
*/ |
|
187
|
|
|
public function remove() { |
|
188
|
|
|
$this->setFieldValue('removed', self::STATUS_REMOVED); |
|
189
|
|
|
|
|
190
|
|
|
return $this->saveRemovementFlag(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Disable removed flag of the record and save changes into the database. |
|
195
|
|
|
* |
|
196
|
|
|
* @return int |
|
197
|
|
|
*/ |
|
198
|
|
|
public function restore() { |
|
199
|
|
|
$this->setFieldValue('removed', self::STATUS_RESTORED); |
|
200
|
|
|
|
|
201
|
|
|
return $this->saveRemovementFlag(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Changes record removement flag and save changes into the database. |
|
206
|
|
|
*/ |
|
207
|
|
|
public function changeRemovement() { |
|
208
|
|
|
if ($this->isRemoved()) { |
|
209
|
|
|
$this->restore(); |
|
210
|
|
|
} else { |
|
211
|
|
|
$this->remove(); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Detects if current DBObject represents not existed DB record. |
|
217
|
|
|
* |
|
218
|
|
|
* @return bool |
|
219
|
|
|
*/ |
|
220
|
|
|
public function isNewRecord() { |
|
221
|
|
|
if (is_null(static::ID_FIELD_NAME)) { |
|
222
|
|
|
return true; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return ($this->id == 0); |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Saves DBObject to the database. If this is a new object - INSERT SQL |
|
230
|
|
|
* instruction executes, if existed one - UPDATE. |
|
231
|
|
|
* |
|
232
|
|
|
* @param bool $debug Debug mode flag. |
|
233
|
|
|
* |
|
234
|
|
|
* @return mixed Primary key value. |
|
235
|
|
|
* @throws DBCoreException If some database error occurred. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function save($debug = false) { |
|
238
|
|
|
if ($this->isNewRecord()) { |
|
239
|
|
|
$insertionId = DBCore::insertDBObject($this, false, $debug); |
|
240
|
|
|
if (Tools::isInteger($insertionId) && $insertionId > 0) { |
|
241
|
|
|
$this->setId($insertionId); |
|
242
|
|
|
|
|
243
|
|
|
return $insertionId; |
|
244
|
|
|
} |
|
245
|
|
|
throw new DBCoreException("Save database object error"); |
|
246
|
|
|
} |
|
247
|
|
|
DBCore::updateDBObject($this, $debug); |
|
248
|
|
|
|
|
249
|
|
|
return $this->id; |
|
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Inserts DBObject to the database. |
|
254
|
|
|
* |
|
255
|
|
|
* @param bool $ignore Ignore unique indexes or not. |
|
256
|
|
|
* @param bool Debug mode flag. |
|
257
|
|
|
* |
|
258
|
|
|
* @return mixed Primary key value. |
|
259
|
|
|
* @throws DBCoreException If some database error occurred. |
|
260
|
|
|
*/ |
|
261
|
|
|
public function insert($ignore = false, $debug = false) { |
|
262
|
|
|
return DBCore::insertDBObject($this, $ignore, $debug); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Inits SQL query. |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $queryType Type of the SQL query from types list from DBQuery. |
|
269
|
|
|
* @param array $conditions List of conditions for WHERE instruction. |
|
270
|
|
|
* @param array $fields List of fields for INSERT or UPDATE types of SQL queries. |
|
271
|
|
|
* |
|
272
|
|
|
* @return DBObject Itself. |
|
273
|
|
|
*/ |
|
274
|
|
|
public function initQuery($queryType, $conditions = [], $fields = []) { |
|
275
|
|
|
$this->dbQuery = new DBPreparedQuery(); |
|
276
|
|
|
|
|
277
|
|
|
$this->dbQuery->setType($queryType); |
|
278
|
|
|
$this->dbQuery->conditions = $conditions; |
|
279
|
|
|
$this->dbQuery->fields = $fields; |
|
280
|
|
|
|
|
281
|
|
|
/* |
|
282
|
|
|
* Inits LIMIT if called dynamic select() or update() method. |
|
283
|
|
|
*/ |
|
284
|
|
|
if (is_null($this->dbQuery->limit)) { |
|
285
|
|
|
$backTrace = debug_backtrace(); |
|
286
|
|
|
if (is_array($backTrace) && isset($backTrace[1])) { |
|
287
|
|
|
$prevCall = $backTrace[1]; |
|
288
|
|
|
if (is_array($prevCall) && isset($prevCall['type'])) { |
|
289
|
|
|
if ($prevCall['type'] == '->') { // dynamic method was called |
|
290
|
|
|
$this->dbQuery->limit = 1; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
unset($backTrace); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return $this; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Prepare DBObject for the SELECT SQL query. |
|
302
|
|
|
* |
|
303
|
|
|
* @param array $conditions List of the conditions fields |
|
304
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
|
305
|
|
|
* |
|
306
|
|
|
* @return DBObject Current object. |
|
307
|
|
|
*/ |
|
308
|
|
|
public function select($conditions = []) { |
|
309
|
|
|
return $this->initQuery(DBQueryType::SELECT, $conditions); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Static way to prepare DBObject for the SELECT SQL query. |
|
314
|
|
|
* |
|
315
|
|
|
* @param array $conditions List of the conditions fields |
|
316
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
|
317
|
|
|
* |
|
318
|
|
|
* @return DBObject Current object. |
|
319
|
|
|
*/ |
|
320
|
|
|
public static function _select($conditions = []) { |
|
321
|
|
|
$ref = new \ReflectionClass(get_called_class()); |
|
322
|
|
|
$dbObject = $ref->newInstance(); |
|
323
|
|
|
|
|
324
|
|
|
return $dbObject->initQuery(DBQueryType::SELECT, $conditions); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Select and returns DB record for current DBObject table by record ID. |
|
329
|
|
|
* |
|
330
|
|
|
* @param mixed $recordId Record ID. |
|
331
|
|
|
* @param bool $debug Debug mode flag. |
|
332
|
|
|
* |
|
333
|
|
|
* @return DBObject Record object or null. |
|
334
|
|
|
*/ |
|
335
|
|
|
public static function _get($recordId, $debug = false) { |
|
336
|
|
|
return static::_select([ |
|
337
|
|
|
static::ID_FIELD_NAME => $recordId |
|
338
|
|
|
])->limit(1)->go($debug); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Prepare DBObject for the UPDATE SQL query. |
|
343
|
|
|
* |
|
344
|
|
|
* @param type $fields List of fields to be updated |
|
345
|
|
|
* (fieldName => fieldValue or sqlAssignment => params). |
|
346
|
|
|
* @param array $conditions List of the conditions fields |
|
347
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
|
348
|
|
|
* |
|
349
|
|
|
* @return DBObject Current object. |
|
350
|
|
|
*/ |
|
351
|
|
|
public function update($fields = [], $conditions = []) { |
|
352
|
|
|
return $this->initQuery(DBQueryType::UPDATE, $conditions, $fields); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Static way to prepare DBObject for the UPDATE SQL query. |
|
357
|
|
|
* |
|
358
|
|
|
* @param type $fields List of fields to be updated |
|
359
|
|
|
* (fieldName => fieldValue or sqlAssignment => params). |
|
360
|
|
|
* @param array $conditions List of the conditions fields |
|
361
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
|
362
|
|
|
* |
|
363
|
|
|
* @return DBObject Current object. |
|
364
|
|
|
*/ |
|
365
|
|
|
public static function _update($fields = [], $conditions = []) { |
|
366
|
|
|
$ref = new \ReflectionClass(get_called_class()); |
|
367
|
|
|
$dbObject = $ref->newInstance(); |
|
368
|
|
|
|
|
369
|
|
|
return $dbObject->initQuery(DBQueryType::UPDATE, $conditions, $fields); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Prepare DBObject for the select query (for ORDER expression). |
|
374
|
|
|
* |
|
375
|
|
|
* @param array $order List of order conditions (fieldName => order), |
|
376
|
|
|
* order may be 'ASC' OR 'DESC'. |
|
377
|
|
|
* |
|
378
|
|
|
* @param array $order |
|
379
|
|
|
* @return DBObject Current object. |
|
380
|
|
|
*/ |
|
381
|
|
|
public function order($order = null) { |
|
382
|
|
|
$this->dbQuery->order = $order; |
|
|
|
|
|
|
383
|
|
|
|
|
384
|
|
|
return $this; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Prepare DBObject for the select query (for LIMIT expression). |
|
389
|
|
|
* |
|
390
|
|
|
* @param int $offset Limit offset value (or count if this is single |
|
391
|
|
|
* parameter). |
|
392
|
|
|
* @param int $count Number of records to select. |
|
393
|
|
|
* |
|
394
|
|
|
* @return DBObject Current object. |
|
395
|
|
|
*/ |
|
396
|
|
|
public function limit($offset = 1, $count = null) { |
|
397
|
|
|
if (is_null($count)) { |
|
398
|
|
|
$this->dbQuery->limit = $offset; |
|
399
|
|
|
} else { |
|
400
|
|
|
$this->dbQuery->limit = [$offset, $count]; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
return $this; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Selects DB record(s) for current DBObject table according to params. |
|
408
|
|
|
* |
|
409
|
|
|
* @param bool $debug Debug mode flag. |
|
410
|
|
|
* |
|
411
|
|
|
* @return mixed DBObject, array of DBObject or null. |
|
412
|
|
|
* @throws DBCoreException If some DB or query syntax errors occurred. |
|
413
|
|
|
*/ |
|
414
|
|
|
public function go($debug = false) { |
|
415
|
|
|
switch ($this->dbQuery->getType()) { |
|
416
|
|
|
case (DBQueryType::SELECT): |
|
417
|
|
|
$this->dbQuery->query = "SELECT * FROM " . static::TABLE_NAME; |
|
418
|
|
|
break; |
|
419
|
|
|
case (DBQueryType::UPDATE): |
|
420
|
|
|
$this->dbQuery->query = "UPDATE " . static::TABLE_NAME . " SET "; |
|
421
|
|
|
$this->dbQuery->sqlPushValues($this->dbQuery->fields); |
|
422
|
|
|
break; |
|
423
|
|
|
case (DBQueryType::DELETE): |
|
424
|
|
|
$this->dbQuery->query = "DELETE FROM " . static::TABLE_NAME; |
|
425
|
|
|
break; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/* |
|
429
|
|
|
* Conditions |
|
430
|
|
|
*/ |
|
431
|
|
|
if ($this->isNewRecord()) { |
|
432
|
|
|
if (!empty($this->dbQuery->conditions)) { |
|
433
|
|
|
$this->dbQuery->query.= " WHERE "; |
|
434
|
|
|
$this->dbQuery->sqlPushValues($this->dbQuery->conditions, " AND "); |
|
435
|
|
|
} |
|
436
|
|
|
} else { |
|
437
|
|
|
$this->dbQuery->query.= " WHERE "; |
|
438
|
|
|
$this->dbQuery->sqlPushValues([static::ID_FIELD_NAME => $this->id]); |
|
|
|
|
|
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/* |
|
442
|
|
|
* Order |
|
443
|
|
|
*/ |
|
444
|
|
|
if ($this->isNewRecord()) { |
|
445
|
|
|
if (!empty($this->dbQuery->order)) { |
|
446
|
|
|
$this->dbQuery->query.= " ORDER BY"; |
|
447
|
|
|
if (is_array($this->dbQuery->order)) { |
|
448
|
|
|
foreach ($this->dbQuery->order as $fieldName => $ord) { |
|
449
|
|
|
$this->dbQuery->query.= " " . $fieldName . " " . $ord . ","; |
|
450
|
|
|
} |
|
451
|
|
|
$this->dbQuery->query = substr($this->dbQuery->query, 0, strlen($this->dbQuery->query) - 1); |
|
452
|
|
|
} elseif (is_string($this->dbQuery->order)) { |
|
453
|
|
|
$this->dbQuery->query.= " " . $this->dbQuery->order; |
|
454
|
|
|
} |
|
455
|
|
|
} |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/* |
|
459
|
|
|
* Limit |
|
460
|
|
|
*/ |
|
461
|
|
|
$count = null; |
|
462
|
|
|
if ($this->isNewRecord()) { |
|
463
|
|
|
if (!is_null($this->dbQuery->limit)) { |
|
464
|
|
|
if (Tools::isInteger($this->dbQuery->limit)) { |
|
465
|
|
|
$this->dbQuery->query.= " LIMIT " . $this->dbQuery->limit; |
|
466
|
|
|
$count = $this->dbQuery->limit; |
|
467
|
|
|
} elseif (is_array($this->dbQuery->limit) && count($this->dbQuery->limit) == 2) { |
|
468
|
|
|
$offset = $this->dbQuery->limit[0]; |
|
469
|
|
|
$count = $this->dbQuery->limit[1]; |
|
470
|
|
|
if (Tools::isInteger($offset) && Tools::isInteger($count)) { |
|
471
|
|
|
$this->dbQuery->query.= " LIMIT " . $offset . ", " . $count; |
|
472
|
|
|
} else { |
|
473
|
|
|
throw new DBCoreException("Invalid LIMIT param in select() method."); |
|
474
|
|
|
} |
|
475
|
|
|
} else { |
|
476
|
|
|
throw new DBCoreException("Invalid LIMIT param in select() method."); |
|
477
|
|
|
} |
|
478
|
|
|
} |
|
479
|
|
|
} else { |
|
480
|
|
|
$this->dbQuery->query.= " LIMIT 1"; |
|
481
|
|
|
$count = 1; |
|
482
|
|
|
} |
|
483
|
|
|
|
|
484
|
|
|
if ($debug) { |
|
485
|
|
|
DBQuery::showQueryDebugInfo( |
|
486
|
|
|
$this->dbQuery->query, |
|
487
|
|
|
$this->dbQuery->types, |
|
488
|
|
|
$this->dbQuery->params |
|
489
|
|
|
); |
|
490
|
|
|
} else { |
|
491
|
|
|
if ($this->dbQuery->isSelector()) { |
|
492
|
|
|
$stmt = $this->dbQuery->go(); |
|
493
|
|
|
if ($stmt !== false) { |
|
494
|
|
|
$data = null; |
|
|
|
|
|
|
495
|
|
|
if ($count !== 1) { |
|
496
|
|
|
$data = DBCore::selectDBObjectsFromStatement($stmt, $this); |
|
497
|
|
|
} else { |
|
498
|
|
|
$data = DBCore::selectDBObjectFromStatement($stmt, $this); |
|
|
|
|
|
|
499
|
|
|
} |
|
500
|
|
|
$stmt->close(); |
|
501
|
|
|
|
|
502
|
|
|
return $data; |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
return null; |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
return $this->dbQuery->go(); |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
return null; |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Deletes DB record for current DBObject. |
|
516
|
|
|
* |
|
517
|
|
|
* @return mixed Number of affected rows (1 if some record was deleted, |
|
518
|
|
|
* 0 - if no) or FALSE if some error occurred. |
|
519
|
|
|
*/ |
|
520
|
|
|
public function delete() { |
|
521
|
|
|
return DBCore::deleteDBObject($this); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
/** |
|
525
|
|
|
* Deletes DB record by ID or condition. |
|
526
|
|
|
* |
|
527
|
|
|
* @param mixed $conditions List of the conditions fields |
|
528
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
|
529
|
|
|
* or ID value of the record |
|
530
|
|
|
* @return DBObject Current object. |
|
531
|
|
|
*/ |
|
532
|
|
|
public static function _delete($conditions = []) { |
|
533
|
|
|
$ref = new \ReflectionClass(get_called_class()); |
|
534
|
|
|
$dbObject = $ref->newInstance(); |
|
535
|
|
|
|
|
536
|
|
|
if (!is_array($conditions)) { // Just record ID provided |
|
537
|
|
|
$recordId = $conditions; |
|
538
|
|
|
$conditions = [ |
|
539
|
|
|
$dbObject->getIdFieldName() => $recordId |
|
540
|
|
|
]; |
|
541
|
|
|
$dbObject->initQuery(DBQueryType::DELETE, $conditions); |
|
542
|
|
|
$dbObject->dbQuery->limit = 1; |
|
543
|
|
|
|
|
544
|
|
|
return $dbObject; |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
return $dbObject->initQuery(DBQueryType::DELETE, $conditions); |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* Returns DB table field name by it's camelcase variant. |
|
552
|
|
|
* |
|
553
|
|
|
* @param string $methodNameFragment |
|
554
|
|
|
* |
|
555
|
|
|
* @return string |
|
556
|
|
|
*/ |
|
557
|
|
|
protected function getFieldName($methodNameFragment) { |
|
558
|
|
|
return substr(strtolower(preg_replace("#([A-Z]{1})#", "_$1", $methodNameFragment)), 1); |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
} |
|
562
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.