• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 17455240903

04 Sep 2025 06:22AM UTC coverage: 84.305% (-0.02%) from 84.32%
17455240903

push

github

web-flow
fix: compileOrderBy method (#9697)

3 of 3 new or added lines in 1 file covered. (100.0%)

22 existing lines in 1 file now uncovered.

20862 of 24746 relevant lines covered (84.3%)

194.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.47
/system/BaseModel.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter;
15

16
use Closure;
17
use CodeIgniter\Database\BaseConnection;
18
use CodeIgniter\Database\BaseResult;
19
use CodeIgniter\Database\Exceptions\DatabaseException;
20
use CodeIgniter\Database\Exceptions\DataException;
21
use CodeIgniter\Database\Query;
22
use CodeIgniter\DataConverter\DataConverter;
23
use CodeIgniter\Entity\Entity;
24
use CodeIgniter\Exceptions\InvalidArgumentException;
25
use CodeIgniter\Exceptions\ModelException;
26
use CodeIgniter\I18n\Time;
27
use CodeIgniter\Pager\Pager;
28
use CodeIgniter\Validation\ValidationInterface;
29
use Config\Feature;
30
use ReflectionClass;
31
use ReflectionException;
32
use ReflectionProperty;
33
use stdClass;
34

35
/**
36
 * The BaseModel class provides a number of convenient features that
37
 * makes working with a databases less painful. Extending this class
38
 * provide means of implementing various database systems
39
 *
40
 * It will:
41
 *      - simplifies pagination
42
 *      - allow specifying the return type (array, object, etc) with each call
43
 *      - automatically set and update timestamps
44
 *      - handle soft deletes
45
 *      - ensure validation is run against objects when saving items
46
 *      - process various callbacks
47
 *      - allow intermingling calls to the db connection
48
 *
49
 * @phpstan-type row_array               array<int|string, float|int|null|object|string|bool>
50
 * @phpstan-type event_data_beforeinsert array{data: row_array}
51
 * @phpstan-type event_data_afterinsert  array{id: int|string, data: row_array, result: bool}
52
 * @phpstan-type event_data_beforefind   array{id?: int|string, method: string, singleton: bool, limit?: int, offset?: int}
53
 * @phpstan-type event_data_afterfind    array{id: int|string|null|list<int|string>, data: row_array|list<row_array>|object|null, method: string, singleton: bool}
54
 * @phpstan-type event_data_beforeupdate array{id: null|list<int|string>, data: row_array}
55
 * @phpstan-type event_data_afterupdate  array{id: null|list<int|string>, data: row_array|object, result: bool}
56
 * @phpstan-type event_data_beforedelete array{id: null|list<int|string>, purge: bool}
57
 * @phpstan-type event_data_afterdelete  array{id: null|list<int|string>, data: null, purge: bool, result: bool}
58
 */
59
abstract class BaseModel
60
{
61
    /**
62
     * Pager instance.
63
     * Populated after calling $this->paginate()
64
     *
65
     * @var Pager
66
     */
67
    public $pager;
68

69
    /**
70
     * Database Connection
71
     *
72
     * @var BaseConnection
73
     */
74
    protected $db;
75

76
    /**
77
     * Last insert ID
78
     *
79
     * @var int|string
80
     */
81
    protected $insertID = 0;
82

83
    /**
84
     * The Database connection group that
85
     * should be instantiated.
86
     *
87
     * @var non-empty-string|null
88
     */
89
    protected $DBGroup;
90

91
    /**
92
     * The format that the results should be returned as.
93
     * Will be overridden if the as* methods are used.
94
     *
95
     * @var string
96
     */
97
    protected $returnType = 'array';
98

99
    /**
100
     * Used by asArray() and asObject() to provide
101
     * temporary overrides of model default.
102
     *
103
     * @var 'array'|'object'|class-string
104
     */
105
    protected $tempReturnType;
106

107
    /**
108
     * Array of column names and the type of value to cast.
109
     *
110
     * @var array<string, string> [column => type]
111
     */
112
    protected array $casts = [];
113

114
    /**
115
     * Custom convert handlers.
116
     *
117
     * @var array<string, class-string> [type => classname]
118
     */
119
    protected array $castHandlers = [];
120

121
    protected ?DataConverter $converter = null;
122

123
    /**
124
     * Determines whether the model should protect field names during
125
     * mass assignment operations such as insert() and update().
126
     *
127
     * When set to true, only the fields explicitly defined in the $allowedFields
128
     * property will be allowed for mass assignment. This helps prevent
129
     * unintended modification of database fields and improves security
130
     * by avoiding mass assignment vulnerabilities.
131
     *
132
     * @var bool
133
     */
134
    protected $protectFields = true;
135

136
    /**
137
     * An array of field names that are allowed
138
     * to be set by the user in inserts/updates.
139
     *
140
     * @var list<string>
141
     */
142
    protected $allowedFields = [];
143

144
    /**
145
     * If true, will set created_at, and updated_at
146
     * values during insert and update routines.
147
     *
148
     * @var bool
149
     */
150
    protected $useTimestamps = false;
151

152
    /**
153
     * The type of column that created_at and updated_at
154
     * are expected to.
155
     *
156
     * Allowed: 'datetime', 'date', 'int'
157
     *
158
     * @var string
159
     */
160
    protected $dateFormat = 'datetime';
161

162
    /**
163
     * The column used for insert timestamps
164
     *
165
     * @var string
166
     */
167
    protected $createdField = 'created_at';
168

169
    /**
170
     * The column used for update timestamps
171
     *
172
     * @var string
173
     */
174
    protected $updatedField = 'updated_at';
175

176
    /**
177
     * If this model should use "softDeletes" and
178
     * simply set a date when rows are deleted, or
179
     * do hard deletes.
180
     *
181
     * @var bool
182
     */
183
    protected $useSoftDeletes = false;
184

185
    /**
186
     * Used by withDeleted to override the
187
     * model's softDelete setting.
188
     *
189
     * @var bool
190
     */
191
    protected $tempUseSoftDeletes;
192

193
    /**
194
     * The column used to save soft delete state
195
     *
196
     * @var string
197
     */
198
    protected $deletedField = 'deleted_at';
199

200
    /**
201
     * Whether to allow inserting empty data.
202
     */
203
    protected bool $allowEmptyInserts = false;
204

205
    /**
206
     * Whether to update Entity's only changed data.
207
     */
208
    protected bool $updateOnlyChanged = true;
209

210
    /**
211
     * Rules used to validate data in insert(), update(), and save() methods.
212
     *
213
     * The array must match the format of data passed to the Validation
214
     * library.
215
     *
216
     * @see https://codeigniter4.github.io/userguide/models/model.html#setting-validation-rules
217
     *
218
     * @var array<string, array<string, array<string, string>|string>|string>|string
219
     */
220
    protected $validationRules = [];
221

222
    /**
223
     * Contains any custom error messages to be
224
     * used during data validation.
225
     *
226
     * @var array<string, array<string, string>>
227
     */
228
    protected $validationMessages = [];
229

230
    /**
231
     * Skip the model's validation. Used in conjunction with skipValidation()
232
     * to skip data validation for any future calls.
233
     *
234
     * @var bool
235
     */
236
    protected $skipValidation = false;
237

238
    /**
239
     * Whether rules should be removed that do not exist
240
     * in the passed data. Used in updates.
241
     *
242
     * @var bool
243
     */
244
    protected $cleanValidationRules = true;
245

246
    /**
247
     * Our validator instance.
248
     *
249
     * @var ValidationInterface|null
250
     */
251
    protected $validation;
252

253
    /*
254
     * Callbacks.
255
     *
256
     * Each array should contain the method names (within the model)
257
     * that should be called when those events are triggered.
258
     *
259
     * "Update" and "delete" methods are passed the same items that
260
     * are given to their respective method.
261
     *
262
     * "Find" methods receive the ID searched for (if present), and
263
     * 'afterFind' additionally receives the results that were found.
264
     */
265

266
    /**
267
     * Whether to trigger the defined callbacks
268
     *
269
     * @var bool
270
     */
271
    protected $allowCallbacks = true;
272

273
    /**
274
     * Used by allowCallbacks() to override the
275
     * model's allowCallbacks setting.
276
     *
277
     * @var bool
278
     */
279
    protected $tempAllowCallbacks;
280

281
    /**
282
     * Callbacks for beforeInsert
283
     *
284
     * @var list<string>
285
     */
286
    protected $beforeInsert = [];
287

288
    /**
289
     * Callbacks for afterInsert
290
     *
291
     * @var list<string>
292
     */
293
    protected $afterInsert = [];
294

295
    /**
296
     * Callbacks for beforeUpdate
297
     *
298
     * @var list<string>
299
     */
300
    protected $beforeUpdate = [];
301

302
    /**
303
     * Callbacks for afterUpdate
304
     *
305
     * @var list<string>
306
     */
307
    protected $afterUpdate = [];
308

309
    /**
310
     * Callbacks for beforeInsertBatch
311
     *
312
     * @var list<string>
313
     */
314
    protected $beforeInsertBatch = [];
315

316
    /**
317
     * Callbacks for afterInsertBatch
318
     *
319
     * @var list<string>
320
     */
321
    protected $afterInsertBatch = [];
322

323
    /**
324
     * Callbacks for beforeUpdateBatch
325
     *
326
     * @var list<string>
327
     */
328
    protected $beforeUpdateBatch = [];
329

330
    /**
331
     * Callbacks for afterUpdateBatch
332
     *
333
     * @var list<string>
334
     */
335
    protected $afterUpdateBatch = [];
336

337
    /**
338
     * Callbacks for beforeFind
339
     *
340
     * @var list<string>
341
     */
342
    protected $beforeFind = [];
343

344
    /**
345
     * Callbacks for afterFind
346
     *
347
     * @var list<string>
348
     */
349
    protected $afterFind = [];
350

351
    /**
352
     * Callbacks for beforeDelete
353
     *
354
     * @var list<string>
355
     */
356
    protected $beforeDelete = [];
357

358
    /**
359
     * Callbacks for afterDelete
360
     *
361
     * @var list<string>
362
     */
363
    protected $afterDelete = [];
364

365
    public function __construct(?ValidationInterface $validation = null)
366
    {
367
        $this->tempReturnType     = $this->returnType;
315✔
368
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
315✔
369
        $this->tempAllowCallbacks = $this->allowCallbacks;
315✔
370

371
        $this->validation = $validation;
315✔
372

373
        $this->initialize();
315✔
374
        $this->createDataConverter();
315✔
375
    }
376

377
    /**
378
     * Creates DataConverter instance.
379
     */
380
    protected function createDataConverter(): void
381
    {
382
        if ($this->useCasts()) {
315✔
383
            $this->converter = new DataConverter(
30✔
384
                $this->casts,
30✔
385
                $this->castHandlers,
30✔
386
                $this->db,
30✔
387
            );
30✔
388
        }
389
    }
390

391
    /**
392
     * Are casts used?
393
     */
394
    protected function useCasts(): bool
395
    {
396
        return $this->casts !== [];
315✔
397
    }
398

399
    /**
400
     * Initializes the instance with any additional steps.
401
     * Optionally implemented by child classes.
402
     *
403
     * @return void
404
     */
405
    protected function initialize()
406
    {
407
    }
314✔
408

409
    /**
410
     * Fetches the row of database.
411
     * This method works only with dbCalls.
412
     *
413
     * @param bool                  $singleton Single or multiple results
414
     * @param array|int|string|null $id        One primary key or an array of primary keys
415
     *
416
     * @return array|object|null The resulting row of data, or null.
417
     */
418
    abstract protected function doFind(bool $singleton, $id = null);
419

420
    /**
421
     * Fetches the column of database.
422
     * This method works only with dbCalls.
423
     *
424
     * @param string $columnName Column Name
425
     *
426
     * @return array|null The resulting row of data, or null if no data found.
427
     *
428
     * @throws DataException
429
     */
430
    abstract protected function doFindColumn(string $columnName);
431

432
    /**
433
     * Fetches all results, while optionally limiting them.
434
     * This method works only with dbCalls.
435
     *
436
     * @param int|null $limit  Limit
437
     * @param int      $offset Offset
438
     *
439
     * @return array
440
     */
441
    abstract protected function doFindAll(?int $limit = null, int $offset = 0);
442

443
    /**
444
     * Returns the first row of the result set.
445
     * This method works only with dbCalls.
446
     *
447
     * @return array|object|null
448
     */
449
    abstract protected function doFirst();
450

451
    /**
452
     * Inserts data into the current database.
453
     * This method works only with dbCalls.
454
     *
455
     * @param         array     $row Row data
456
     * @phpstan-param row_array $row
457
     *
458
     * @return bool
459
     */
460
    abstract protected function doInsert(array $row);
461

462
    /**
463
     * Compiles batch insert and runs the queries, validating each row prior.
464
     * This method works only with dbCalls.
465
     *
466
     * @param array|null $set       An associative array of insert values
467
     * @param bool|null  $escape    Whether to escape values
468
     * @param int        $batchSize The size of the batch to run
469
     * @param bool       $testing   True means only number of records is returned, false will execute the query
470
     *
471
     * @return bool|int Number of rows inserted or FALSE on failure
472
     */
473
    abstract protected function doInsertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false);
474

475
    /**
476
     * Updates a single record in the database.
477
     * This method works only with dbCalls.
478
     *
479
     * @param         array|int|string|null $id  ID
480
     * @param         array|null            $row Row data
481
     * @phpstan-param row_array|null        $row
482
     */
483
    abstract protected function doUpdate($id = null, $row = null): bool;
484

485
    /**
486
     * Compiles an update and runs the query.
487
     * This method works only with dbCalls.
488
     *
489
     * @param array|null  $set       An associative array of update values
490
     * @param string|null $index     The where key
491
     * @param int         $batchSize The size of the batch to run
492
     * @param bool        $returnSQL True means SQL is returned, false will execute the query
493
     *
494
     * @return false|int|list<string> Number of rows affected or FALSE on failure, SQL array when testMode
495
     *
496
     * @throws DatabaseException
497
     */
498
    abstract protected function doUpdateBatch(?array $set = null, ?string $index = null, int $batchSize = 100, bool $returnSQL = false);
499

500
    /**
501
     * Deletes a single record from the database where $id matches.
502
     * This method works only with dbCalls.
503
     *
504
     * @param array|int|string|null $id    The rows primary key(s)
505
     * @param bool                  $purge Allows overriding the soft deletes setting.
506
     *
507
     * @return bool|string
508
     *
509
     * @throws DatabaseException
510
     */
511
    abstract protected function doDelete($id = null, bool $purge = false);
512

513
    /**
514
     * Permanently deletes all rows that have been marked as deleted.
515
     * through soft deletes (deleted = 1).
516
     * This method works only with dbCalls.
517
     *
518
     * @return bool|string Returns a string if in test mode.
519
     */
520
    abstract protected function doPurgeDeleted();
521

522
    /**
523
     * Works with the find* methods to return only the rows that
524
     * have been deleted.
525
     * This method works only with dbCalls.
526
     *
527
     * @return void
528
     */
529
    abstract protected function doOnlyDeleted();
530

531
    /**
532
     * Compiles a replace and runs the query.
533
     * This method works only with dbCalls.
534
     *
535
     * @param row_array|null $row       Row data
536
     * @param bool           $returnSQL Set to true to return Query String
537
     *
538
     * @return BaseResult|false|Query|string
539
     */
540
    abstract protected function doReplace(?array $row = null, bool $returnSQL = false);
541

542
    /**
543
     * Grabs the last error(s) that occurred from the Database connection.
544
     * This method works only with dbCalls.
545
     *
546
     * @return array<string, string>
547
     */
548
    abstract protected function doErrors();
549

550
    /**
551
     * Public getter to return the id value using the idValue() method.
552
     * For example with SQL this will return $data->$this->primaryKey.
553
     *
554
     * @param object|row_array $row Row data
555
     *
556
     * @return array|int|string|null
557
     */
558
    abstract public function getIdValue($row);
559

560
    /**
561
     * Override countAllResults to account for soft deleted accounts.
562
     * This method works only with dbCalls.
563
     *
564
     * @param bool $reset Reset
565
     * @param bool $test  Test
566
     *
567
     * @return int|string
568
     */
569
    abstract public function countAllResults(bool $reset = true, bool $test = false);
570

571
    /**
572
     * Loops over records in batches, allowing you to operate on them.
573
     * This method works only with dbCalls.
574
     *
575
     * @param int                                          $size     Size
576
     * @param Closure(array<string, string>|object): mixed $userFunc Callback Function
577
     *
578
     * @return void
579
     *
580
     * @throws DataException
581
     */
582
    abstract public function chunk(int $size, Closure $userFunc);
583

584
    /**
585
     * Fetches the row of database.
586
     *
587
     * @param array|int|string|null $id One primary key or an array of primary keys
588
     *
589
     * @return         array|object|null                                                    The resulting row of data, or null.
590
     * @phpstan-return ($id is int|string ? row_array|object|null : list<row_array|object>)
591
     */
592
    public function find($id = null)
593
    {
594
        $singleton = is_numeric($id) || is_string($id);
58✔
595

596
        if ($this->tempAllowCallbacks) {
58✔
597
            // Call the before event and check for a return
598
            $eventData = $this->trigger('beforeFind', [
55✔
599
                'id'        => $id,
55✔
600
                'method'    => 'find',
55✔
601
                'singleton' => $singleton,
55✔
602
            ]);
55✔
603

604
            if (isset($eventData['returnData']) && $eventData['returnData'] === true) {
55✔
605
                return $eventData['data'];
3✔
606
            }
607
        }
608

609
        $eventData = [
56✔
610
            'id'        => $id,
56✔
611
            'data'      => $this->doFind($singleton, $id),
56✔
612
            'method'    => 'find',
56✔
613
            'singleton' => $singleton,
56✔
614
        ];
56✔
615

616
        if ($this->tempAllowCallbacks) {
55✔
617
            $eventData = $this->trigger('afterFind', $eventData);
52✔
618
        }
619

620
        $this->tempReturnType     = $this->returnType;
55✔
621
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
55✔
622
        $this->tempAllowCallbacks = $this->allowCallbacks;
55✔
623

624
        return $eventData['data'];
55✔
625
    }
626

627
    /**
628
     * Fetches the column of database.
629
     *
630
     * @param string $columnName Column Name
631
     *
632
     * @return array|null The resulting row of data, or null if no data found.
633
     *
634
     * @throws DataException
635
     */
636
    public function findColumn(string $columnName)
637
    {
638
        if (str_contains($columnName, ',')) {
3✔
639
            throw DataException::forFindColumnHaveMultipleColumns();
1✔
640
        }
641

642
        $resultSet = $this->doFindColumn($columnName);
2✔
643

644
        return $resultSet !== null ? array_column($resultSet, $columnName) : null;
2✔
645
    }
646

647
    /**
648
     * Fetches all results, while optionally limiting them.
649
     *
650
     * @param int $limit  Limit
651
     * @param int $offset Offset
652
     *
653
     * @return array
654
     */
655
    public function findAll(?int $limit = null, int $offset = 0)
656
    {
657
        $limitZeroAsAll = config(Feature::class)->limitZeroAsAll ?? true;
22✔
658
        if ($limitZeroAsAll) {
22✔
659
            $limit ??= 0;
22✔
660
        }
661

662
        if ($this->tempAllowCallbacks) {
22✔
663
            // Call the before event and check for a return
664
            $eventData = $this->trigger('beforeFind', [
22✔
665
                'method'    => 'findAll',
22✔
666
                'limit'     => $limit,
22✔
667
                'offset'    => $offset,
22✔
668
                'singleton' => false,
22✔
669
            ]);
22✔
670

671
            if (isset($eventData['returnData']) && $eventData['returnData'] === true) {
22✔
672
                return $eventData['data'];
1✔
673
            }
674
        }
675

676
        $eventData = [
22✔
677
            'data'      => $this->doFindAll($limit, $offset),
22✔
678
            'limit'     => $limit,
22✔
679
            'offset'    => $offset,
22✔
680
            'method'    => 'findAll',
22✔
681
            'singleton' => false,
22✔
682
        ];
22✔
683

684
        if ($this->tempAllowCallbacks) {
22✔
685
            $eventData = $this->trigger('afterFind', $eventData);
22✔
686
        }
687

688
        $this->tempReturnType     = $this->returnType;
22✔
689
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
22✔
690
        $this->tempAllowCallbacks = $this->allowCallbacks;
22✔
691

692
        return $eventData['data'];
22✔
693
    }
694

695
    /**
696
     * Returns the first row of the result set.
697
     *
698
     * @return array|object|null
699
     */
700
    public function first()
701
    {
702
        if ($this->tempAllowCallbacks) {
24✔
703
            // Call the before event and check for a return
704
            $eventData = $this->trigger('beforeFind', [
24✔
705
                'method'    => 'first',
24✔
706
                'singleton' => true,
24✔
707
            ]);
24✔
708

709
            if (isset($eventData['returnData']) && $eventData['returnData'] === true) {
24✔
710
                return $eventData['data'];
1✔
711
            }
712
        }
713

714
        $eventData = [
24✔
715
            'data'      => $this->doFirst(),
24✔
716
            'method'    => 'first',
24✔
717
            'singleton' => true,
24✔
718
        ];
24✔
719

720
        if ($this->tempAllowCallbacks) {
24✔
721
            $eventData = $this->trigger('afterFind', $eventData);
24✔
722
        }
723

724
        $this->tempReturnType     = $this->returnType;
24✔
725
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
24✔
726
        $this->tempAllowCallbacks = $this->allowCallbacks;
24✔
727

728
        return $eventData['data'];
24✔
729
    }
730

731
    /**
732
     * A convenience method that will attempt to determine whether the
733
     * data should be inserted or updated. Will work with either
734
     * an array or object. When using with custom class objects,
735
     * you must ensure that the class will provide access to the class
736
     * variables, even if through a magic method.
737
     *
738
     * @param object|row_array $row Row data
739
     *
740
     * @throws ReflectionException
741
     */
742
    public function save($row): bool
743
    {
744
        if ((array) $row === []) {
28✔
745
            return true;
1✔
746
        }
747

748
        if ($this->shouldUpdate($row)) {
27✔
749
            $response = $this->update($this->getIdValue($row), $row);
17✔
750
        } else {
751
            $response = $this->insert($row, false);
14✔
752

753
            if ($response !== false) {
13✔
754
                $response = true;
12✔
755
            }
756
        }
757

758
        return $response;
26✔
759
    }
760

761
    /**
762
     * This method is called on save to determine if entry have to be updated.
763
     * If this method returns false insert operation will be executed
764
     *
765
     * @param         array|object     $row Row data
766
     * @phpstan-param row_array|object $row
767
     */
768
    protected function shouldUpdate($row): bool
769
    {
770
        $id = $this->getIdValue($row);
27✔
771

772
        return ! ($id === null || $id === [] || $id === '');
27✔
773
    }
774

775
    /**
776
     * Returns last insert ID or 0.
777
     *
778
     * @return int|string
779
     */
780
    public function getInsertID()
781
    {
782
        return is_numeric($this->insertID) ? (int) $this->insertID : $this->insertID;
11✔
783
    }
784

785
    /**
786
     * Inserts data into the database. If an object is provided,
787
     * it will attempt to convert it to an array.
788
     *
789
     * @param object|row_array|null $row      Row data
790
     * @param bool                  $returnID Whether insert ID should be returned or not.
791
     *
792
     * @return ($returnID is true ? false|int|string : bool)
793
     *
794
     * @throws ReflectionException
795
     */
796
    public function insert($row = null, bool $returnID = true)
797
    {
798
        $this->insertID = 0;
109✔
799

800
        // Set $cleanValidationRules to false temporary.
801
        $cleanValidationRules       = $this->cleanValidationRules;
109✔
802
        $this->cleanValidationRules = false;
109✔
803

804
        $row = $this->transformDataToArray($row, 'insert');
109✔
805

806
        // Validate data before saving.
807
        if (! $this->skipValidation && ! $this->validate($row)) {
107✔
808
            // Restore $cleanValidationRules
809
            $this->cleanValidationRules = $cleanValidationRules;
19✔
810

811
            return false;
19✔
812
        }
813

814
        // Restore $cleanValidationRules
815
        $this->cleanValidationRules = $cleanValidationRules;
90✔
816

817
        // Must be called first, so we don't
818
        // strip out created_at values.
819
        $row = $this->doProtectFieldsForInsert($row);
90✔
820

821
        // doProtectFields() can further remove elements from
822
        // $row, so we need to check for empty dataset again
823
        if (! $this->allowEmptyInserts && $row === []) {
89✔
824
            throw DataException::forEmptyDataset('insert');
2✔
825
        }
826

827
        // Set created_at and updated_at with same time
828
        $date = $this->setDate();
87✔
829
        $row  = $this->setCreatedField($row, $date);
87✔
830
        $row  = $this->setUpdatedField($row, $date);
87✔
831

832
        $eventData = ['data' => $row];
87✔
833

834
        if ($this->tempAllowCallbacks) {
87✔
835
            $eventData = $this->trigger('beforeInsert', $eventData);
87✔
836
        }
837

838
        $result = $this->doInsert($eventData['data']);
86✔
839

840
        $eventData = [
85✔
841
            'id'     => $this->insertID,
85✔
842
            'data'   => $eventData['data'],
85✔
843
            'result' => $result,
85✔
844
        ];
85✔
845

846
        if ($this->tempAllowCallbacks) {
85✔
847
            // Trigger afterInsert events with the inserted data and new ID
848
            $this->trigger('afterInsert', $eventData);
85✔
849
        }
850

851
        $this->tempAllowCallbacks = $this->allowCallbacks;
85✔
852

853
        // If insertion failed, get out of here
854
        if (! $result) {
85✔
855
            return $result;
2✔
856
        }
857

858
        // otherwise return the insertID, if requested.
859
        return $returnID ? $this->insertID : $result;
83✔
860
    }
861

862
    /**
863
     * Set datetime to created field.
864
     *
865
     * @param row_array  $row
866
     * @param int|string $date timestamp or datetime string
867
     */
868
    protected function setCreatedField(array $row, $date): array
869
    {
870
        if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $row)) {
99✔
871
            $row[$this->createdField] = $date;
39✔
872
        }
