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

codeigniter4 / CodeIgniter4 / 20589198684

30 Dec 2025 04:55AM UTC coverage: 84.503% (-0.03%) from 84.53%
20589198684

Pull #9853

github

web-flow
Merge f1d8312ec into e2fc5243b
Pull Request #9853: feat(encryption): Add previous keys fallback feature

65 of 77 new or added lines in 5 files covered. (84.42%)

38 existing lines in 3 files now uncovered.

21572 of 25528 relevant lines covered (84.5%)

203.31 hits per line

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

96.01
/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\Database\RawSql;
23
use CodeIgniter\DataCaster\Cast\CastInterface;
24
use CodeIgniter\DataConverter\DataConverter;
25
use CodeIgniter\Entity\Cast\CastInterface as EntityCastInterface;
26
use CodeIgniter\Entity\Entity;
27
use CodeIgniter\Exceptions\InvalidArgumentException;
28
use CodeIgniter\Exceptions\ModelException;
29
use CodeIgniter\I18n\Time;
30
use CodeIgniter\Pager\Pager;
31
use CodeIgniter\Validation\ValidationInterface;
32
use Config\Feature;
33
use ReflectionClass;
34
use ReflectionException;
35
use ReflectionProperty;
36
use stdClass;
37

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

73
    /**
74
     * Database Connection.
75
     *
76
     * @var BaseConnection
77
     */
78
    protected $db;
79

80
    /**
81
     * Last insert ID.
82
     *
83
     * @var int|string
84
     */
85
    protected $insertID = 0;
86

87
    /**
88
     * The Database connection group that
89
     * should be instantiated.
90
     *
91
     * @var non-empty-string|null
92
     */
93
    protected $DBGroup;
94

95
    /**
96
     * The format that the results should be returned as.
97
     *
98
     * Will be overridden if the `$this->asArray()`, `$this->asObject()` methods are used.
99
     *
100
     * @var 'array'|'object'|class-string
101
     */
102
    protected $returnType = 'array';
103

104
    /**
105
     * The temporary format of the result.
106
     *
107
     * Used by `$this->asArray()` and `$this->asObject()` to provide
108
     * temporary overrides of model default.
109
     *
110
     * @var 'array'|'object'|class-string
111
     */
112
    protected $tempReturnType;
113

114
    /**
115
     * Array of column names and the type of value to cast.
116
     *
117
     * @var array<string, string> Array order `['column' => 'type']`.
118
     */
119
    protected array $casts = [];
120

121
    /**
122
     * Custom convert handlers.
123
     *
124
     * @var array<string, class-string<CastInterface|EntityCastInterface>> Array order `['type' => 'classname']`.
125
     */
126
    protected array $castHandlers = [];
127

128
    protected ?DataConverter $converter = null;
129

130
    /**
131
     * Determines whether the model should protect field names during
132
     * mass assignment operations such as $this->insert(), $this->update().
133
     *
134
     * When set to `true`, only the fields explicitly defined in the `$allowedFields`
135
     * property will be allowed for mass assignment. This helps prevent
136
     * unintended modification of database fields and improves security
137
     * by avoiding mass assignment vulnerabilities.
138
     *
139
     * @var bool
140
     */
141
    protected $protectFields = true;
142

143
    /**
144
     * An array of field names that are allowed
145
     * to be set by the user in inserts/updates.
146
     *
147
     * @var list<string>
148
     */
149
    protected $allowedFields = [];
150

151
    /**
152
     * If true, will set created_at, and updated_at
153
     * values during insert and update routines.
154
     *
155
     * @var bool
156
     */
157
    protected $useTimestamps = false;
158

159
    /**
160
     * The type of column that created_at and updated_at
161
     * are expected to.
162
     *
163
     * @var 'date'|'datetime'|'int'
164
     */
165
    protected $dateFormat = 'datetime';
166

167
    /**
168
     * The column used for insert timestamps.
169
     *
170
     * @var string
171
     */
172
    protected $createdField = 'created_at';
173

174
    /**
175
     * The column used for update timestamps.
176
     *
177
     * @var string
178
     */
179
    protected $updatedField = 'updated_at';
180

181
    /**
182
     * If this model should use "softDeletes" and
183
     * simply set a date when rows are deleted, or
184
     * do hard deletes.
185
     *
186
     * @var bool
187
     */
188
    protected $useSoftDeletes = false;
189

190
    /**
191
     * Used by $this->withDeleted() to override the
192
     * model's "softDelete" setting.
193
     *
194
     * @var bool
195
     */
196
    protected $tempUseSoftDeletes;
197

198
    /**
199
     * The column used to save soft delete state.
200
     *
201
     * @var string
202
     */
203
    protected $deletedField = 'deleted_at';
204

205
    /**
206
     * Whether to allow inserting empty data.
207
     */
208
    protected bool $allowEmptyInserts = false;
209

210
    /**
211
     * Whether to update Entity's only changed data.
212
     */
213
    protected bool $updateOnlyChanged = true;
214

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

228
    /**
229
     * Contains any custom error messages to be
230
     * used during data validation.
231
     *
232
     * @var array<string, array<string, string>> The column is used as the keys.
233
     */
234
    protected $validationMessages = [];
235

236
    /**
237
     * Skip the model's validation.
238
     *
239
     * Used in conjunction with `$this->skipValidation()`
240
     * to skip data validation for any future calls.
241
     *
242
     * @var bool
243
     */
244
    protected $skipValidation = false;
245

246
    /**
247
     * Whether rules should be removed that do not exist
248
     * in the passed data. Used in updates.
249
     *
250
     * @var bool
251
     */
252
    protected $cleanValidationRules = true;
253

254
    /**
255
     * Our validator instance.
256
     *
257
     * @var ValidationInterface|null
258
     */
259
    protected $validation;
260

261
    /*
262
     * Callbacks.
263
     *
264
     * Each array should contain the method names (within the model)
265
     * that should be called when those events are triggered.
266
     *
267
     * "Update" and "delete" methods are passed the same items that
268
     * are given to their respective method.
269
     *
270
     * "Find" methods receive the ID searched for (if present), and
271
     * 'afterFind' additionally receives the results that were found.
272
     */
273

274
    /**
275
     * Whether to trigger the defined callbacks.
276
     *
277
     * @var bool
278
     */
279
    protected $allowCallbacks = true;
280

281
    /**
282
     * Used by $this->allowCallbacks() to override the
283
     * model's $allowCallbacks setting.
284
     *
285
     * @var bool
286
     */
287
    protected $tempAllowCallbacks;
288

289
    /**
290
     * Callbacks for "beforeInsert" event.
291
     *
292
     * @var list<string>
293
     */
294
    protected $beforeInsert = [];
295

296
    /**
297
     * Callbacks for "afterInsert" event.
298
     *
299
     * @var list<string>
300
     */
301
    protected $afterInsert = [];
302

303
    /**
304
     * Callbacks for "beforeUpdate" event.
305
     *
306
     * @var list<string>
307
     */
308
    protected $beforeUpdate = [];
309

310
    /**
311
     * Callbacks for "afterUpdate" event.
312
     *
313
     * @var list<string>
314
     */
315
    protected $afterUpdate = [];
316

