Completed
Push — master ( 69d137...9b4748 )
by Dmytro
04:18 queued 01:44
created

DBObject::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
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.
0 ignored issues
show
Documentation introduced by
The doc-type mixed. could not be parsed: Unknown type name "mixed." at position 0. (view supported doc-types)

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.

Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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]
0 ignored issues
show
Documentation introduced by
The property activation does not exist on object<Asymptix\db\DBObject>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
Documentation introduced by
The property id does not exist on object<Asymptix\db\DBObject>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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]
0 ignored issues
show
Documentation introduced by
The property removed does not exist on object<Asymptix\db\DBObject>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
Documentation introduced by
The property id does not exist on object<Asymptix\db\DBObject>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Asymptix\db\DBObject>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
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, $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;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Asymptix\db\DBObject>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
250
    }
251
252
    /**
253
     * Inits SQL query.
254
     *
255
     * @param string $queryType Type of the SQL query from types list from DBQuery.
256
     * @param array $conditions List of conditions for WHERE instruction.
257
     * @param array $fields List of fields for INSERT or UPDATE types of SQL queries.
258
     *
259
     * @return DBObject Itself.
260
     */
261
    public function initQuery($queryType, $conditions = [], $fields = []) {
262
        $this->dbQuery = new DBPreparedQuery();
263
264
        $this->dbQuery->setType($queryType);
265
        $this->dbQuery->conditions = $conditions;
266
        $this->dbQuery->fields = $fields;
267
268
        /*
269
         * Inits LIMIT if called dynamic select() or update() method.
270
         */
271
        if (is_null($this->dbQuery->limit)) {
272
            $backTrace = debug_backtrace();
273
            if (is_array($backTrace) && isset($backTrace[1])) {
274
                $prevCall = $backTrace[1];
275
                if (is_array($prevCall) && isset($prevCall['type'])) {
276
                    if ($prevCall['type'] == '->') { // dynamic method was called
277
                        $this->dbQuery->limit = 1;
278
                    }
279
                }
280
            }
281
            unset($backTrace);
282
        }
283
284
        return $this;
285
    }
286
287
    /**
288
     * Prepare DBObject for the SELECT SQL query.
289
     *
290
     * @param array $conditions List of the conditions fields
291
     *           (fieldName => fieldValue or sqlCondition => params).
292
     *
293
     * @return DBObject Current object.
294
     */
295
    public function select($conditions = []) {
296
        return $this->initQuery(DBQueryType::SELECT, $conditions);
297
    }
298
299
    /**
300
     * Static way to prepare DBObject for the SELECT SQL query.
301
     *
302
     * @param array $conditions List of the conditions fields
303
     *           (fieldName => fieldValue or sqlCondition => params).
304
     *
305
     * @return DBObject Current object.
306
     */
307
    public static function _select($conditions = []) {
308
        $ref = new \ReflectionClass(get_called_class());
309
        $dbObject = $ref->newInstance();
310
311
        return $dbObject->initQuery(DBQueryType::SELECT, $conditions);
312
    }
313
314
    /**
315
     * Select and returns DB record for current DBObject table by record ID.
316
     *
317
     * @param mixed $recordId Record ID.
318
     * @param bool $debug Debug mode flag.
319
     *
320
     * @return DBObject Record object or null.
321
     */
322
    public static function _get($recordId, $debug = false) {
323
        return static::_select([
324
            static::ID_FIELD_NAME => $recordId
325
        ])->limit(1)->go($debug);
326
    }
327
328
    /**
329
     * Prepare DBObject for the UPDATE SQL query.
330
     *
331
     * @param type $fields List of fields to be updated
332
     *           (fieldName => fieldValue or sqlAssignment => params).
333
     * @param array $conditions List of the conditions fields
334
     *           (fieldName => fieldValue or sqlCondition => params).
335
     *
336
     * @return DBObject Current object.
337
     */