873

874
        return $row;
99✔
875
    }
876

877
    /**
878
     * Set datetime to updated field.
879
     *
880
     * @param row_array  $row
881
     * @param int|string $date timestamp or datetime string
882
     */
883
    protected function setUpdatedField(array $row, $date): array
884
    {
885
        if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
114✔
886
            $row[$this->updatedField] = $date;
43✔
887
        }
888

889
        return $row;
114✔
890
    }
891

892
    /**
893
     * Compiles batch insert runs the queries, validating each row prior.
894
     *
895
     * @param list<object|row_array>|null $set       an associative array of insert values
896
     * @param bool|null                   $escape    Whether to escape values
897
     * @param int                         $batchSize The size of the batch to run
898
     * @param bool                        $testing   True means only number of records is returned, false will execute the query
899
     *
900
     * @return bool|int Number of rows inserted or FALSE on failure
901
     *
902
     * @throws ReflectionException
903
     */
904
    public function insertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false)
905
    {
906
        // Set $cleanValidationRules to false temporary.
907
        $cleanValidationRules       = $this->cleanValidationRules;
14✔
908
        $this->cleanValidationRules = false;
14✔
909

910
        if (is_array($set)) {
14✔
911
            foreach ($set as &$row) {
14✔
912
                $row = $this->transformDataRowToArray($row);
14✔
913

914
                // Validate every row.
915
                if (! $this->skipValidation && ! $this->validate($row)) {
14✔
916
                    // Restore $cleanValidationRules
917
                    $this->cleanValidationRules = $cleanValidationRules;
3✔
918

919
                    return false;
3✔
920
                }
921

922
                // Must be called first so we don't
923
                // strip out created_at values.
924
                $row = $this->doProtectFieldsForInsert($row);
11✔
925

926
                // Set created_at and updated_at with same time
927
                $date = $this->setDate();
11✔
928
                $row  = $this->setCreatedField($row, $date);
11✔
929
                $row  = $this->setUpdatedField($row, $date);
11✔
930
            }
931
        }
932

933
        // Restore $cleanValidationRules
934
        $this->cleanValidationRules = $cleanValidationRules;
11✔
935

936
        $eventData = ['data' => $set];
11✔
937

938
        if ($this->tempAllowCallbacks) {
11✔
939
            $eventData = $this->trigger('beforeInsertBatch', $eventData);
11✔
940
        }
941

942
        $result = $this->doInsertBatch($eventData['data'], $escape, $batchSize, $testing);
11✔
943

944
        $eventData = [
11✔
945
            'data'   => $eventData['data'],
11✔
946
            'result' => $result,
11✔
947
        ];
11✔
948

949
        if ($this->tempAllowCallbacks) {
11✔
950
            // Trigger afterInsert events with the inserted data and new ID
951
            $this->trigger('afterInsertBatch', $eventData);
11✔
952
        }
953

954
        $this->tempAllowCallbacks = $this->allowCallbacks;
11✔
955

956
        return $result;
11✔
957
    }