317
    /**
318
     * Callbacks for "beforeInsertBatch" event.
319
     *
320
     * @var list<string>
321
     */
322
    protected $beforeInsertBatch = [];
323

324
    /**
325
     * Callbacks for "afterInsertBatch" event.
326
     *
327
     * @var list<string>
328
     */
329
    protected $afterInsertBatch = [];
330

331
    /**
332
     * Callbacks for "beforeUpdateBatch" event.
333
     *
334
     * @var list<string>
335
     */
336
    protected $beforeUpdateBatch = [];
337

338
    /**
339
     * Callbacks for "afterUpdateBatch" event.
340
     *
341
     * @var list<string>
342
     */
343
    protected $afterUpdateBatch = [];
344

345
    /**
346
     * Callbacks for "beforeFind" event.
347
     *
348
     * @var list<string>
349
     */
350
    protected $beforeFind = [];
351

352
    /**
353
     * Callbacks for "afterFind" event.
354
     *
355
     * @var list<string>
356
     */
357
    protected $afterFind = [];
358

359
    /**
360
     * Callbacks for "beforeDelete" event.
361
     *
362
     * @var list<string>
363
     */
364
    protected $beforeDelete = [];
365

366
    /**
367
     * Callbacks for "afterDelete" event.
368
     *
369
     * @var list<string>
370
     */
371
    protected $afterDelete = [];
372

373
    public function __construct(?ValidationInterface $validation = null)
374
    {
375
        $this->tempReturnType     = $this->returnType;
391✔
376
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
391✔
377
        $this->tempAllowCallbacks = $this->allowCallbacks;
391✔
378

379
        $this->validation = $validation;
391✔
380

381
        $this->initialize();
391✔
382
        $this->createDataConverter();
391✔
383
    }
384

385
    /**
386
     * Creates DataConverter instance.
387
     */
388
    protected function createDataConverter(): void
389
    {
390
        if ($this->useCasts()) {
391✔
391
            $this->converter = new DataConverter(
30✔
392
                $this->casts,
30✔
393
                $this->castHandlers,
30✔
394
                $this->db,
30✔
395
            );
30✔
396
        }
397
    }
398

399
    /**
400
     * Are casts used?
401
     */
402
    protected function useCasts(): bool
403
    {
404
        return $this->casts !== [];
391✔
405
    }
406

407
    /**
408
     * Initializes the instance with any additional steps.
409
     * Optionally implemented by child classes.
410
     *
411
     * @return void
412
     */
413
    protected function initialize()
414
    {
415
    }
390✔
416

417
    /**
418
     * Fetches the row(s) of database with a primary key
419
     * matching $id.
420
     * This method works only with DB calls.
421
     *
422
     * @param bool                             $singleton Single or multiple results.
423
     * @param int|list<int|string>|string|null $id        One primary key or an array of primary keys.
424
     *
425
     * @return ($singleton is true ? object|row_array|null : list<object|row_array>) The resulting row of data or `null`.
426
     */
427
    abstract protected function doFind(bool $singleton, $id = null);
428

429
    /**
430
     * Fetches the column of database.
431
     * This method works only with DB calls.
432
     *
433
     * @return list<row_array>|null The resulting row of data or `null` if no data found.
434
     *
435
     * @throws DataException
436
     */
437
    abstract protected function doFindColumn(string $columnName);
438

439
    /**
440
     * Fetches all results, while optionally limiting them.
441
     * This method works only with DB calls.
442
     *
443
     * @return list<object|row_array>
444
     */
445
    abstract protected function doFindAll(?int $limit = null, int $offset = 0);
446

447
    /**
448
     * Returns the first row of the result set.
449
     * This method works only with DB calls.
450
     *
451
     * @return object|row_array|null
452
     */
453
    abstract protected function doFirst();
454

455
    /**
456
     * Inserts data into the current database.
457
     * This method works only with DB calls.
458
     *
459
     * @param row_array $row
460
     *
461
     * @return bool
462
     */
463
    abstract protected function doInsert(array $row);
464

465
    /**
466
     * Compiles batch insert and runs the queries, validating each row prior.
467
     * This method works only with DB calls.
468
     *
469
     * @param list<object|row_array>|null $set       An associative array of insert values.
470
     * @param bool|null                   $escape    Whether to escape values.
471
     * @param int                         $batchSize The size of the batch to run.
472
     * @param bool                        $testing   `true` means only number of records is returned, `false` will execute the query.
473
     *
474
     * @return false|int|list<string> Number of rows affected or `false` on failure, SQL array when test mode
475
     */
476
    abstract protected function doInsertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false);
477

478
    /**
479
     * Updates a single record in the database.
480
     * This method works only with DB calls.
481
     *
482
     * @param int|list<int|string>|string|null $id
483
     * @param row_array|null                   $row
484
     */
485
    abstract protected function doUpdate($id = null, $row = null): bool;
486

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

502
    /**
503
     * Deletes a single record from the database where $id matches
504
     * the table's primary key.
505
     * This method works only with DB calls.
506
     *
507
     * @param int|list<int|string>|string|null $id    The rows primary key(s).
508
     * @param bool                             $purge Allows overriding the soft deletes setting.
509
     *
510
     * @return bool|string Returns a SQL string if in test mode.
511
     *
512
     * @throws DatabaseException
513
     */
514
    abstract protected function doDelete($id = null, bool $purge = false);
515

516
    /**
517
     * Permanently deletes all rows that have been marked as deleted
518
     * through soft deletes (value of column $deletedField is not null).
519
     * This method works only with DB calls.
520
     *
521
     * @return bool|string Returns a SQL string if in test mode.
522
     */
523
    abstract protected function doPurgeDeleted();
524

525
    /**
526
     * Works with the $this->find* methods to return only the rows that
527
     * have been deleted (value of column $deletedField is not null).
528
     * This method works only with DB calls.
529
     *
530
     * @return void
531
     */
532
    abstract protected function doOnlyDeleted();
533

534
    /**
535
     * Compiles a replace and runs the query.
536
     * This method works only with DB calls.
537
     *
538
     * @param row_array|null $row
539
     * @param bool           $returnSQL `true` means SQL is returned, `false` will execute the query.
540
     *
541
     * @return BaseResult|false|Query|string
542
     */
543
    abstract protected function doReplace(?array $row = null, bool $returnSQL = false);
544

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

553
    /**
554
     * Public getter to return the ID value for the data array or object.
555
     * For example with SQL this will return `$data->{$this->primaryKey}`.
556
     *
557
     * @param object|row_array $row
558
     *
559
     * @return int|string|null
560
     */
561
    abstract public function getIdValue($row);
562

563
    /**
564
     * Override countAllResults to account for soft deleted accounts.
565
     * This method works only with DB calls.
566
     *
567
     * @param bool $reset When `false`, the `$tempUseSoftDeletes` will be
568
     *                    dependent on `$useSoftDeletes` value because we don't
569
     *                    want to add the same "where" condition for the second time.
570
     * @param bool $test  `true` returns the number of all records, `false` will execute the query.
571
     *
572
     * @return int|string Returns a SQL string if in test mode.
573
     */
574
    abstract public function countAllResults(bool $reset = true, bool $test = false);
575

