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 - 2018, 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\BasicObject { |
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 Oneself. |
273
|
|
|
* @throws DBCoreException If some error occurred. |
274
|
|
|
*/ |
275
|
|
|
public function initQuery($queryType, $conditions = [], $fields = []) { |
276
|
|
|
$this->dbQuery = new DBPreparedQuery(); |
277
|
|
|
|
278
|
|
|
$this->dbQuery->setType($queryType); |
279
|
|
|
|
280
|
|
|
if (!is_array($conditions)) { |
281
|
|
|
throw new DBCoreException("Invalid conditions array"); |
282
|
|
|
} |
283
|
|
|
$this->dbQuery->conditions = $conditions; |
284
|
|
|
|
285
|
|
|
if (!is_array($fields)) { |
286
|
|
|
throw new DBCoreException("Invalid fields array"); |
287
|
|
|
} |
288
|
|
|
$this->dbQuery->fields = $fields; |
289
|
|
|
|
290
|
|
|
/* |
291
|
|
|
* Inits LIMIT if called dynamic select() or update() method. |
292
|
|
|
*/ |
293
|
|
|
if (is_null($this->dbQuery->limit)) { |
294
|
|
|
$backTrace = debug_backtrace(); |
295
|
|
|
if (is_array($backTrace) && isset($backTrace[1])) { |
296
|
|
|
$prevCall = $backTrace[1]; |
297
|
|
|
if (is_array($prevCall) && isset($prevCall['type'])) { |
298
|
|
|
if ($prevCall['type'] == '->') { // dynamic method was called |
299
|
|
|
$this->dbQuery->limit = 1; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
unset($backTrace); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Prepare DBObject for the SELECT SQL query. |
311
|
|
|
* |
312
|
|
|
* @param array $conditions List of the conditions fields |
313
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
314
|
|
|
* |
315
|
|
|
* @return DBObject Current object. |
316
|
|
|
*/ |
317
|
|
|
public function select($conditions = []) { |
318
|
|
|
return $this->initQuery(DBQueryType::SELECT, $conditions); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Static way to prepare DBObject for the SELECT SQL query. |
323
|
|
|
* |
324
|
|
|
* @param array $conditions List of the conditions fields |
325
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
326
|
|
|
* |
327
|
|
|
* @return DBObject Current object. |
328
|
|
|
*/ |
329
|
|
|
public static function _select($conditions = []) { |
330
|
|
|
$ref = new \ReflectionClass(get_called_class()); |
331
|
|
|
$dbObject = $ref->newInstance(); |
332
|
|
|
|
333
|
|
|
return $dbObject->initQuery(DBQueryType::SELECT, $conditions); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Select and returns DB record for current DBObject table by record ID. |
338
|
|
|
* |
339
|
|
|
* @param mixed $recordId Record ID. |
340
|
|
|
* @param bool $debug Debug mode flag. |
341
|
|
|
* |
342
|
|
|
* @return DBObject Record object or null. |
343
|
|
|
*/ |
344
|
|
|
public static function _get($recordId, $debug = false) { |
345
|
|
|
return static::_select([ |
346
|
|
|
static::ID_FIELD_NAME => $recordId |
347
|
|
|
])->limit(1)->go($debug); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Returns result of the COUNT() SQL query. |
352
|
|
|
* |
353
|
|
|
* @param array $conditions Conditions list. |
354
|
|
|
* @param type $debug Debug mode flag. |
355
|
|
|
* |
356
|
|
|
* @return int |
357
|
|
|
*/ |
358
|
|
View Code Duplication |
public static function _count($conditions = [], $debug = false) { |
|
|
|
|
359
|
|
|
$dbQuery = (new DBPreparedQuery())->prepare( |
360
|
|
|
"SELECT COUNT(*) as 'val' FROM " . static::TABLE_NAME, |
361
|
|
|
$conditions |
362
|
|
|
); |
363
|
|
|
|
364
|
|
|
if (!$debug) { |
365
|
|
|
return (int)DBCore::selectSingleValue($dbQuery); |
366
|
|
|
} |
367
|
|
|
$dbQuery->debug(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Returns result of the MAX($field) SQL query. |
372
|
|
|
* |
373
|
|
|
* @param string $field Name of the field. |
374
|
|
|
* @param array $conditions Conditions list. |
375
|
|
|
* @param type $debug Debug mode flag. |
376
|
|
|
* |
377
|
|
|
* @return int |
378
|
|
|
*/ |
379
|
|
View Code Duplication |
public static function _max($field, $conditions = [], $debug = false) { |
|
|
|
|
380
|
|
|
$dbQuery = (new DBPreparedQuery())->prepare( |
381
|
|
|
"SELECT MAX(`" . $field . "`) as 'val' FROM " . static::TABLE_NAME, |
382
|
|
|
$conditions |
383
|
|
|
); |
384
|
|
|
|
385
|
|
|
if (!$debug) { |
386
|
|
|
return DBCore::selectSingleValue($dbQuery); |
387
|
|
|
} |
388
|
|
|
$dbQuery->debug(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Returns result of the MIN($field) SQL query. |
393
|
|
|
* |
394
|
|
|
* @param string $field Name of the field. |
395
|
|
|
* @param array $conditions Conditions list. |
396
|
|
|
* @param type $debug Debug mode flag. |
397
|
|
|
* |
398
|
|
|
* @return int |
399
|
|
|
*/ |
400
|
|
View Code Duplication |
public static function _min($field, $conditions = [], $debug = false) { |
|
|
|
|
401
|
|
|
$dbQuery = (new DBPreparedQuery())->prepare( |
402
|
|
|
"SELECT MIN(`" . $field . "`) as 'val' FROM " . static::TABLE_NAME, |
403
|
|
|
$conditions |
404
|
|
|
); |
405
|
|
|
|
406
|
|
|
if (!$debug) { |
407
|
|
|
return DBCore::selectSingleValue($dbQuery); |
408
|
|
|
} |
409
|
|
|
$dbQuery->debug(); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Prepare DBObject for the UPDATE SQL query. |
414
|
|
|
* |
415
|
|
|
* @param type $fields List of fields to be updated |
416
|
|
|
* (fieldName => fieldValue or sqlAssignment => params). |
417
|
|
|
* @param array $conditions List of the conditions fields |
418
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
419
|
|
|
* |
420
|
|
|
* @return DBObject Current object. |
421
|
|
|
*/ |
422
|
|
|
public function update($fields = [], $conditions = []) { |
423
|
|
|
return $this->initQuery(DBQueryType::UPDATE, $conditions, $fields); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Static way to prepare DBObject for the UPDATE SQL query. |
428
|
|
|
* |
429
|
|
|
* @param type $fields List of fields to be updated |
430
|
|
|
* (fieldName => fieldValue or sqlAssignment => params). |
431
|
|
|
* @param array $conditions List of the conditions fields |
432
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
433
|
|
|
* |
434
|
|
|
* @return DBObject Current object. |
435
|
|
|
*/ |
436
|
|
|
public static function _update($fields = [], $conditions = []) { |
437
|
|
|
$ref = new \ReflectionClass(get_called_class()); |
438
|
|
|
$dbObject = $ref->newInstance(); |
439
|
|
|
|
440
|
|
|
return $dbObject->initQuery(DBQueryType::UPDATE, $conditions, $fields); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Prepare DBObject for the select query (for ORDER expression). |
445
|
|
|
* |
446
|
|
|
* @param array $order List of order conditions (fieldName => order), |
447
|
|
|
* order may be 'ASC' OR 'DESC'. |
448
|
|
|
* |
449
|
|
|
* @param array $order |
450
|
|
|
* @return DBObject Current object. |
451
|
|
|
*/ |
452
|
|
|
public function order($order = null) { |
453
|
|
|
$this->dbQuery->order = $order; |
|
|
|
|
454
|
|
|
|
455
|
|
|
return $this; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Prepare DBObject for the select query (for LIMIT expression). |
460
|
|
|
* |
461
|
|
|
* @param int $offset Limit offset value (or count if this is single |
462
|
|
|
* parameter). |
463
|
|
|
* @param int $count Number of records to select. |
464
|
|
|
* |
465
|
|
|
* @return DBObject Current object. |
466
|
|
|
*/ |
467
|
|
|
public function limit($offset = 1, $count = null) { |
468
|
|
|
if (is_null($offset)) { |
469
|
|
|
return $this; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
View Code Duplication |
if (is_null($count)) { |
|
|
|
|
473
|
|
|
$this->dbQuery->limit = $offset; |
474
|
|
|
} else { |
475
|
|
|
$this->dbQuery->limit = [$offset, $count]; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Selects DB record(s) for current DBObject table according to params. |
483
|
|
|
* |
484
|
|
|
* @param bool $debug Debug mode flag. |
485
|
|
|
* |
486
|
|
|
* @return mixed DBObject, array of DBObject or null. |
487
|
|
|
* @throws DBCoreException If some DB or query syntax errors occurred. |
488
|
|
|
*/ |
489
|
|
|
public function go($debug = false) { |
490
|
|
|
switch ($this->dbQuery->getType()) { |
491
|
|
|
case (DBQueryType::SELECT): |
492
|
|
|
$this->dbQuery->query = "SELECT * FROM " . static::TABLE_NAME; |
493
|
|
|
break; |
494
|
|
|
case (DBQueryType::UPDATE): |
495
|
|
|
$this->dbQuery->query = "UPDATE " . static::TABLE_NAME . " SET "; |
496
|
|
|
$this->dbQuery->sqlPushValues($this->dbQuery->fields); |
497
|
|
|
break; |
498
|
|
|
case (DBQueryType::DELETE): |
499
|
|
|
$this->dbQuery->query = "DELETE FROM " . static::TABLE_NAME; |
500
|
|
|
break; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/* |
504
|
|
|
* Conditions |
505
|
|
|
*/ |
506
|
|
|
if ($this->isNewRecord()) { |
507
|
|
|
$this->dbQuery->prepareConditions(); |
508
|
|
|
} else { |
509
|
|
|
$this->dbQuery->query.= " WHERE "; |
510
|
|
|
$this->dbQuery->sqlPushValues([static::ID_FIELD_NAME => $this->id]); |
|
|
|
|
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/* |
514
|
|
|
* Order |
515
|
|
|
*/ |
516
|
|
|
if ($this->isNewRecord()) { |
517
|
|
|
$this->dbQuery->prepareOrder(); |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/* |
521
|
|
|
* Limit |
522
|
|
|
*/ |
523
|
|
|
$count = null; |
|
|
|
|
524
|
|
|
if ($this->isNewRecord()) { |
525
|
|
|
$count = $this->dbQuery->prepareLimit(); |
526
|
|
|
} else { |
527
|
|
|
$this->dbQuery->query.= " LIMIT 1"; |
528
|
|
|
$count = 1; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
if ($debug) { |
532
|
|
|
$this->dbQuery->debug(); |
533
|
|
|
} else { |
534
|
|
|
if ($this->dbQuery->isSelector()) { |
535
|
|
|
$stmt = $this->dbQuery->go(); |
536
|
|
|
if ($stmt !== false) { |
537
|
|
|
$data = null; |
|
|
|
|
538
|
|
|
if ($count !== 1) { |
539
|
|
|
$data = DBCore::selectDBObjectsFromStatement($stmt, $this); |
540
|
|
|
} else { |
541
|
|
|
$data = DBCore::selectDBObjectFromStatement($stmt, $this); |
|
|
|
|
542
|
|
|
} |
543
|
|
|
$stmt->close(); |
544
|
|
|
|
545
|
|
|
return $data; |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
return null; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
return $this->dbQuery->go(); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
return null; |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Deletes DB record for current DBObject. |
559
|
|
|
* |
560
|
|
|
* @return mixed Number of affected rows (1 if some record was deleted, |
561
|
|
|
* 0 - if no) or FALSE if some error occurred. |
562
|
|
|
*/ |
563
|
|
|
public function delete() { |
564
|
|
|
return DBCore::deleteDBObject($this); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* Deletes DB record by ID or condition. |
569
|
|
|
* |
570
|
|
|
* @param mixed $conditions List of the conditions fields |
571
|
|
|
* (fieldName => fieldValue or sqlCondition => params). |
572
|
|
|
* or ID value of the record |
573
|
|
|
* @return DBObject Current object. |
574
|
|
|
*/ |
575
|
|
|
public static function _delete($conditions = []) { |
576
|
|
|
$ref = new \ReflectionClass(get_called_class()); |
577
|
|
|
$dbObject = $ref->newInstance(); |
578
|
|
|
|
579
|
|
|
if (!is_array($conditions)) { // Just record ID provided |
580
|
|
|
$recordId = $conditions; |
581
|
|
|
$conditions = [ |
582
|
|
|
$dbObject->getIdFieldName() => $recordId |
583
|
|
|
]; |
584
|
|
|
$dbObject->initQuery(DBQueryType::DELETE, $conditions); |
585
|
|
|
$dbObject->dbQuery->limit = 1; |
586
|
|
|
|
587
|
|
|
return $dbObject; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
return $dbObject->initQuery(DBQueryType::DELETE, $conditions); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Returns DB table field name by it's camelcase variant. |
595
|
|
|
* |
596
|
|
|
* @param string $methodNameFragment |
597
|
|
|
* |
598
|
|
|
* @return string |
599
|
|
|
*/ |
600
|
|
|
protected function getFieldName($methodNameFragment) { |
601
|
|
|
return substr(strtolower(preg_replace("#([A-Z]{1})#", "_$1", $methodNameFragment)), 1); |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
} |
605
|
|
|
|
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.