958

959
    /**
960
     * Updates a single record in the database. If an object is provided,
961
     * it will attempt to convert it into an array.
962
     *
963
     * @param         array|int|string|null $id
964
     * @param         array|object|null     $row Row data
965
     * @phpstan-param row_array|object|null $row
966
     *
967
     * @throws ReflectionException
968
     */
969
    public function update($id = null, $row = null): bool
970
    {
971
        if (is_bool($id)) {
46✔
972
            throw new InvalidArgumentException('update(): argument #1 ($id) should not be boolean.');
1✔
973
        }
974

975
        if (is_numeric($id) || is_string($id)) {
45✔
976
            $id = [$id];
38✔
977
        }
978

979
        $row = $this->transformDataToArray($row, 'update');
45✔
980

981
        // Validate data before saving.
982
        if (! $this->skipValidation && ! $this->validate($row)) {
43✔
983
            return false;
4✔
984
        }
985

986
        // Must be called first, so we don't
987
        // strip out updated_at values.
988
        $row = $this->doProtectFields($row);
39✔
989

990
        // doProtectFields() can further remove elements from
991
        // $row, so we need to check for empty dataset again
992
        if ($row === []) {
39✔
993
            throw DataException::forEmptyDataset('update');
2✔
994
        }
995

996
        $row = $this->setUpdatedField($row, $this->setDate());
37✔
997

998
        $eventData = [
37✔
999
            'id'   => $id,
37✔
1000
            'data' => $row,
37✔
1001
        ];
37✔
1002

1003
        if ($this->tempAllowCallbacks) {
37✔
1004
            $eventData = $this->trigger('beforeUpdate', $eventData);
37✔
1005
        }
1006

1007
        $eventData = [
37✔
1008
            'id'     => $id,
37✔
1009
            'data'   => $eventData['data'],
37✔
1010
            'result' => $this->doUpdate($id, $eventData['data']),
37✔
1011
        ];
37✔
1012

1013
        if ($this->tempAllowCallbacks) {
36✔
1014
            $this->trigger('afterUpdate', $eventData);
36✔
1015
        }
1016

1017
        $this->tempAllowCallbacks = $this->allowCallbacks;
36✔
1018

1019
        return $eventData['result'];
36✔
1020
    }