576
    /**
577
     * Loops over records in batches, allowing you to operate on them.
578
     * This method works only with DB calls.
579
     *
580
     * @param Closure(array<string, string>|object): mixed $userFunc
581
     *
582
     * @return void
583
     *
584
     * @throws DataException
585
     */
586
    abstract public function chunk(int $size, Closure $userFunc);
587

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

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

607
            if (isset($eventData['returnData']) && $eventData['returnData'] === true) {
55✔
608
                return $eventData['data'];
3✔
609
            }
610
        }
611

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

619
        if ($this->tempAllowCallbacks) {
55✔
620
            $eventData = $this->trigger('afterFind', $eventData);
52✔
621
        }
622

623
        $this->tempReturnType     = $this->returnType;
55✔
624
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
55✔
625
        $this->tempAllowCallbacks = $this->allowCallbacks;
55✔
626

627
        return $eventData['data'];
55✔
628
    }
629

630
    /**
631
     * Fetches the column of database.
632
     *
633
     * @return list<bool|float|int|list<mixed>|object|string|null>|null The resulting row of data, or `null` if no data found.
634
     *
635
     * @throws DataException
636
     */
637
    public function findColumn(string $columnName)
638
    {
639
        if (str_contains($columnName, ',')) {
3✔
640
            throw DataException::forFindColumnHaveMultipleColumns();
1✔
641
        }
642

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

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

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

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

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

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

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

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

690
        return $eventData['data'];
22✔
691
    }
692

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

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

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

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

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

726
        return $eventData['data'];
24✔
727
    }
728

729
    /**
730
     * A convenience method that will attempt to determine whether the
731
     * data should be inserted or updated.
732
     *
733
     * Will work with either an array or object.
734
     * 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
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 object|row_array $row
766
     */
767
    protected function shouldUpdate($row): bool
768
    {
769
        $id = $this->getIdValue($row);
27✔
770

771
        return ! in_array($id, [null, [], ''], true);
27✔
772
    }
773

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

784
    /**
785
     * Validates that the primary key values are valid for update/delete/insert operations.
786
     * Throws exception if invalid.
787
     *
788
     * @param bool $allowArray Whether to allow array of IDs (true for update/delete, false for insert)
789
     *
790
     * @phpstan-assert non-zero-int|non-empty-list<int|string>|RawSql|non-falsy-string $id
791
     * @throws         InvalidArgumentException
792
     */
793
    protected function validateID(mixed $id, bool $allowArray = true): void
794
    {
795
        if (is_array($id)) {
143✔
796
            // Check if arrays are allowed
797
            if (! $allowArray) {
130✔
798
                throw new InvalidArgumentException(
8✔
799
                    'Invalid primary key: only a single value is allowed, not an array.',
8✔
800
                );
8✔
801
            }
802

803
            // Check for empty array
804
            if ($id === []) {
122✔
805
                throw new InvalidArgumentException('Invalid primary key: cannot be an empty array.');
5✔
806
            }
807

808
            // Validate each ID in the array recursively
809
            foreach ($id as $key => $valueId) {
117✔
810
                if (is_array($valueId)) {
117✔
811
                    throw new InvalidArgumentException(
5✔
812
                        sprintf('Invalid primary key at index %s: nested arrays are not allowed.', $key),
5✔
813
                    );
5✔
814
                }
815

816
                // Recursive call for each value (single values only in recursion)
817
                $this->validateID($valueId, false);
112✔
818
            }
819

820
            return;
62✔
821
        }
822

823
        // Allow RawSql objects for complex scenarios
824
        if ($id instanceof RawSql) {
129✔
825
            return;
2✔
826
        }
827

828
        // Check for invalid single values
829
        if (in_array($id, [null, 0, '0', '', true, false], true)) {
127✔
830
            $type = is_bool($id) ? 'boolean ' . var_export($id, true) : var_export($id, true);
60✔
831

832
            throw new InvalidArgumentException(
60✔
833
                sprintf('Invalid primary key: %s is not allowed.', $type),
60✔
834
            );
60✔
835
        }
836

837
        // Only allow int and string at this point
838
        if (! is_int($id) && ! is_string($id)) {
97✔
UNCOV
839
            throw new InvalidArgumentException(
×
UNCOV
840
                sprintf('Invalid primary key: must be int or string, %s given.', get_debug_type($id)),
×
UNCOV
841
            );
×
842
        }
843
    }
844

845
    /**
846
     * Inserts data into the database. If an object is provided,
847
     * it will attempt to convert it to an array.
848
     *
849
     * @param object|row_array|null $row
850
     * @param bool                  $returnID Whether insert ID should be returned or not.
851
     *
852
     * @return ($returnID is true ? false|int|string : bool)
853
     *
854
     * @throws ReflectionException
855
     */
856
    public function insert($row = null, bool $returnID = true)
857
    {
858
        $this->insertID = 0;
119✔
859

860
        // Set $cleanValidationRules to false temporary.
861
        $cleanValidationRules       = $this->cleanValidationRules;
119✔
862
        $this->cleanValidationRules = false;
119✔
863

864
        $row = $this->transformDataToArray($row, 'insert');
119✔
865

866
        // Validate data before saving.
867
        if (! $this->skipValidation && ! $this->validate($row)) {
117✔
868
            // Restore $cleanValidationRules
869
            $this->cleanValidationRules = $cleanValidationRules;
19✔
870

871
            return false;
19✔
872
        }
873

874
        // Restore $cleanValidationRules
875
        $this->cleanValidationRules = $cleanValidationRules;
100✔
876

877
        // Must be called first, so we don't
878
        // strip out created_at values.
879
        $row = $this->doProtectFieldsForInsert($row);
100✔
880

881
        // doProtectFields() can further remove elements from
882
        // $row, so we need to check for empty dataset again
883
        if (! $this->allowEmptyInserts && $row === []) {
99✔
884
            throw DataException::forEmptyDataset('insert');
2✔
885
        }
886

887
        // Set created_at and updated_at with same time
888
        $date = $this->setDate();
97✔
889
        $row  = $this->setCreatedField($row, $date);
97✔
890
        $row  = $this->setUpdatedField($row, $date);
97✔
891

892
        $eventData = ['data' => $row];
97✔
893

894
        if ($this->tempAllowCallbacks) {
97✔
895
            $eventData = $this->trigger('beforeInsert', $eventData);
97✔
896
        }
897

898
        $result = $this->doInsert($eventData['data']);
96✔
899

900
        $eventData = [
85✔
901
            'id'     => $this->insertID,
85✔
902
            'data'   => $eventData['data'],
85✔
903
            'result' => $result,
85✔
904
        ];
85✔
905

906
        if ($this->tempAllowCallbacks) {
85✔
907
            // Trigger afterInsert events with the inserted data and new ID
908
            $this->trigger('afterInsert', $eventData);
85✔
909
        }
910

911
        $this->tempAllowCallbacks = $this->allowCallbacks;
85✔
912

913
        // If insertion failed, get out of here
914
        if (! $result) {
85✔
915
            return $result;
2✔
916
        }
917

918
        // otherwise return the insertID, if requested.
919
        return $returnID ? $this->insertID : $result;
83✔
920
    }
921

922
    /**
923
     * Set datetime to created field.
924
     *
925
     * @param row_array  $row
926
     * @param int|string $date Timestamp or datetime string.
927
     *
928
     * @return row_array
929
     */