338
    public function update($fields = [], $conditions = []) {
339
        return $this->initQuery(DBQueryType::UPDATE, $conditions, $fields);
340
    }
341
342
    /**
343
     * Static way to prepare DBObject for the UPDATE SQL query.
344
     *
345
     * @param type $fields List of fields to be updated
346
     *           (fieldName => fieldValue or sqlAssignment => params).
347
     * @param array $conditions List of the conditions fields
348
     *           (fieldName => fieldValue or sqlCondition => params).
349
     *
350
     * @return DBObject Current object.
351
     */
352
    public static function _update($fields = [], $conditions = []) {
353
        $ref = new \ReflectionClass(get_called_class());
354
        $dbObject = $ref->newInstance();
355
356
        return $dbObject->initQuery(DBQueryType::UPDATE, $conditions, $fields);
357
    }
358
359
    /**
360
     * Prepare DBObject for the select query (for ORDER expression).
361
     *
362
     * @param array $order List of order conditions (fieldName => order),
363
     *           order may be 'ASC' OR 'DESC'.
364
     *
365
     * @param array $order
366
     * @return DBObject Current object.
367
     */
368
    public function order($order = null) {
369
        $this->dbQuery->order = $order;
0 ignored issues
show
Documentation Bug introduced by
It seems like $order can be null. However, the property $order is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
370
371
        return $this;
372
    }
373
374
    /**
375
     * Prepare DBObject for the select query (for LIMIT expression).
376
     *
377
     * @param int $offset Limit offset value (or count if this is single
378
     *           parameter).
379
     * @param int $count Number of records to select.
380
     *
381
     * @return DBObject Current object.
382
     */
383
    public function limit($offset = 1, $count = null) {
384
        if (is_null($count)) {
385
            $this->dbQuery->limit = $offset;
386
        } else {
387
            $this->dbQuery->limit = [$offset, $count];
388
        }
389
390
        return $this;
391
    }
392
393
    /**
394
     * Selects DB record(s) for current DBObject table according to params.
395
     *
396
     * @param bool $debug Debug mode flag.
397
     *
398
     * @return mixed DBObject, array of DBObject or null.
399
     * @throws DBCoreException If some DB or query syntax errors occurred.
400
     */