1021

1022
    /**
1023
     * Compiles an update and runs the query.
1024
     *
1025
     * @param list<object|row_array>|null $set       an associative array of insert values
1026
     * @param string|null                 $index     The where key
1027
     * @param int                         $batchSize The size of the batch to run
1028
     * @param bool                        $returnSQL True means SQL is returned, false will execute the query
1029
     *
1030
     * @return false|int|list<string> Number of rows affected or FALSE on failure, SQL array when testMode
1031
     *
1032
     * @throws DatabaseException
1033
     * @throws ReflectionException
1034
     */
1035
    public function updateBatch(?array $set = null, ?string $index = null, int $batchSize = 100, bool $returnSQL = false)
1036
    {
1037
        if (is_array($set)) {
6✔
1038
            foreach ($set as &$row) {
6✔
1039
                $row = $this->transformDataRowToArray($row);
6✔
1040

1041
                // Validate data before saving.
1042
                if (! $this->skipValidation && ! $this->validate($row)) {
6✔
1043
                    return false;
1✔
1044
                }
1045

1046
                // Save updateIndex for later
1047
                $updateIndex = $row[$index] ?? null;
5✔
1048

1049
                if ($updateIndex === null) {
5✔
1050
                    throw new InvalidArgumentException(
1✔
1051
                        'The index ("' . $index . '") for updateBatch() is missing in the data: '
1✔
1052
                        . json_encode($row),
1✔
1053
                    );
1✔
1054
                }
1055

1056
                // Must be called first so we don't
1057
                // strip out updated_at values.
1058
                $row = $this->doProtectFields($row);
4✔
1059

1060
                // Restore updateIndex value in case it was wiped out
1061
                $row[$index] = $updateIndex;
4✔
1062

1063
                $row = $this->setUpdatedField($row, $this->setDate());
4✔
1064
            }
1065
        }
1066

1067
        $eventData = ['data' => $set];
4✔
1068

1069
        if ($this->tempAllowCallbacks) {
4✔
1070
            $eventData = $this->trigger('beforeUpdateBatch', $eventData);
4✔
1071
        }
1072

1073
        $result = $this->doUpdateBatch($eventData['data'], $index, $batchSize, $returnSQL);
4✔
1074

1075
        $eventData = [
4✔
1076
            'data'   => $eventData['data'],
4✔
1077
            'result' => $result,
4✔
1078
        ];
4✔
1079

1080
        if ($this->tempAllowCallbacks) {
4✔
1081
            // Trigger afterInsert events with the inserted data and new ID
1082
            $this->trigger('afterUpdateBatch', $eventData);
4✔
1083
        }
1084

1085
        $this->tempAllowCallbacks = $this->allowCallbacks;
4✔
1086

1087
        return $result;
4✔
1088
    }