930
    protected function setCreatedField(array $row, $date): array
931
    {
932
        if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $row)) {
119✔
933
            $row[$this->createdField] = $date;
39✔
934
        }
935

936
        return $row;
119✔
937
    }
938

939
    /**
940
     * Set datetime to updated field.
941
     *
942
     * @param row_array  $row
943
     * @param int|string $date Timestamp or datetime string
944
     *
945
     * @return row_array
946
     */
947
    protected function setUpdatedField(array $row, $date): array
948
    {
949
        if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
135✔
950
            $row[$this->updatedField] = $date;
43✔
951
        }
952

953
        return $row;
135✔
954
    }
955

956
    /**
957
     * Compiles batch insert runs the queries, validating each row prior.
958
     *
959
     * @param list<object|row_array>|null $set       An associative array of insert values.
960
     * @param bool|null                   $escape    Whether to escape values.
961
     * @param int                         $batchSize The size of the batch to run.
962
     * @param bool                        $testing   `true` means only number of records is returned, `false` will execute the query.
963
     *
964
     * @return false|int|list<string> Number of rows inserted or `false` on failure.
965
     *
966
     * @throws ReflectionException
967
     */
968
    public function insertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false)
969
    {
970
        // Set $cleanValidationRules to false temporary.
971
        $cleanValidationRules       = $this->cleanValidationRules;
24✔
972
        $this->cleanValidationRules = false;
24✔
973

974
        if (is_array($set)) {
24✔
975
            foreach ($set as &$row) {
24✔
976
                $row = $this->transformDataToArray($row, 'insert');
24✔
977

978
                // Validate every row.
979
                if (! $this->skipValidation && ! $this->validate($row)) {
24✔
980
                    // Restore $cleanValidationRules
981
                    $this->cleanValidationRules = $cleanValidationRules;
3✔
982

983
                    return false;
3✔
984
                }
985

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

990
                // Set created_at and updated_at with same time
991
                $date = $this->setDate();
21✔
992
                $row  = $this->setCreatedField($row, $date);
21✔
993
                $row  = $this->setUpdatedField($row, $date);
21✔
994
            }
995
        }
996

997
        // Restore $cleanValidationRules
998
        $this->cleanValidationRules = $cleanValidationRules;
21✔
999

1000
        $eventData = ['data' => $set];
21✔
1001

1002
        if ($this->tempAllowCallbacks) {
21✔
1003
            $eventData = $this->trigger('beforeInsertBatch', $eventData);
21✔
1004
        }
1005

1006
        $result = $this->doInsertBatch($eventData['data'], $escape, $batchSize, $testing);
21✔
1007

1008
        $eventData = [
11✔
1009
            'data'   => $eventData['data'],
11✔
1010
            'result' => $result,
11✔
1011
        ];
11✔
1012

1013
        if ($this->tempAllowCallbacks) {
11✔
1014
            // Trigger afterInsert events with the inserted data and new ID
1015
            $this->trigger('afterInsertBatch', $eventData);
11✔
1016
        }
1017

1018
        $this->tempAllowCallbacks = $this->allowCallbacks;
11✔
1019

1020
        return $result;
11✔
1021
    }
1022

1023
    /**
1024
     * Updates a single record in the database. If an object is provided,
1025
     * it will attempt to convert it into an array.
1026
     *
1027
     * @param int|list<int|string>|RawSql|string|null $id
1028
     * @param object|row_array|null                   $row
1029
     *
1030
     * @throws ReflectionException
1031
     */
1032
    public function update($id = null, $row = null): bool
1033
    {
1034
        if ($id !== null) {
58✔
1035
            if (! is_array($id)) {
52✔
1036
                $id = [$id];
44✔
1037
            }
1038

1039
            $this->validateID($id);
52✔
1040
        }
1041

1042
        $row = $this->transformDataToArray($row, 'update');
46✔
1043

1044
        // Validate data before saving.
1045
        if (! $this->skipValidation && ! $this->validate($row)) {
44✔
1046
            return false;
4✔
1047
        }
1048

1049
        // Must be called first, so we don't
1050
        // strip out updated_at values.
1051
        $row = $this->doProtectFields($row);
40✔
1052

1053
        // doProtectFields() can further remove elements from
1054
        // $row, so we need to check for empty dataset again
1055
        if ($row === []) {
40✔
1056
            throw DataException::forEmptyDataset('update');
2✔
1057
        }
1058

1059
        $row = $this->setUpdatedField($row, $this->setDate());
38✔
1060

1061
        $eventData = [
38✔
1062
            'id'   => $id,
38✔
1063
            'data' => $row,
38✔
1064
        ];
38✔
1065

1066
        if ($this->tempAllowCallbacks) {
38✔
1067
            $eventData = $this->trigger('beforeUpdate', $eventData);
38✔
1068
        }
1069

1070
        $eventData = [
38✔
1071
            'id'     => $id,
38✔
1072
            'data'   => $eventData['data'],
38✔
1073
            'result' => $this->doUpdate($id, $eventData['data']),
38✔
1074
        ];
38✔
1075

1076
        if ($this->tempAllowCallbacks) {
37✔
1077
            $this->trigger('afterUpdate', $eventData);
37✔
1078
        }
1079

1080
        $this->tempAllowCallbacks = $this->allowCallbacks;
37✔
1081

1082
        return $eventData['result'];
37✔
1083
    }
1084

1085
    /**
1086
     * Compiles an update and runs the query.
1087
     *
1088
     * @param list<object|row_array>|null $set       An associative array of insert values.
1089
     * @param string|null                 $index     The where key.
1090
     * @param int                         $batchSize The size of the batch to run.
1091
     * @param bool                        $returnSQL `true` means SQL is returned, `false` will execute the query.
1092
     *
1093
     * @return false|int|list<string> Number of rows affected or `false` on failure, SQL array when test mode.
1094
     *
1095
     * @throws DatabaseException
1096
     * @throws ReflectionException
1097
     */
1098
    public function updateBatch(?array $set = null, ?string $index = null, int $batchSize = 100, bool $returnSQL = false)
1099
    {
1100
        if (is_array($set)) {
6✔
1101
            foreach ($set as &$row) {
6✔
1102
                $row = $this->transformDataToArray($row, 'update');
6✔
1103

1104
                // Validate data before saving.
1105
                if (! $this->skipValidation && ! $this->validate($row)) {
6✔
1106
                    return false;
1✔
1107
                }
1108

1109
                // Save updateIndex for later
1110
                $updateIndex = $row[$index] ?? null;
5✔
1111

1112
                if ($updateIndex === null) {
5✔
1113
                    throw new InvalidArgumentException(
1✔
1114
                        'The index ("' . $index . '") for updateBatch() is missing in the data: '
1✔
1115
                        . json_encode($row),
1✔
1116
                    );
1✔
1117
                }
1118

1119
                // Must be called first so we don't
1120
                // strip out updated_at values.
1121
                $row = $this->doProtectFields($row);
4✔
1122

1123
                // Restore updateIndex value in case it was wiped out
1124
                $row[$index] = $updateIndex;
4✔
1125

1126
                $row = $this->setUpdatedField($row, $this->setDate());
4✔
1127
            }
1128
        }
1129

1130
        $eventData = ['data' => $set];
4✔
1131

1132
        if ($this->tempAllowCallbacks) {
4✔
1133
            $eventData = $this->trigger('beforeUpdateBatch', $eventData);
4✔
1134
        }
1135

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

1138
        $eventData = [
4✔
1139
            'data'   => $eventData['data'],
4✔
1140
            'result' => $result,
4✔
1141
        ];
4✔
1142

1143
        if ($this->tempAllowCallbacks) {
4✔
1144
            // Trigger afterInsert events with the inserted data and new ID
1145
            $this->trigger('afterUpdateBatch', $eventData);
4✔
1146
        }
1147

1148
        $this->tempAllowCallbacks = $this->allowCallbacks;
4✔
1149

1150
        return $result;
4✔
1151
    }