401
    public function go($debug = false) {
402
        switch ($this->dbQuery->getType()) {
403
            case (DBQueryType::SELECT):
404
                $this->dbQuery->query = "SELECT * FROM " . static::TABLE_NAME;
405
                break;
406
            case (DBQueryType::UPDATE):
407
                $this->dbQuery->query = "UPDATE " . static::TABLE_NAME . " SET ";
408
                $this->dbQuery->sqlPushValues($this->dbQuery->fields);
409
                break;
410
            case (DBQueryType::DELETE):
411
                $this->dbQuery->query = "DELETE FROM " . static::TABLE_NAME;
412
                break;
413
        }
414
415
        /*
416
         * Conditions
417
         */
418
        if ($this->isNewRecord()) {
419
            if (!empty($this->dbQuery->conditions)) {
420
                $this->dbQuery->query.= " WHERE ";
421
                $this->dbQuery->sqlPushValues($this->dbQuery->conditions, " AND ");
422
            }
423
        } else {
424
            $this->dbQuery->query.= " WHERE ";
425
            $this->dbQuery->sqlPushValues([static::ID_FIELD_NAME => $this->id]);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Asymptix\db\DBObject>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
426
        }
427
428
        /*
429
         * Order
430
         */
431
        if ($this->isNewRecord()) {
432
            if (!empty($this->dbQuery->order)) {
433
                $this->dbQuery->query.= " ORDER BY";
434
                if (is_array($this->dbQuery->order)) {
435
                    foreach ($this->dbQuery->order as $fieldName => $ord) {
436
                        $this->dbQuery->query.= " " . $fieldName . " " . $ord . ",";
437
                    }
438
                    $this->dbQuery->query = substr($this->dbQuery->query, 0, strlen($this->dbQuery->query) - 1);
439
                } elseif (is_string($this->dbQuery->order)) {
440
                    $this->dbQuery->query.= " " . $this->dbQuery->order;
441
                }
442
            }
443
        }
444
445
        /*
446
         * Limit
447
         */
448
        $count = null;
449
        if ($this->isNewRecord()) {
450
            if (!is_null($this->dbQuery->limit)) {
451
                if (Tools::isInteger($this->dbQuery->limit)) {
452
                    $this->dbQuery->query.= " LIMIT " . $this->dbQuery->limit;
453
                    $count = $this->dbQuery->limit;
454
                } elseif (is_array($this->dbQuery->limit) && count($this->dbQuery->limit) == 2) {
455
                    $offset = $this->dbQuery->limit[0];
456
                    $count = $this->dbQuery->limit[1];
457
                    if (Tools::isInteger($offset) && Tools::isInteger($count)) {
458
                        $this->dbQuery->query.= " LIMIT " . $offset . ", " . $count;
459
                    } else {
460
                        throw new DBCoreException("Invalid LIMIT param in select() method.");
461
                    }
462
                } else {
463
                    throw new DBCoreException("Invalid LIMIT param in select() method.");
464
                }
465
            }
466
        } else {
467
            $this->dbQuery->query.= " LIMIT 1";
468
            $count = 1;
469
        }
470
471
        if ($debug) {
472
            DBQuery::showQueryDebugInfo(
473
                $this->dbQuery->query,
474
                $this->dbQuery->types,
475
                $this->dbQuery->params
476
            );
477
        } else {
478
            if ($this->dbQuery->isSelector()) {
479
                $stmt = $this->dbQuery->go();
480
                if ($stmt !== false) {
481
                    $data = null;
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
482
                    if ($count !== 1) {
483
                        $data = DBCore::selectDBObjectsFromStatement($stmt, $this);
484
                    } else {
485
                        $data = DBCore::selectDBObjectFromStatement($stmt, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<Asymptix\db\DBObject>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
486
                    }
487
                    $stmt->close();
488
489
                    return $data;
490
                }
491
492
                return null;
493
            }
494
495
            return $this->dbQuery->go();
496
        }
497
498
        return null;
499
    }
500
501
    /**
502
     * Deletes DB record for current DBObject.
503
     *
504
     * @return mixed Number of affected rows (1 if some record was deleted,
505
     *            0 - if no) or FALSE if some error occurred.
506
     */
507
    public function delete() {
508
        return DBCore::deleteDBObject($this);
509
    }
510
511
    /**
512
     * Deletes DB record by ID or condition.
513
     *
514
     * @param mixed $conditions List of the conditions fields
515
     *           (fieldName => fieldValue or sqlCondition => params).
516
     *           or ID value of the record
517
     * @return DBObject Current object.
518
     */
519
    public static function _delete($conditions = []) {
520
        $ref = new \ReflectionClass(get_called_class());
521
        $dbObject = $ref->newInstance();
522
523
        if (!is_array($conditions)) { // Just record ID provided
524
            $recordId = $conditions;
525
            $conditions = [
526
                $dbObject->getIdFieldName() => $recordId
527
            ];
528
            $dbObject->initQuery(DBQueryType::DELETE, $conditions);
529
            $dbObject->dbQuery->limit = 1;
530
531
            return $dbObject;
532
        }
533
534
        return $dbObject->initQuery(DBQueryType::DELETE, $conditions);
535
    }
536
537
    /**
538
     * Returns DB table field name by it's camelcase variant.
539
     *
540
     * @param string $methodNameFragment
541
     *
542
     * @return string
543
     */
544
    protected function getFieldName($methodNameFragment) {
545
        return substr(strtolower(preg_replace("#([A-Z]{1})#", "_$1", $methodNameFragment)), 1);
546
    }
547
548
}
549