1089

1090
    /**
1091
     * Deletes a single record from the database where $id matches.
1092
     *
1093
     * @param int|list<int|string>|string|null $id    The rows primary key(s)
1094
     * @param bool                             $purge Allows overriding the soft deletes setting.
1095
     *
1096
     * @return BaseResult|bool
1097
     *
1098
     * @throws DatabaseException
1099
     */
1100
    public function delete($id = null, bool $purge = false)
1101
    {
1102
        if (is_bool($id)) {
40✔
UNCOV
1103
            throw new InvalidArgumentException('delete(): argument #1 ($id) should not be boolean.');
×
1104
        }
1105

1106
        if (! in_array($id, [null, 0, '0'], true) && (is_numeric($id) || is_string($id))) {
40✔
1107
            $id = [$id];
20✔
1108
        }
1109

1110
        $eventData = [
40✔
1111
            'id'    => $id,
40✔
1112
            'purge' => $purge,
40✔
1113
        ];
40✔
1114

1115
        if ($this->tempAllowCallbacks) {
40✔
1116
            $this->trigger('beforeDelete', $eventData);
39✔
1117
        }
1118

1119
        $eventData = [
40✔
1120
            'id'     => $id,
40✔
1121
            'data'   => null,
40✔
1122
            'purge'  => $purge,
40✔
1123
            'result' => $this->doDelete($id, $purge),
40✔
1124
        ];
40✔
1125

1126
        if ($this->tempAllowCallbacks) {
27✔
1127
            $this->trigger('afterDelete', $eventData);
26✔
1128
        }
1129

1130
        $this->tempAllowCallbacks = $this->allowCallbacks;
27✔
1131

1132
        return $eventData['result'];
27✔
1133
    }
1134

1135
    /**
1136
     * Permanently deletes all rows that have been marked as deleted
1137
     * through soft deletes (deleted = 1).
1138
     *
1139
     * @return bool|string Returns a string if in test mode.
1140
     */
1141
    public function purgeDeleted()
1142
    {
1143
        if (! $this->useSoftDeletes) {
2✔
1144
            return true;
1✔
1145
        }
1146

1147
        return $this->doPurgeDeleted();
1✔
1148
    }
1149

1150
    /**
1151
     * Sets $useSoftDeletes value so that we can temporarily override
1152
     * the soft deletes settings. Can be used for all find* methods.
1153
     *
1154
     * @param bool $val Value
1155
     *
1156
     * @return $this
1157
     */
1158
    public function withDeleted(bool $val = true)
1159
    {
1160
        $this->tempUseSoftDeletes = ! $val;
23✔
1161

1162
        return $this;
23✔
1163
    }
1164

1165
    /**
1166
     * Works with the find* methods to return only the rows that
1167
     * have been deleted.
1168
     *
1169
     * @return $this
1170
     */
1171
    public function onlyDeleted()
1172
    {
1173
        $this->tempUseSoftDeletes = false;
1✔
1174
        $this->doOnlyDeleted();
1✔
1175

1176
        return $this;
1✔
1177
    }
1178

1179
    /**
1180
     * Compiles a replace and runs the query.
1181
     *
1182
     * @param row_array|null $row       Row data
1183
     * @param bool           $returnSQL Set to true to return Query String
1184
     *
1185
     * @return BaseResult|false|Query|string
1186
     */
1187
    public function replace(?array $row = null, bool $returnSQL = false)
1188
    {
1189
        // Validate data before saving.
1190
        if (($row !== null) && ! $this->skipValidation && ! $this->validate($row)) {
3✔
1191
            return false;
1✔
1192
        }
1193

1194
        $row = (array) $row;
2✔
1195
        $row = $this->setCreatedField($row, $this->setDate());
2✔
1196
        $row = $this->setUpdatedField($row, $this->setDate());
2✔
1197

1198
        return $this->doReplace($row, $returnSQL);
2✔
1199
    }
1200

1201
    /**
1202
     * Grabs the last error(s) that occurred. If data was validated,
1203
     * it will first check for errors there, otherwise will try to
1204
     * grab the last error from the Database connection.
1205
     *
1206
     * The return array should be in the following format:
1207
     *  ['source' => 'message']
1208
     *
1209
     * @param bool $forceDB Always grab the db error, not validation
1210
     *
1211
     * @return array<string, string>
1212
     */
1213
    public function errors(bool $forceDB = false)
1214
    {
1215
        if ($this->validation === null) {
24✔
UNCOV
1216
            return $this->doErrors();
×
1217
        }
1218

1219
        // Do we have validation errors?
1220
        if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors()) !== []) {
24✔
1221
            return $errors;
22✔
1222
        }
1223

1224
        return $this->doErrors();
2✔
1225
    }
1226

1227
    /**
1228
     * Works with Pager to get the size and offset parameters.
1229
     * Expects a GET variable (?page=2) that specifies the page of results
1230
     * to display.
1231
     *
1232
     * @param int|null $perPage Items per page
1233
     * @param string   $group   Will be used by the pagination library to identify a unique pagination set.
1234
     * @param int|null $page    Optional page number (useful when the page number is provided in different way)
1235
     * @param int      $segment Optional URI segment number (if page number is provided by URI segment)
1236
     *
1237
     * @return array|null
1238
     */
1239
    public function paginate(?int $perPage = null, string $group = 'default', ?int $page = null, int $segment = 0)
1240
    {
1241
        // Since multiple models may use the Pager, the Pager must be shared.
1242
        $pager = service('pager');
8✔
1243

1244
        if ($segment !== 0) {
8✔
1245
            $pager->setSegment($segment, $group);
×
1246
        }
1247

1248
        $page = $page >= 1 ? $page : $pager->getCurrentPage($group);
8✔
1249
        // Store it in the Pager library, so it can be paginated in the views.
1250
        $this->pager = $pager->store($group, $page, $perPage, $this->countAllResults(false), $segment);
8✔
1251
        $perPage     = $this->pager->getPerPage($group);
8✔
1252
        $offset      = ($pager->getCurrentPage($group) - 1) * $perPage;
8✔
1253

1254
        return $this->findAll($perPage, $offset);
8✔
1255
    }