1152

1153
    /**
1154
     * Deletes a single record from the database where $id matches.
1155
     *
1156
     * @param int|list<int|string>|RawSql|string|null $id    The rows primary key(s).
1157
     * @param bool                                    $purge Allows overriding the soft deletes setting.
1158
     *
1159
     * @return bool|string Returns a SQL string if in test mode.
1160
     *
1161
     * @throws DatabaseException
1162
     */
1163
    public function delete($id = null, bool $purge = false)
1164
    {
1165
        if ($id !== null) {
84✔
1166
            if (! is_array($id)) {
70✔
1167
                $id = [$id];
41✔
1168
            }
1169

1170
            $this->validateID($id);
70✔
1171
        }
1172

1173
        $eventData = [
36✔
1174
            'id'    => $id,
36✔
1175
            'purge' => $purge,
36✔
1176
        ];
36✔
1177

1178
        if ($this->tempAllowCallbacks) {
36✔
1179
            $this->trigger('beforeDelete', $eventData);
35✔
1180
        }
1181

1182
        $eventData = [
36✔
1183
            'id'     => $id,
36✔
1184
            'data'   => null,
36✔
1185
            'purge'  => $purge,
36✔
1186
            'result' => $this->doDelete($id, $purge),
36✔
1187
        ];
36✔
1188

1189
        if ($this->tempAllowCallbacks) {
31✔
1190
            $this->trigger('afterDelete', $eventData);
30✔
1191
        }
1192

1193
        $this->tempAllowCallbacks = $this->allowCallbacks;
31✔
1194

1195
        return $eventData['result'];
31✔
1196
    }
1197

1198
    /**
1199
     * Permanently deletes all rows that have been marked as deleted
1200
     * through soft deletes (value of column $deletedField is not null).
1201
     *
1202
     * @return bool|string Returns a SQL string if in test mode.
1203
     */
1204
    public function purgeDeleted()
1205
    {
1206
        if (! $this->useSoftDeletes) {
2✔
1207
            return true;
1✔
1208
        }
1209

1210
        return $this->doPurgeDeleted();
1✔
1211
    }
1212

1213
    /**
1214
     * Sets $useSoftDeletes value so that we can temporarily override
1215
     * the soft deletes settings. Can be used for all find* methods.
1216
     *
1217
     * @return $this
1218
     */
1219
    public function withDeleted(bool $val = true)
1220
    {
1221
        $this->tempUseSoftDeletes = ! $val;
23✔
1222

1223
        return $this;
23✔
1224
    }
1225

1226
    /**
1227
     * Works with the $this->find* methods to return only the rows that
1228
     * have been deleted.
1229
     *
1230
     * @return $this
1231
     */
1232
    public function onlyDeleted()
1233
    {
1234
        $this->tempUseSoftDeletes = false;
1✔
1235
        $this->doOnlyDeleted();
1✔
1236

1237
        return $this;
1✔
1238
    }
1239

1240
    /**
1241
     * Compiles a replace and runs the query.
1242
     *
1243
     * @param row_array|null $row
1244
     * @param bool           $returnSQL `true` means SQL is returned, `false` will execute the query.
1245
     *
1246
     * @return BaseResult|false|Query|string
1247
     */
1248
    public function replace(?array $row = null, bool $returnSQL = false)
1249
    {
1250
        // Validate data before saving.
1251
        if (($row !== null) && ! $this->skipValidation && ! $this->validate($row)) {
3✔
1252
            return false;
1✔
1253
        }
1254

1255
        $row = (array) $row;
2✔
1256
        $row = $this->setCreatedField($row, $this->setDate());
2✔
1257
        $row = $this->setUpdatedField($row, $this->setDate());
2✔
1258

1259
        return $this->doReplace($row, $returnSQL);
2✔
1260
    }
1261

1262
    /**
1263
     * Grabs the last error(s) that occurred.
1264
     *
1265
     * If data was validated, it will first check for errors there,
1266
     *  otherwise will try to grab the last error from the Database connection.
1267
     *
1268
     * The return array should be in the following format:
1269
     *  `['source' => 'message']`.
1270
     *
1271
     * @param bool $forceDB Always grab the DB error, not validation.
1272
     *
1273
     * @return array<string, string>
1274
     */
1275
    public function errors(bool $forceDB = false)
1276
    {
1277
        if ($this->validation === null) {
24✔
UNCOV
1278
            return $this->doErrors();
×
1279
        }
1280

1281
        // Do we have validation errors?
1282
        if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors()) !== []) {
24✔
1283
            return $errors;
22✔
1284
        }
1285

1286
        return $this->doErrors();
2✔
1287
    }
1288

1289
    /**
1290
     * Works with Pager to get the size and offset parameters.
1291
     * Expects a GET variable (?page=2) that specifies the page of results
1292
     * to display.
1293
     *
1294
     * @param int|null $perPage Items per page.
1295
     * @param string   $group   Will be used by the pagination library to identify a unique pagination set.
1296
     * @param int|null $page    Optional page number (useful when the page number is provided in different way).
1297
     * @param int      $segment Optional URI segment number (if page number is provided by URI segment).
1298
     *
1299
     * @return list<object|row_array>
1300
     */
1301
    public function paginate(?int $perPage = null, string $group = 'default', ?int $page = null, int $segment = 0)
1302
    {
1303
        // Since multiple models may use the Pager, the Pager must be shared.
1304
        $pager = service('pager');
8✔
1305

1306
        if ($segment !== 0) {
8✔
UNCOV
1307
            $pager->setSegment($segment, $group);
×
1308
        }
1309

1310
        $page = $page >= 1 ? $page : $pager->getCurrentPage($group);
8✔
1311
        // Store it in the Pager library, so it can be paginated in the views.
1312
        $this->pager = $pager->store($group, $page, $perPage, $this->countAllResults(false), $segment);
8✔
1313
        $perPage     = $this->pager->getPerPage($group);
8✔
1314
        $offset      = ($pager->getCurrentPage($group) - 1) * $perPage;
8✔
1315

1316
        return $this->findAll($perPage, $offset);
8✔
1317
    }
1318

1319
    /**
1320
     * It could be used when you have to change default or override current allowed fields.
1321
     *
1322
     * @param list<string> $allowedFields Array with names of fields.
1323
     *
1324
     * @return $this
1325
     */
1326
    public function setAllowedFields(array $allowedFields)
1327
    {
1328
        $this->allowedFields = $allowedFields;
10✔
1329

1330
        return $this;
10✔
1331
    }