1256

1257
    /**
1258
     * It could be used when you have to change default or override current allowed fields.
1259
     *
1260
     * @param array $allowedFields Array with names of fields
1261
     *
1262
     * @return $this
1263
     */
1264
    public function setAllowedFields(array $allowedFields)
1265
    {
1266
        $this->allowedFields = $allowedFields;
10✔
1267

1268
        return $this;
10✔
1269
    }
1270

1271
    /**
1272
     * Sets whether or not we should whitelist data set during
1273
     * updates or inserts against $this->availableFields.
1274
     *
1275
     * @param bool $protect Value
1276
     *
1277
     * @return $this
1278
     */
1279
    public function protect(bool $protect = true)
1280
    {
1281
        $this->protectFields = $protect;
12✔
1282

1283
        return $this;
12✔
1284
    }
1285

1286
    /**
1287
     * Ensures that only the fields that are allowed to be updated are
1288
     * in the data array.
1289
     *
1290
     * @used-by update() to protect against mass assignment vulnerabilities.
1291
     * @used-by updateBatch() to protect against mass assignment vulnerabilities.
1292
     *
1293
     * @param         array     $row Row data
1294
     * @phpstan-param row_array $row
1295
     *
1296
     * @throws DataException
1297
     */
1298
    protected function doProtectFields(array $row): array
1299
    {
1300
        if (! $this->protectFields) {
43✔
1301
            return $row;
2✔
1302
        }
1303

1304
        if ($this->allowedFields === []) {
41✔
UNCOV
1305
            throw DataException::forInvalidAllowedFields(static::class);
×
1306
        }
1307

1308
        foreach (array_keys($row) as $key) {
41✔
1309
            if (! in_array($key, $this->allowedFields, true)) {
41✔
1310
                unset($row[$key]);
23✔
1311
            }
1312
        }
1313

1314
        return $row;
41✔
1315
    }
1316

1317
    /**
1318
     * Ensures that only the fields that are allowed to be inserted are in
1319
     * the data array.
1320
     *
1321
     * @used-by insert() to protect against mass assignment vulnerabilities.
1322
     * @used-by insertBatch() to protect against mass assignment vulnerabilities.
1323
     *
1324
     * @param         array     $row Row data
1325
     * @phpstan-param row_array $row
1326
     *
1327
     * @throws DataException
1328
     */
1329
    protected function doProtectFieldsForInsert(array $row): array
1330
    {
UNCOV
1331
        return $this->doProtectFields($row);
×
1332
    }
1333

1334
    /**
1335
     * Sets the date or current date if null value is passed.
1336
     *
1337
     * @param int|null $userData An optional PHP timestamp to be converted.
1338
     *
1339
     * @return int|string
1340
     *
1341
     * @throws ModelException
1342
     */
1343
    protected function setDate(?int $userData = null)
1344
    {
1345
        $currentDate = $userData ?? Time::now()->getTimestamp();
133✔
1346

1347
        return $this->intToDate($currentDate);
133✔
1348
    }
1349

1350
    /**
1351
     * A utility function to allow child models to use the type of
1352
     * date/time format that they prefer. This is primarily used for
1353
     * setting created_at, updated_at and deleted_at values, but can be
1354
     * used by inheriting classes.
1355
     *
1356
     * The available time formats are:
1357
     *  - 'int'      - Stores the date as an integer timestamp
1358
     *  - 'datetime' - Stores the data in the SQL datetime format
1359
     *  - 'date'     - Stores the date (only) in the SQL date format.
1360
     *
1361
     * @param int $value value
1362
     *
1363
     * @return int|string
1364
     *
1365
     * @throws ModelException
1366
     */
1367
    protected function intToDate(int $value)
1368
    {
1369
        return match ($this->dateFormat) {
133✔
1370
            'int'      => $value,
36✔
1371
            'datetime' => date($this->db->dateFormat['datetime'], $value),
95✔
1372
            'date'     => date($this->db->dateFormat['date'], $value),
1✔
1373
            default    => throw ModelException::forNoDateFormat(static::class),
133✔
1374
        };
133✔
1375
    }
1376

1377
    /**
1378
     * Converts Time value to string using $this->dateFormat.
1379
     *
1380
     * The available time formats are:
1381
     *  - 'int'      - Stores the date as an integer timestamp
1382
     *  - 'datetime' - Stores the data in the SQL datetime format
1383
     *  - 'date'     - Stores the date (only) in the SQL date format.
1384
     *
1385
     * @param Time $value value
1386
     *
1387
     * @return int|string
1388
     */
1389
    protected function timeToDate(Time $value)
1390
    {
1391
        return match ($this->dateFormat) {
5✔
1392
            'datetime' => $value->format($this->db->dateFormat['datetime']),
3✔
1393
            'date'     => $value->format($this->db->dateFormat['date']),
1✔
1394
            'int'      => $value->getTimestamp(),
1✔
1395
            default    => (string) $value,
5✔
1396
        };
5✔
1397
    }
1398

1399
    /**
1400
     * Set the value of the skipValidation flag.
1401
     *
1402
     * @param bool $skip Value
1403
     *
1404
     * @return $this
1405
     */
1406
    public function skipValidation(bool $skip = true)
1407
    {
1408
        $this->skipValidation = $skip;
2✔
1409

1410
        return $this;
2✔
1411
    }
1412

1413
    /**
1414
     * Allows to set (and reset) validation messages.
1415
     * It could be used when you have to change default or override current validate messages.
1416
     *
1417
     * @param array $validationMessages Value
1418
     *
1419
     * @return $this
1420
     */
1421
    public function setValidationMessages(array $validationMessages)
1422
    {
UNCOV
1423
        $this->validationMessages = $validationMessages;
×
1424

UNCOV
1425
        return $this;
×
1426
    }
1427

1428
    /**
1429
     * Allows to set field wise validation message.
1430
     * It could be used when you have to change default or override current validate messages.
1431
     *
1432
     * @param string $field         Field Name
1433
     * @param array  $fieldMessages Validation messages
1434
     *
1435
     * @return $this
1436
     */
1437
    public function setValidationMessage(string $field, array $fieldMessages)
1438
    {
1439
        $this->validationMessages[$field] = $fieldMessages;
2✔
1440

1441
        return $this;
2✔
1442
    }
1443

1444
    /**
1445
     * Allows to set (and reset) validation rules.
1446
     * It could be used when you have to change default or override current validate rules.
1447
     *
1448
     * @param array<string, array<string, array<string, string>|string>|string> $validationRules Value
1449
     *
1450
     * @return $this
1451
     */
1452
    public function setValidationRules(array $validationRules)
1453
    {
1454
        $this->validationRules = $validationRules;
2✔
1455

1456
        return $this;
2✔
1457
    }
1458

1459
    /**
1460
     * Allows to set field wise validation rules.
1461
     * It could be used when you have to change default or override current validate rules.
1462
     *
1463
     * @param string       $field      Field Name
1464
     * @param array|string $fieldRules Validation rules
1465
     *
1466
     * @return $this
1467
     */
1468
    public function setValidationRule(string $field, $fieldRules)
1469
    {
1470
        $rules = $this->validationRules;
2✔
1471

1472
        // ValidationRules can be either a string, which is the group name,
1473
        // or an array of rules.
1474
        if (is_string($rules)) {
2✔
1475
            $this->ensureValidation();
1✔
1476

1477
            [$rules, $customErrors] = $this->validation->loadRuleGroup($rules);
1✔
1478

1479
            $this->validationRules = $rules;
1✔
1480
            $this->validationMessages += $customErrors;
1✔
1481
        }
1482

1483
        $this->validationRules[$field] = $fieldRules;
2✔
1484

1485
        return $this;
2✔
1486
    }
1487

1488
    /**
1489
     * Should validation rules be removed before saving?
1490
     * Most handy when doing updates.
1491
     *
1492
     * @param bool $choice Value
1493
     *
1494
     * @return $this
1495
     */
1496
    public function cleanRules(bool $choice = false)
1497
    {
1498
        $this->cleanValidationRules = $choice;
2✔
1499

1500
        return $this;
2✔
1501
    }