1332

1333
    /**
1334
     * Sets whether or not we should whitelist data set during
1335
     * updates or inserts against $this->availableFields.
1336
     *
1337
     * @return $this
1338
     */
1339
    public function protect(bool $protect = true)
1340
    {
1341
        $this->protectFields = $protect;
12✔
1342

1343
        return $this;
12✔
1344
    }
1345

1346
    /**
1347
     * Ensures that only the fields that are allowed to be updated are
1348
     * in the data array.
1349
     *
1350
     * @used-by update() to protect against mass assignment vulnerabilities.
1351
     * @used-by updateBatch() to protect against mass assignment vulnerabilities.
1352
     *
1353
     * @param row_array $row
1354
     *
1355
     * @return row_array
1356
     *
1357
     * @throws DataException
1358
     */
1359
    protected function doProtectFields(array $row): array
1360
    {
1361
        if (! $this->protectFields) {
44✔
1362
            return $row;
2✔
1363
        }
1364

1365
        if ($this->allowedFields === []) {
42✔
UNCOV
1366
            throw DataException::forInvalidAllowedFields(static::class);
×
1367
        }
1368

1369
        foreach (array_keys($row) as $key) {
42✔
1370
            if (! in_array($key, $this->allowedFields, true)) {
42✔
1371
                unset($row[$key]);
23✔
1372
            }
1373
        }
1374

1375
        return $row;
42✔
1376
    }
1377

1378
    /**
1379
     * Ensures that only the fields that are allowed to be inserted are in
1380
     * the data array.
1381
     *
1382
     * @used-by insert() to protect against mass assignment vulnerabilities.
1383
     * @used-by insertBatch() to protect against mass assignment vulnerabilities.
1384
     *
1385
     * @param row_array $row
1386
     *
1387
     * @return row_array
1388
     *
1389
     * @throws DataException
1390
     */
1391
    protected function doProtectFieldsForInsert(array $row): array
1392
    {
UNCOV
1393
        return $this->doProtectFields($row);
×
1394
    }
1395

1396
    /**
1397
     * Sets the timestamp or current timestamp if null value is passed.
1398
     *
1399
     * @param int|null $userDate An optional PHP timestamp to be converted
1400
     *
1401
     * @return int|string
1402
     *
1403
     * @throws ModelException
1404
     */
1405
    protected function setDate(?int $userDate = null)
1406
    {
1407
        $currentDate = $userDate ?? Time::now()->getTimestamp();
157✔
1408

1409
        return $this->intToDate($currentDate);
157✔
1410
    }
1411

1412
    /**
1413
     * A utility function to allow child models to use the type of
1414
     * date/time format that they prefer. This is primarily used for
1415
     * setting created_at, updated_at and deleted_at values, but can be
1416
     * used by inheriting classes.
1417
     *
1418
     * The available time formats are:
1419
     *  - 'int'      - Stores the date as an integer timestamp.
1420
     *  - 'datetime' - Stores the data in the SQL datetime format.
1421
     *  - 'date'     - Stores the date (only) in the SQL date format.
1422
     *
1423
     * @return int|string
1424
     *
1425
     * @throws ModelException
1426
     */
1427
    protected function intToDate(int $value)
1428
    {
1429
        return match ($this->dateFormat) {
157✔
1430
            'int'      => $value,
36✔
1431
            'datetime' => date($this->db->dateFormat['datetime'], $value),
119✔
1432
            'date'     => date($this->db->dateFormat['date'], $value),
1✔
1433
            default    => throw ModelException::forNoDateFormat(static::class),
157✔
1434
        };
157✔
1435
    }
1436

1437
    /**
1438
     * Converts Time value to string using $this->dateFormat.
1439
     *
1440
     * The available time formats are:
1441
     *  - 'int'      - Stores the date as an integer timestamp.
1442
     *  - 'datetime' - Stores the data in the SQL datetime format.
1443
     *  - 'date'     - Stores the date (only) in the SQL date format.
1444
     *
1445
     * @return int|string
1446
     */
1447
    protected function timeToDate(Time $value)
1448
    {
1449
        return match ($this->dateFormat) {
5✔
1450
            'datetime' => $value->format($this->db->dateFormat['datetime']),
3✔
1451
            'date'     => $value->format($this->db->dateFormat['date']),
1✔
1452
            'int'      => $value->getTimestamp(),
1✔
1453
            default    => (string) $value,
5✔
1454
        };
5✔
1455
    }
1456

1457
    /**
1458
     * Set the value of the $skipValidation flag.
1459
     *
1460
     * @return $this
1461
     */
1462
    public function skipValidation(bool $skip = true)
1463
    {
1464
        $this->skipValidation = $skip;
2✔
1465

1466
        return $this;
2✔
1467
    }
1468

1469
    /**
1470
     * Allows to set (and reset) validation messages.
1471
     * It could be used when you have to change default or override current validate messages.
1472
     *
1473
     * @param array<string, array<string, string>> $validationMessages
1474
     *
1475
     * @return $this
1476
     */
1477
    public function setValidationMessages(array $validationMessages)
1478
    {
UNCOV
1479
        $this->validationMessages = $validationMessages;
×
1480

UNCOV
1481
        return $this;
×
1482
    }
1483

1484
    /**
1485
     * Allows to set field wise validation message.
1486
     * It could be used when you have to change default or override current validate messages.
1487
     *
1488
     * @param array<string, string> $fieldMessages
1489
     *
1490
     * @return $this
1491
     */
1492
    public function setValidationMessage(string $field, array $fieldMessages)
1493
    {
1494
        $this->validationMessages[$field] = $fieldMessages;
2✔
1495

1496
        return $this;
2✔
1497
    }
1498

1499
    /**
1500
     * Allows to set (and reset) validation rules.
1501
     * It could be used when you have to change default or override current validate rules.
1502
     *
1503
     * @param array<string, array<string, array<string, string>|string>|string> $validationRules
1504
     *
1505
     * @return $this
1506
     */
1507
    public function setValidationRules(array $validationRules)
1508
    {
1509
        $this->validationRules = $validationRules;
2✔
1510

1511
        return $this;
2✔
1512
    }
1513

1514
    /**
1515
     * Allows to set field wise validation rules.
1516
     * It could be used when you have to change default or override current validate rules.
1517
     *
1518
     * @param array<string, array<string, string>|string>|string $fieldRules
1519
     *
1520
     * @return $this
1521
     */
1522
    public function setValidationRule(string $field, $fieldRules)
1523
    {
1524
        $rules = $this->validationRules;
2✔
1525

1526
        // ValidationRules can be either a string, which is the group name,
1527
        // or an array of rules.
1528
        if (is_string($rules)) {
2✔
1529
            $this->ensureValidation();
1✔
1530

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

1533
            $this->validationRules = $rules;
1✔
1534
            $this->validationMessages += $customErrors;
1✔
1535
        }
1536

1537
        $this->validationRules[$field] = $fieldRules;
2✔
1538

1539
        return $this;
2✔
1540
    }
1541

1542
    /**
1543
     * Should validation rules be removed before saving?
1544
     * Most handy when doing updates.
1545
     *
1546
     * @return $this
1547
     */
1548
    public function cleanRules(bool $choice = false)