1502

1503
    /**
1504
     * Validate the row data against the validation rules (or the validation group)
1505
     * specified in the class property, $validationRules.
1506
     *
1507
     * @param         array|object     $row Row data
1508
     * @phpstan-param row_array|object $row
1509
     */
1510
    public function validate($row): bool
1511
    {
1512
        if ($this->skipValidation) {
151✔
UNCOV
1513
            return true;
×
1514
        }
1515

1516
        $rules = $this->getValidationRules();
151✔
1517

1518
        if ($rules === []) {
151✔
1519
            return true;
105✔
1520
        }
1521

1522
        // Validation requires array, so cast away.
1523
        if (is_object($row)) {
46✔
1524
            $row = (array) $row;
2✔
1525
        }
1526

1527
        if ($row === []) {
46✔
UNCOV
1528
            return true;
×
1529
        }
1530

1531
        $rules = $this->cleanValidationRules ? $this->cleanValidationRules($rules, $row) : $rules;
46✔
1532

1533
        // If no data existed that needs validation
1534
        // our job is done here.
1535
        if ($rules === []) {
46✔
1536
            return true;
2✔
1537
        }
1538

1539
        $this->ensureValidation();
44✔
1540

1541
        $this->validation->reset()->setRules($rules, $this->validationMessages);
44✔
1542

1543
        return $this->validation->run($row, null, $this->DBGroup);
44✔
1544
    }
1545

1546
    /**
1547
     * Returns the model's defined validation rules so that they
1548
     * can be used elsewhere, if needed.
1549
     *
1550
     * @param array $options Options
1551
     */
1552
    public function getValidationRules(array $options = []): array
1553
    {
1554
        $rules = $this->validationRules;
153✔
1555

1556
        // ValidationRules can be either a string, which is the group name,
1557
        // or an array of rules.
1558
        if (is_string($rules)) {
153✔
1559
            $this->ensureValidation();
13✔
1560

1561
            [$rules, $customErrors] = $this->validation->loadRuleGroup($rules);
13✔
1562

1563
            $this->validationMessages += $customErrors;
13✔
1564
        }
1565

1566
        if (isset($options['except'])) {
153✔
UNCOV
1567
            $rules = array_diff_key($rules, array_flip($options['except']));
×
1568
        } elseif (isset($options['only'])) {
153✔
UNCOV
1569
            $rules = array_intersect_key($rules, array_flip($options['only']));
×
1570
        }
1571

1572
        return $rules;
153✔
1573
    }
1574

1575
    protected function ensureValidation(): void
1576
    {
1577
        if ($this->validation === null) {
45✔
1578
            $this->validation = service('validation', null, false);
29✔
1579
        }
1580
    }
1581

1582
    /**
1583
     * Returns the model's validation messages, so they
1584
     * can be used elsewhere, if needed.
1585
     */
1586
    public function getValidationMessages(): array
1587
    {
1588
        return $this->validationMessages;
2✔
1589
    }
1590

1591
    /**
1592
     * Removes any rules that apply to fields that have not been set
1593
     * currently so that rules don't block updating when only updating
1594
     * a partial row.
1595
     *
1596
     * @param         array     $rules Array containing field name and rule
1597
     * @param         array     $row   Row data (@TODO Remove null in param type)
1598
     * @phpstan-param row_array $row
1599
     */
1600
    protected function cleanValidationRules(array $rules, ?array $row = null): array
1601
    {
1602
        if ($row === null || $row === []) {
21✔
1603
            return [];
2✔
1604
        }
1605

1606
        foreach (array_keys($rules) as $field) {
19✔
1607
            if (! array_key_exists($field, $row)) {
19✔
1608
                unset($rules[$field]);
8✔
1609
            }
1610
        }
1611

1612
        return $rules;
19✔
1613
    }
1614

1615
    /**
1616
     * Sets $tempAllowCallbacks value so that we can temporarily override
1617
     * the setting. Resets after the next method that uses triggers.
1618
     *
1619
     * @param bool $val value
1620
     *
1621
     * @return $this
1622
     */
1623
    public function allowCallbacks(bool $val = true)
1624
    {
1625
        $this->tempAllowCallbacks = $val;
3✔
1626

1627
        return $this;
3✔
1628
    }
1629

1630
    /**
1631
     * A simple event trigger for Model Events that allows additional
1632
     * data manipulation within the model. Specifically intended for
1633
     * usage by child models this can be used to format data,
1634
     * save/load related classes, etc.
1635
     *
1636
     * It is the responsibility of the callback methods to return
1637
     * the data itself.
1638
     *
1639
     * Each $eventData array MUST have a 'data' key with the relevant
1640
     * data for callback methods (like an array of key/value pairs to insert
1641
     * or update, an array of results, etc.)
1642
     *
1643
     * If callbacks are not allowed then returns $eventData immediately.
1644
     *
1645
     * @param string $event     Event
1646
     * @param array  $eventData Event Data
1647
     *
1648
     * @return array
1649
     *
1650
     * @throws DataException
1651
     */
1652
    protected function trigger(string $event, array $eventData)
1653
    {
1654
        // Ensure it's a valid event
1655
        if (! isset($this->{$event}) || $this->{$event} === []) {
193✔
1656
            return $eventData;
178✔
1657
        }
1658

1659
        foreach ($this->{$event} as $callback) {
15✔
1660
            if (! method_exists($this, $callback)) {
15✔
1661
                throw DataException::forInvalidMethodTriggered($callback);
1✔
1662
            }
1663

1664
            $eventData = $this->{$callback}($eventData);
14✔
1665
        }
1666

1667
        return $eventData;
14✔
1668
    }
1669

1670
    /**
1671
     * If the model is using casts, this will convert the data
1672
     * in $row according to the rules defined in `$casts`.
1673
     *
1674
     * @param object|row_array|null $row Row data
1675
     *
1676
     * @return object|row_array|null Converted row data
1677
     *
1678
     * @used-by insertBatch()
1679
     * @used-by updateBatch()
1680
     *
1681
     * @throws ReflectionException
1682
     * @deprecated Since 4.6.4, temporary solution - will be removed in 4.7
1683
     */
1684
    protected function transformDataRowToArray(array|object|null $row): array|object|null
1685
    {
1686
        // If casts are used, convert the data first
1687
        if ($this->useCasts()) {
20✔
1688
            if (is_array($row)) {
2✔
1689
                $row = $this->converter->toDataSource($row);
2✔
UNCOV
1690
            } elseif ($row instanceof stdClass) {
×
UNCOV
1691
                $row = (array) $row;
×
UNCOV
1692
                $row = $this->converter->toDataSource($row);
×
UNCOV
1693
            } elseif ($row instanceof Entity) {
×
UNCOV
1694
                $row = $this->converter->extract($row);
×
UNCOV
1695
            } elseif (is_object($row)) {
×
UNCOV
1696
                $row = $this->converter->extract($row);
×
1697
            }
1698
        } elseif (is_object($row) && ! $row instanceof stdClass) {
18✔
1699
            // If $row is using a custom class with public or protected
1700
            // properties representing the collection elements, we need to grab
1701
            // them as an array.
1702
            $row = $this->objectToArray($row, false, true);
2✔
1703
        }
1704

1705
        // If it's still a stdClass, go ahead and convert to
1706
        // an array so doProtectFields and other model methods
1707
        // don't have to do special checks.
1708
        if (is_object($row)) {
20✔
UNCOV
1709
            $row = (array) $row;
×
1710
        }
1711

1712
        // Convert any Time instances to appropriate $dateFormat
1713
        return $this->timeToString($row);
20✔
1714
    }
1715

1716
    /**
1717
     * Sets the return type of the results to be as an associative array.
1718
     *
1719
     * @return $this
1720
     */
1721
    public function asArray()
1722
    {
1723
        $this->tempReturnType = 'array';
31✔
1724

1725
        return $this;
31✔
1726
    }
1727

1728
    /**
1729
     * Sets the return type to be of the specified type of object.
1730
     * Defaults to a simple object, but can be any class that has
1731
     * class vars with the same name as the collection columns,
1732
     * or at least allows them to be created.
1733
     *
1734
     * @param 'object'|class-string $class Class Name
1735
     *
1736
     * @return $this
1737
     */