1549
    {
1550
        $this->cleanValidationRules = $choice;
2✔
1551

1552
        return $this;
2✔
1553
    }
1554

1555
    /**
1556
     * Validate the row data against the validation rules (or the validation group)
1557
     * specified in the class property, $validationRules.
1558
     *
1559
     * @param object|row_array $row
1560
     */
1561
    public function validate($row): bool
1562
    {
1563
        if ($this->skipValidation) {
172✔
UNCOV
1564
            return true;
×
1565
        }
1566

1567
        $rules = $this->getValidationRules();
172✔
1568

1569
        if ($rules === []) {
172✔
1570
            return true;
126✔
1571
        }
1572

1573
        // Validation requires array, so cast away.
1574
        if (is_object($row)) {
46✔
1575
            $row = (array) $row;
2✔
1576
        }
1577

1578
        if ($row === []) {
46✔
UNCOV
1579
            return true;
×
1580
        }
1581

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

1584
        // If no data existed that needs validation
1585
        // our job is done here.
1586
        if ($rules === []) {
46✔
1587
            return true;
2✔
1588
        }
1589

1590
        $this->ensureValidation();
44✔
1591

1592
        $this->validation->reset()->setRules($rules, $this->validationMessages);
44✔
1593

1594
        return $this->validation->run($row, null, $this->DBGroup);
44✔
1595
    }
1596

1597
    /**
1598
     * Returns the model's defined validation rules so that they
1599
     * can be used elsewhere, if needed.
1600
     *
1601
     * @param array{only?: list<string>, except?: list<string>} $options Filter the list of rules
1602
     *
1603
     * @return array<string, array<string, array<string, string>|string>|string>
1604
     */
1605
    public function getValidationRules(array $options = []): array
1606
    {
1607
        $rules = $this->validationRules;
174✔
1608

1609
        // ValidationRules can be either a string, which is the group name,
1610
        // or an array of rules.
1611
        if (is_string($rules)) {
174✔
1612
            $this->ensureValidation();
13✔
1613

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

1616
            $this->validationMessages += $customErrors;
13✔
1617
        }
1618

1619
        if (isset($options['except'])) {
174✔
UNCOV
1620
            $rules = array_diff_key($rules, array_flip($options['except']));
×
1621
        } elseif (isset($options['only'])) {
174✔
UNCOV
1622
            $rules = array_intersect_key($rules, array_flip($options['only']));
×
1623
        }
1624

1625
        return $rules;
174✔
1626
    }
1627

1628
    protected function ensureValidation(): void
1629
    {
1630
        if ($this->validation === null) {
45✔
1631
            $this->validation = service('validation', null, false);
29✔
1632
        }
1633
    }
1634

1635
    /**
1636
     * Returns the model's validation messages, so they
1637
     * can be used elsewhere, if needed.
1638
     *
1639
     * @return array<string, array<string, string>>
1640
     */
1641
    public function getValidationMessages(): array
1642
    {
1643
        return $this->validationMessages;
2✔
1644
    }
1645

1646
    /**
1647
     * Removes any rules that apply to fields that have not been set
1648
     * currently so that rules don't block updating when only updating
1649
     * a partial row.
1650
     *
1651
     * @param array<string, array<string, array<string, string>|string>|string> $rules
1652
     * @param row_array                                                         $row
1653
     *
1654
     * @return array<string, array<string, array<string, string>|string>|string>
1655
     */
1656
    protected function cleanValidationRules(array $rules, array $row): array
1657
    {
1658
        if ($row === []) {
21✔
1659
            return [];
2✔
1660
        }
1661

1662
        foreach (array_keys($rules) as $field) {
19✔
1663
            if (! array_key_exists($field, $row)) {
19✔
1664
                unset($rules[$field]);
8✔
1665
            }
1666
        }
1667

1668
        return $rules;
19✔
1669
    }
1670

1671
    /**
1672
     * Sets $tempAllowCallbacks value so that we can temporarily override
1673
     * the setting. Resets after the next method that uses triggers.
1674
     *
1675
     * @return $this
1676
     */
1677
    public function allowCallbacks(bool $val = true)
1678
    {
1679
        $this->tempAllowCallbacks = $val;
3✔
1680

1681
        return $this;
3✔
1682
    }
1683

1684
    /**
1685
     * A simple event trigger for Model Events that allows additional
1686
     * data manipulation within the model. Specifically intended for
1687
     * usage by child models this can be used to format data,
1688
     * save/load related classes, etc.
1689
     *
1690
     * It is the responsibility of the callback methods to return
1691
     * the data itself.
1692
     *
1693
     * Each $eventData array MUST have a 'data' key with the relevant
1694
     * data for callback methods (like an array of key/value pairs to insert
1695
     * or update, an array of results, etc.)
1696
     *
1697
     * If callbacks are not allowed then returns $eventData immediately.
1698
     *
1699
     * @template TEventData of array<string, mixed>
1700
     *
1701
     * @param string     $event     Valid property of the model event: $this->before*, $this->after*, etc.
1702
     * @param TEventData $eventData
1703
     *
1704
     * @return TEventData
1705
     *
1706
     * @throws DataException
1707
     */
1708
    protected function trigger(string $event, array $eventData)
1709
    {
1710
        // Ensure it's a valid event
1711
        if (! isset($this->{$event}) || $this->{$event} === []) {
210✔
1712
            return $eventData;
195✔
1713
        }
1714

1715
        foreach ($this->{$event} as $callback) {
15✔
1716
            if (! method_exists($this, $callback)) {
15✔
1717
                throw DataException::forInvalidMethodTriggered($callback);
1✔
1718
            }
1719

1720
            $eventData = $this->{$callback}($eventData);
14✔
1721
        }
1722

1723
        return $eventData;
14✔
1724
    }
1725

1726
    /**
1727
     * Sets the return type of the results to be as an associative array.
1728
     *
1729
     * @return $this
1730
     */
1731
    public function asArray()
1732
    {
1733
        $this->tempReturnType = 'array';
31✔
1734

1735
        return $this;
31✔
1736
    }
1737

1738
    /**
1739
     * Sets the return type to be of the specified type of object.
1740
     * Defaults to a simple object, but can be any class that has
1741
     * class vars with the same name as the collection columns,
1742
     * or at least allows them to be created.
1743
     *
1744
     * @param 'object'|class-string $class
1745
     *
1746
     * @return $this
1747
     */
1748
    public function asObject(string $class = 'object')
1749
    {
1750
        $this->tempReturnType = $class;
18✔
1751

1752
        return $this;
18✔
1753
    }
1754

1755
    /**
1756
     * Takes a class and returns an array of its public and protected
1757
     * properties as an array suitable for use in creates and updates.
1758
     * This method uses `$this->objectToRawArray()` internally and does conversion
1759
     * to string on all Time instances.
1760
     *
1761
     * @param object $object
1762
     * @param bool   $onlyChanged Returns only the changed properties.
1763
     * @param bool   $recursive   If `true`, inner entities will be cast as array as well.
1764
     *
1765
     * @return array<string, mixed>
1766
     *
1767
     * @throws ReflectionException
1768
     */
1769
    protected function objectToArray($object, bool $onlyChanged = true, bool $recursive = false): array
1770
    {
1771
        $properties = $this->objectToRawArray($object, $onlyChanged, $recursive);
24✔
1772

1773
        // Convert any Time instances to appropriate $dateFormat
1774
        return $this->timeToString($properties);
24✔
1775
    }
1776

1777
    /**
1778
     * Convert any Time instances to appropriate $dateFormat.
1779
     *
1780
     * @param array<string, mixed> $properties
1781
     *
1782
     * @return array<string, mixed>
1783
     */
1784
    protected function timeToString(array $properties): array
1785
    {
1786
        if ($properties === []) {
163✔
1787
            return [];
1✔
1788
        }
1789

1790
        return array_map(function ($value) {
162✔
1791
            if ($value instanceof Time) {
162✔
1792
                return $this->timeToDate($value);
5✔
1793
            }
1794

1795
            return $value;
162✔
1796
        }, $properties);
162✔
1797
    }
1798

1799
    /**
1800
     * Takes a class and returns an array of its public and protected
1801
     * properties as an array with raw values.
1802
     *
1803
     * @param object $object
1804
     * @param bool   $onlyChanged Returns only the changed properties.
1805
     * @param bool   $recursive   If `true`, inner entities will be cast as array as well.
1806
     *
1807
     * @return array<string, mixed> Array with raw values
1808
     *
1809
     * @throws ReflectionException
1810
     */
1811
    protected function objectToRawArray($object, bool $onlyChanged = true, bool $recursive = false): array
1812
    {
1813
        // Entity::toRawArray() returns array
1814
        if (method_exists($object, 'toRawArray')) {
24✔
1815
            $properties = $object->toRawArray($onlyChanged, $recursive);
22✔
1816
        } else {
1817
            $mirror = new ReflectionClass($object);
2✔
1818
            $props  = $mirror->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
2✔
1819

1820
            $properties = [];
2✔
1821

1822
            // Loop over each property,
1823
            // saving the name/value in a new array we can return
1824
            foreach ($props as $prop) {
2✔
1825
                $properties[$prop->getName()] = $prop->getValue($object);
2✔
1826
            }
1827
        }
1828

1829
        return $properties;
24✔
1830
    }
1831

1832
    /**
1833
     * Transform data to array.
1834
     *
1835
     * @param object|row_array|null $row
1836
     *
1837
     * @return array<int|string, mixed>
1838
     *
1839
     * @throws DataException
1840
     * @throws InvalidArgumentException
1841
     * @throws ReflectionException
1842
     *
1843
     * @used-by insert()
1844
     * @used-by insertBatch()
1845
     * @used-by update()
1846
     * @used-by updateBatch()
1847
     */
1848
    protected function transformDataToArray($row, string $type): array
1849
    {
1850
        if (! in_array($type, ['insert', 'update'], true)) {
167✔
1851
            throw new InvalidArgumentException(sprintf('Invalid type "%s" used upon transforming data to array.', $type));
1✔
1852
        }
1853

1854
        if (! $this->allowEmptyInserts && ($row === null || (array) $row === [])) {
166✔
1855
            throw DataException::forEmptyDataset($type);
6✔
1856
        }
1857

1858
        // If it validates with entire rules, all fields are needed.
1859
        if ($this->skipValidation === false && $this->cleanValidationRules === false) {
163✔
1860
            $onlyChanged = false;
143✔
1861
        } else {
1862
            $onlyChanged = ($type === 'update' && $this->updateOnlyChanged);
49✔
1863
        }
1864

1865
        if ($this->useCasts()) {
163✔
1866
            if (is_array($row)) {
27✔
1867
                $row = $this->converter->toDataSource($row);
27✔
1868
            } elseif ($row instanceof stdClass) {
7✔
1869
                $row = (array) $row;
3✔
1870
                $row = $this->converter->toDataSource($row);
3✔
1871
            } elseif ($row instanceof Entity) {
4✔
1872
                $row = $this->converter->extract($row, $onlyChanged);
2✔
1873
            } elseif (is_object($row)) {
2✔
1874
                $row = $this->converter->extract($row, $onlyChanged);
2✔
1875
            }
1876
        }
1877
        // If $row is using a custom class with public or protected
1878
        // properties representing the collection elements, we need to grab
1879
        // them as an array.
1880
        elseif (is_object($row) && ! $row instanceof stdClass) {
136✔
1881
            $row = $this->objectToArray($row, $onlyChanged, true);
23✔
1882
        }
1883

1884
        // If it's still a stdClass, go ahead and convert to
1885
        // an array so doProtectFields and other model methods
1886
        // don't have to do special checks.
1887
        if (is_object($row)) {
163✔
1888
            $row = (array) $row;
13✔
1889
        }
1890

1891
        // If it's still empty here, means $row is no change or is empty object
1892
        if (! $this->allowEmptyInserts && ($row === null || $row === [])) {
163✔
UNCOV
1893
            throw DataException::forEmptyDataset($type);
×
1894
        }
1895

1896
        // Convert any Time instances to appropriate $dateFormat
1897
        return $this->timeToString($row);
163✔
1898
    }
1899

1900
    /**
1901
     * Provides the DB connection and model's properties.
1902
     *
1903
     * @return mixed
1904
     */
1905
    public function __get(string $name)
1906
    {
1907
        if (property_exists($this, $name)) {
50✔
1908
            return $this->{$name};
50✔
1909
        }
1910

1911
        return $this->db->{$name} ?? null;
1✔
1912
    }
1913

1914
    /**
1915
     * Checks for the existence of properties across this model, and DB connection.
1916
     */
1917
    public function __isset(string $name): bool
1918
    {
1919
        if (property_exists($this, $name)) {
50✔
1920
            return true;
50✔
1921
        }
1922

1923
        return isset($this->db->{$name});
1✔
1924
    }
1925

1926
    /**
1927
     * Provides direct access to method in the database connection.
1928
     *
1929
     * @param array<int|string, mixed> $params
1930
     *
1931
     * @return mixed
1932
     */
1933
    public function __call(string $name, array $params)
1934
    {
UNCOV
1935
        if (method_exists($this->db, $name)) {
×
UNCOV
1936
            return $this->db->{$name}(...$params);
×
1937
        }
1938

UNCOV
1939
        return null;
×
1940
    }
1941

1942
    /**
1943
     * Sets $allowEmptyInserts.
1944
     */
1945
    public function allowEmptyInserts(bool $value = true): self
1946
    {
1947
        $this->allowEmptyInserts = $value;
1✔
1948

1949
        return $this;
1✔
1950
    }
1951

1952
    /**
1953
     * Converts database data array to return type value.
1954
     *
1955
     * @param array<string, mixed>          $row        Raw data from database.
1956
     * @param 'array'|'object'|class-string $returnType
1957
     *
1958
     * @return array<string, mixed>|object
1959
     */
1960
    protected function convertToReturnType(array $row, string $returnType): array|object
1961
    {
1962
        if ($returnType === 'array') {
25✔
1963
            return $this->converter->fromDataSource($row);
10✔
1964
        }
1965

1966
        if ($returnType === 'object') {
17✔
1967
            return (object) $this->converter->fromDataSource($row);
5✔
1968
        }
1969

1970
        return $this->converter->reconstruct($returnType, $row);
12✔
1971
    }
1972
}
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