1738
    public function asObject(string $class = 'object')
1739
    {
1740
        $this->tempReturnType = $class;
18✔
1741

1742
        return $this;
18✔
1743
    }
1744

1745
    /**
1746
     * Takes a class and returns an array of its public and protected
1747
     * properties as an array suitable for use in creates and updates.
1748
     * This method uses objectToRawArray() internally and does conversion
1749
     * to string on all Time instances
1750
     *
1751
     * @param object $object      Object
1752
     * @param bool   $onlyChanged Only Changed Property
1753
     * @param bool   $recursive   If true, inner entities will be cast as array as well
1754
     *
1755
     * @return array<string, mixed>
1756
     *
1757
     * @throws ReflectionException
1758
     */
1759
    protected function objectToArray($object, bool $onlyChanged = true, bool $recursive = false): array
1760
    {
1761
        $properties = $this->objectToRawArray($object, $onlyChanged, $recursive);
24✔
1762

1763
        // Convert any Time instances to appropriate $dateFormat
1764
        return $this->timeToString($properties);
24✔
1765
    }
1766

1767
    /**
1768
     * Convert any Time instances to appropriate $dateFormat.
1769
     *
1770
     * @param array<string, mixed> $properties
1771
     *
1772
     * @return array<string, mixed>
1773
     */
1774
    protected function timeToString(array $properties): array
1775
    {
1776
        if ($properties === []) {
142✔
1777
            return [];
1✔
1778
        }
1779

1780
        return array_map(function ($value) {
141✔
1781
            if ($value instanceof Time) {
141✔
1782
                return $this->timeToDate($value);
5✔
1783
            }
1784

1785
            return $value;
141✔
1786
        }, $properties);
141✔
1787
    }
1788

1789
    /**
1790
     * Takes a class and returns an array of its public and protected
1791
     * properties as an array with raw values.
1792
     *
1793
     * @param object $object      Object
1794
     * @param bool   $onlyChanged Only Changed Property
1795
     * @param bool   $recursive   If true, inner entities will be casted as array as well
1796
     *
1797
     * @return array<string, mixed> Array with raw values.
1798
     *
1799
     * @throws ReflectionException
1800
     */
1801
    protected function objectToRawArray($object, bool $onlyChanged = true, bool $recursive = false): array
1802
    {
1803
        // Entity::toRawArray() returns array.
1804
        if (method_exists($object, 'toRawArray')) {
24✔
1805
            $properties = $object->toRawArray($onlyChanged, $recursive);
22✔
1806
        } else {
1807
            $mirror = new ReflectionClass($object);
2✔
1808
            $props  = $mirror->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
2✔
1809

1810
            $properties = [];
2✔
1811

1812
            // Loop over each property,
1813
            // saving the name/value in a new array we can return.
1814
            foreach ($props as $prop) {
2✔
1815
                $properties[$prop->getName()] = $prop->getValue($object);
2✔
1816
            }
1817
        }
1818

1819
        return $properties;
24✔
1820
    }
1821

1822
    /**
1823
     * Transform data to array.
1824
     *
1825
     * @param object|row_array|null $row  Row data
1826
     * @param string                $type Type of data (insert|update)
1827
     *
1828
     * @throws DataException
1829
     * @throws InvalidArgumentException
1830
     * @throws ReflectionException
1831
     *
1832
     * @used-by insert()
1833
     * @used-by update()
1834
     */
1835
    protected function transformDataToArray($row, string $type): array
1836
    {
1837
        if (! in_array($type, ['insert', 'update'], true)) {
126✔
1838
            throw new InvalidArgumentException(sprintf('Invalid type "%s" used upon transforming data to array.', $type));
1✔
1839
        }
1840

1841
        if (! $this->allowEmptyInserts && ($row === null || (array) $row === [])) {
125✔
1842
            throw DataException::forEmptyDataset($type);
6✔
1843
        }
1844

1845
        // If it validates with entire rules, all fields are needed.
1846
        if ($this->skipValidation === false && $this->cleanValidationRules === false) {
122✔
1847
            $onlyChanged = false;
109✔
1848
        } else {
1849
            $onlyChanged = ($type === 'update' && $this->updateOnlyChanged);
42✔
1850
        }
1851

1852
        if ($this->useCasts()) {
122✔
1853
            if (is_array($row)) {
25✔
1854
                $row = $this->converter->toDataSource($row);
25✔
1855
            } elseif ($row instanceof stdClass) {
7✔
1856
                $row = (array) $row;
3✔
1857
                $row = $this->converter->toDataSource($row);
3✔
1858
            } elseif ($row instanceof Entity) {
4✔
1859
                $row = $this->converter->extract($row, $onlyChanged);
2✔
1860
            } elseif (is_object($row)) {
2✔
1861
                $row = $this->converter->extract($row, $onlyChanged);
2✔
1862
            }
1863
        }
1864
        // If $row is using a custom class with public or protected
1865
        // properties representing the collection elements, we need to grab
1866
        // them as an array.
1867
        elseif (is_object($row) && ! $row instanceof stdClass) {
97✔
1868
            $row = $this->objectToArray($row, $onlyChanged, true);
21✔
1869
        }
1870

1871
        // If it's still a stdClass, go ahead and convert to
1872
        // an array so doProtectFields and other model methods
1873
        // don't have to do special checks.
1874
        if (is_object($row)) {
122✔
1875
            $row = (array) $row;
13✔
1876
        }
1877

1878
        // If it's still empty here, means $row is no change or is empty object
1879
        if (! $this->allowEmptyInserts && ($row === null || $row === [])) {
122✔
UNCOV
1880
            throw DataException::forEmptyDataset($type);
×
1881
        }
1882

1883
        // Convert any Time instances to appropriate $dateFormat
1884
        return $this->timeToString($row);
122✔
1885
    }
1886

1887
    /**
1888
     * Provides the db connection and model's properties.
1889
     *
1890
     * @param string $name Name
1891
     *
1892
     * @return array|bool|float|int|object|string|null
1893
     */
1894
    public function __get(string $name)
1895
    {
1896
        if (property_exists($this, $name)) {
50✔
1897
            return $this->{$name};
50✔
1898
        }
1899

1900
        return $this->db->{$name} ?? null;
1✔
1901
    }
1902

1903
    /**
1904
     * Checks for the existence of properties across this model, and db connection.
1905
     *
1906
     * @param string $name Name
1907
     */
1908
    public function __isset(string $name): bool
1909
    {
1910
        if (property_exists($this, $name)) {
50✔
1911
            return true;
50✔
1912
        }
1913

1914
        return isset($this->db->{$name});
1✔
1915
    }
1916

1917
    /**
1918
     * Provides direct access to method in the database connection.
1919
     *
1920
     * @param string $name   Name
1921
     * @param array  $params Params
1922
     *
1923
     * @return $this|null
1924
     */
1925
    public function __call(string $name, array $params)
1926
    {
UNCOV
1927
        if (method_exists($this->db, $name)) {
×
UNCOV
1928
            return $this->db->{$name}(...$params);
×
1929
        }
1930

UNCOV
1931
        return null;
×
1932
    }
1933

1934
    /**
1935
     * Sets $allowEmptyInserts.
1936
     */
1937
    public function allowEmptyInserts(bool $value = true): self
1938
    {
1939
        $this->allowEmptyInserts = $value;
1✔
1940

1941
        return $this;
1✔
1942
    }
1943

1944
    /**
1945
     * Converts database data array to return type value.
1946
     *
1947
     * @param array<string, mixed>          $row        Raw data from database
1948
     * @param 'array'|'object'|class-string $returnType
1949
     */
1950
    protected function convertToReturnType(array $row, string $returnType): array|object
1951
    {
1952
        if ($returnType === 'array') {
25✔
1953
            return $this->converter->fromDataSource($row);
10✔
1954
        }
1955

1956
        if ($returnType === 'object') {
17✔
1957
            return (object) $this->converter->fromDataSource($row);
5✔
1958
        }
1959

1960
        return $this->converter->reconstruct($returnType, $row);
12✔
1961
    }
1962
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc