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

codeigniter4 / CodeIgniter4 / 22177039776

19 Feb 2026 09:58AM UTC coverage: 85.652% (+0.001%) from 85.651%
22177039776

Pull #9962

github

web-flow
Merge fd694575b into 658e1a5b1
Pull Request #9962: feat: Chunk array method in models

13 of 15 new or added lines in 1 file covered. (86.67%)

17 existing lines in 2 files now uncovered.

22320 of 26059 relevant lines covered (85.65%)

206.26 hits per line

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

95.64
/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;
398✔
376
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
398✔
377
        $this->tempAllowCallbacks = $this->allowCallbacks;
398✔
378

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

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

385
    /**
386
     * Creates DataConverter instance.
387
     */
388
    protected function createDataConverter(): void
389
    {
390
        if ($this->useCasts()) {
398✔
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 !== [];
398✔
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
    }
397✔
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
     * Loops over records in batches, allowing you to operate on each chunk at a time.
590
     * This method works only with DB calls.
591
     *
592
     * This method calls the `$userFunc` with the chunk, instead of a single record as in `chunk()`.
593
     * This allows you to operate on multiple records at once, which can be more efficient for certain operations.
594
     *
595
     * @param Closure(list<array<string, string>>|list<object>): mixed $userFunc
596
     *
597
     * @return void
598
     *
599
     * @throws DataException
600
     */
601
    abstract public function chunkArray(int $size, Closure $userFunc);
602

603
    /**
604
     * Fetches the row of database.
605
     *
606
     * @param int|list<int|string>|string|null $id One primary key or an array of primary keys.
607
     *
608
     * @return ($id is int|string ? object|row_array|null :  list<object|row_array>)
609
     */
610
    public function find($id = null)
611
    {
612
        $singleton = is_numeric($id) || is_string($id);
58✔
613

614
        if ($this->tempAllowCallbacks) {
58✔
615
            // Call the before event and check for a return
616
            $eventData = $this->trigger('beforeFind', [
55✔
617
                'id'        => $id,
55✔
618
                'method'    => 'find',
55✔
619
                'singleton' => $singleton,
55✔
620
            ]);
55✔
621

622
            if (isset($eventData['returnData']) && $eventData['returnData'] === true) {
55✔
623
                return $eventData['data'];
3✔
624
            }
625
        }
626

627
        $eventData = [
56✔
628
            'id'        => $id,
56✔
629
            'data'      => $this->doFind($singleton, $id),
56✔
630
            'method'    => 'find',
56✔
631
            'singleton' => $singleton,
56✔
632
        ];
56✔
633

634
        if ($this->tempAllowCallbacks) {
55✔
635
            $eventData = $this->trigger('afterFind', $eventData);
52✔
636
        }
637

638
        $this->tempReturnType     = $this->returnType;
55✔
639
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
55✔
640
        $this->tempAllowCallbacks = $this->allowCallbacks;
55✔
641

642
        return $eventData['data'];
55✔
643
    }
644

645
    /**
646
     * Fetches the column of database.
647
     *
648
     * @return list<bool|float|int|list<mixed>|object|string|null>|null The resulting row of data, or `null` if no data found.
649
     *
650
     * @throws DataException
651
     */
652
    public function findColumn(string $columnName)
653
    {
654
        if (str_contains($columnName, ',')) {
3✔
655
            throw DataException::forFindColumnHaveMultipleColumns();
1✔
656
        }
657

658
        $resultSet = $this->doFindColumn($columnName);
2✔
659

660
        return $resultSet !== null ? array_column($resultSet, $columnName) : null;
2✔
661
    }
662

663
    /**
664
     * Fetches all results, while optionally limiting them.
665
     *
666
     * @return list<object|row_array>
667
     */
668
    public function findAll(?int $limit = null, int $offset = 0)
669
    {
670
        $limitZeroAsAll = config(Feature::class)->limitZeroAsAll ?? true;
22✔
671
        if ($limitZeroAsAll) {
22✔
672
            $limit ??= 0;
22✔
673
        }
674

675
        if ($this->tempAllowCallbacks) {
22✔
676
            // Call the before event and check for a return
677
            $eventData = $this->trigger('beforeFind', [
22✔
678
                'method'    => 'findAll',
22✔
679
                'limit'     => $limit,
22✔
680
                'offset'    => $offset,
22✔
681
                'singleton' => false,
22✔
682
            ]);
22✔
683

684
            if (isset($eventData['returnData']) && $eventData['returnData'] === true) {
22✔
685
                return $eventData['data'];
1✔
686
            }
687
        }
688

689
        $eventData = [
22✔
690
            'data'      => $this->doFindAll($limit, $offset),
22✔
691
            'limit'     => $limit,
22✔
692
            'offset'    => $offset,
22✔
693
            'method'    => 'findAll',
22✔
694
            'singleton' => false,
22✔
695
        ];
22✔
696

697
        if ($this->tempAllowCallbacks) {
22✔
698
            $eventData = $this->trigger('afterFind', $eventData);
22✔
699
        }
700

701
        $this->tempReturnType     = $this->returnType;
22✔
702
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
22✔
703
        $this->tempAllowCallbacks = $this->allowCallbacks;
22✔
704

705
        return $eventData['data'];
22✔
706
    }
707

708
    /**
709
     * Returns the first row of the result set.
710
     *
711
     * @return object|row_array|null
712
     */
713
    public function first()
714
    {
715
        if ($this->tempAllowCallbacks) {
24✔
716
            // Call the before event and check for a return
717
            $eventData = $this->trigger('beforeFind', [
24✔
718
                'method'    => 'first',
24✔
719
                'singleton' => true,
24✔
720
            ]);
24✔
721

722
            if (isset($eventData['returnData']) && $eventData['returnData'] === true) {
24✔
723
                return $eventData['data'];
1✔
724
            }
725
        }
726

727
        $eventData = [
24✔
728
            'data'      => $this->doFirst(),
24✔
729
            'method'    => 'first',
24✔
730
            'singleton' => true,
24✔
731
        ];
24✔
732

733
        if ($this->tempAllowCallbacks) {
24✔
734
            $eventData = $this->trigger('afterFind', $eventData);
24✔
735
        }
736

737
        $this->tempReturnType     = $this->returnType;
24✔
738
        $this->tempUseSoftDeletes = $this->useSoftDeletes;
24✔
739
        $this->tempAllowCallbacks = $this->allowCallbacks;
24✔
740

741
        return $eventData['data'];
24✔
742
    }
743

744
    /**
745
     * A convenience method that will attempt to determine whether the
746
     * data should be inserted or updated.
747
     *
748
     * Will work with either an array or object.
749
     * When using with custom class objects,
750
     * you must ensure that the class will provide access to the class
751
     * variables, even if through a magic method.
752
     *
753
     * @param object|row_array $row
754
     *
755
     * @throws ReflectionException
756
     */
757
    public function save($row): bool
758
    {
759
        if ((array) $row === []) {
28✔
760
            return true;
1✔
761
        }
762

763
        if ($this->shouldUpdate($row)) {
27✔
764
            $response = $this->update($this->getIdValue($row), $row);
17✔
765
        } else {
766
            $response = $this->insert($row, false);
14✔
767

768
            if ($response !== false) {
13✔
769
                $response = true;
12✔
770
            }
771
        }
772

773
        return $response;
26✔
774
    }
775

776
    /**
777
     * This method is called on save to determine if entry have to be updated.
778
     * If this method returns `false` insert operation will be executed.
779
     *
780
     * @param object|row_array $row
781
     */
782
    protected function shouldUpdate($row): bool
783
    {
784
        $id = $this->getIdValue($row);
27✔
785

786
        return ! in_array($id, [null, [], ''], true);
27✔
787
    }
788

789
    /**
790
     * Returns last insert ID or 0.
791
     *
792
     * @return int|string
793
     */
794
    public function getInsertID()
795
    {
796
        return is_numeric($this->insertID) ? (int) $this->insertID : $this->insertID;
11✔
797
    }
798

799
    /**
800
     * Validates that the primary key values are valid for update/delete/insert operations.
801
     * Throws exception if invalid.
802
     *
803
     * @param bool $allowArray Whether to allow array of IDs (true for update/delete, false for insert)
804
     *
805
     * @phpstan-assert non-zero-int|non-empty-list<int|string>|RawSql|non-falsy-string $id
806
     * @throws         InvalidArgumentException
807
     */
808
    protected function validateID(mixed $id, bool $allowArray = true): void
809
    {
810
        if (is_array($id)) {
143✔
811
            // Check if arrays are allowed
812
            if (! $allowArray) {
130✔
813
                throw new InvalidArgumentException(
8✔
814
                    'Invalid primary key: only a single value is allowed, not an array.',
8✔
815
                );
8✔
816
            }
817

818
            // Check for empty array
819
            if ($id === []) {
122✔
820
                throw new InvalidArgumentException('Invalid primary key: cannot be an empty array.');
5✔
821
            }
822

823
            // Validate each ID in the array recursively
824
            foreach ($id as $key => $valueId) {
117✔
825
                if (is_array($valueId)) {
117✔
826
                    throw new InvalidArgumentException(
5✔
827
                        sprintf('Invalid primary key at index %s: nested arrays are not allowed.', $key),
5✔
828
                    );
5✔
829
                }
830

831
                // Recursive call for each value (single values only in recursion)
832
                $this->validateID($valueId, false);
112✔
833
            }
834

835
            return;
62✔
836
        }
837

838
        // Allow RawSql objects for complex scenarios
839
        if ($id instanceof RawSql) {
129✔
840
            return;
2✔
841
        }
842

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

847
            throw new InvalidArgumentException(
60✔
848
                sprintf('Invalid primary key: %s is not allowed.', $type),
60✔
849
            );
60✔
850
        }
851

852
        // Only allow int and string at this point
853
        if (! is_int($id) && ! is_string($id)) {
97✔
UNCOV
854
            throw new InvalidArgumentException(
×
855
                sprintf('Invalid primary key: must be int or string, %s given.', get_debug_type($id)),
×
856
            );
×
857
        }
858
    }
859

860
    /**
861
     * Inserts data into the database. If an object is provided,
862
     * it will attempt to convert it to an array.
863
     *
864
     * @param object|row_array|null $row
865
     * @param bool                  $returnID Whether insert ID should be returned or not.
866
     *
867
     * @return ($returnID is true ? false|int|string : bool)
868
     *
869
     * @throws ReflectionException
870
     */
871
    public function insert($row = null, bool $returnID = true)
872
    {
873
        $this->insertID = 0;
119✔
874

875
        // Set $cleanValidationRules to false temporary.
876
        $cleanValidationRules       = $this->cleanValidationRules;
119✔
877
        $this->cleanValidationRules = false;
119✔
878

879
        $row = $this->transformDataToArray($row, 'insert');
119✔
880

881
        // Validate data before saving.
882
        if (! $this->skipValidation && ! $this->validate($row)) {
117✔
883
            // Restore $cleanValidationRules
884
            $this->cleanValidationRules = $cleanValidationRules;
19✔
885

886
            return false;
19✔
887
        }
888

889
        // Restore $cleanValidationRules
890
        $this->cleanValidationRules = $cleanValidationRules;
100✔
891

892
        // Must be called first, so we don't
893
        // strip out created_at values.
894
        $row = $this->doProtectFieldsForInsert($row);
100✔
895

896
        // doProtectFields() can further remove elements from
897
        // $row, so we need to check for empty dataset again
898
        if (! $this->allowEmptyInserts && $row === []) {
99✔
899
            throw DataException::forEmptyDataset('insert');
2✔
900
        }
901

902
        // Set created_at and updated_at with same time
903
        $date = $this->setDate();
97✔
904
        $row  = $this->setCreatedField($row, $date);
97✔
905
        $row  = $this->setUpdatedField($row, $date);
97✔
906

907
        $eventData = ['data' => $row];
97✔
908

909
        if ($this->tempAllowCallbacks) {
97✔
910
            $eventData = $this->trigger('beforeInsert', $eventData);
97✔
911
        }
912

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

915
        $eventData = [
85✔
916
            'id'     => $this->insertID,
85✔
917
            'data'   => $eventData['data'],
85✔
918
            'result' => $result,
85✔
919
        ];
85✔
920

921
        if ($this->tempAllowCallbacks) {
85✔
922
            // Trigger afterInsert events with the inserted data and new ID
923
            $this->trigger('afterInsert', $eventData);
85✔
924
        }
925

926
        $this->tempAllowCallbacks = $this->allowCallbacks;
85✔
927

928
        // If insertion failed, get out of here
929
        if (! $result) {
85✔
930
            return $result;
2✔
931
        }
932

933
        // otherwise return the insertID, if requested.
934
        return $returnID ? $this->insertID : $result;
83✔
935
    }
936

937
    /**
938
     * Set datetime to created field.
939
     *
940
     * @param row_array  $row
941
     * @param int|string $date Timestamp or datetime string.
942
     *
943
     * @return row_array
944
     */
945
    protected function setCreatedField(array $row, $date): array
946
    {
947
        if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $row)) {
119✔
948
            $row[$this->createdField] = $date;
39✔
949
        }
950

951
        return $row;
119✔
952
    }
953

954
    /**
955
     * Set datetime to updated field.
956
     *
957
     * @param row_array  $row
958
     * @param int|string $date Timestamp or datetime string
959
     *
960
     * @return row_array
961
     */
962
    protected function setUpdatedField(array $row, $date): array
963
    {
964
        if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
136✔
965
            $row[$this->updatedField] = $date;
43✔
966
        }
967

968
        return $row;
136✔
969
    }
970

971
    /**
972
     * Compiles batch insert runs the queries, validating each row prior.
973
     *
974
     * @param list<object|row_array>|null $set       An associative array of insert values.
975
     * @param bool|null                   $escape    Whether to escape values.
976
     * @param int                         $batchSize The size of the batch to run.
977
     * @param bool                        $testing   `true` means only number of records is returned, `false` will execute the query.
978
     *
979
     * @return false|int|list<string> Number of rows inserted or `false` on failure.
980
     *
981
     * @throws ReflectionException
982
     */
983
    public function insertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false)
984
    {
985
        // Set $cleanValidationRules to false temporary.
986
        $cleanValidationRules       = $this->cleanValidationRules;
24✔
987
        $this->cleanValidationRules = false;
24✔
988

989
        if (is_array($set)) {
24✔
990
            foreach ($set as &$row) {
24✔
991
                $row = $this->transformDataToArray($row, 'insert');
24✔
992

993
                // Validate every row.
994
                if (! $this->skipValidation && ! $this->validate($row)) {
24✔
995
                    // Restore $cleanValidationRules
996
                    $this->cleanValidationRules = $cleanValidationRules;
3✔
997

998
                    return false;
3✔
999
                }
1000

1001
                // Must be called first so we don't
1002
                // strip out created_at values.
1003
                $row = $this->doProtectFieldsForInsert($row);
21✔
1004

1005
                // Set created_at and updated_at with same time
1006
                $date = $this->setDate();
21✔
1007
                $row  = $this->setCreatedField($row, $date);
21✔
1008
                $row  = $this->setUpdatedField($row, $date);
21✔
1009
            }
1010
        }
1011

1012
        // Restore $cleanValidationRules
1013
        $this->cleanValidationRules = $cleanValidationRules;
21✔
1014

1015
        $eventData = ['data' => $set];
21✔
1016

1017
        if ($this->tempAllowCallbacks) {
21✔
1018
            $eventData = $this->trigger('beforeInsertBatch', $eventData);
21✔
1019
        }
1020

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

1023
        $eventData = [
11✔
1024
            'data'   => $eventData['data'],
11✔
1025
            'result' => $result,
11✔
1026
        ];
11✔
1027

1028
        if ($this->tempAllowCallbacks) {
11✔
1029
            // Trigger afterInsert events with the inserted data and new ID
1030
            $this->trigger('afterInsertBatch', $eventData);
11✔
1031
        }
1032

1033
        $this->tempAllowCallbacks = $this->allowCallbacks;
11✔
1034

1035
        return $result;
11✔
1036
    }
1037

1038
    /**
1039
     * Updates a single record in the database. If an object is provided,
1040
     * it will attempt to convert it into an array.
1041
     *
1042
     * @param int|list<int|string>|RawSql|string|null $id
1043
     * @param object|row_array|null                   $row
1044
     *
1045
     * @throws ReflectionException
1046
     */
1047
    public function update($id = null, $row = null): bool
1048
    {
1049
        if ($id !== null) {
58✔
1050
            if (! is_array($id)) {
52✔
1051
                $id = [$id];
44✔
1052
            }
1053

1054
            $this->validateID($id);
52✔
1055
        }
1056

1057
        $row = $this->transformDataToArray($row, 'update');
46✔
1058

1059
        // Validate data before saving.
1060
        if (! $this->skipValidation && ! $this->validate($row)) {
44✔
1061
            return false;
4✔
1062
        }
1063

1064
        // Must be called first, so we don't
1065
        // strip out updated_at values.
1066
        $row = $this->doProtectFields($row);
40✔
1067

1068
        // doProtectFields() can further remove elements from
1069
        // $row, so we need to check for empty dataset again
1070
        if ($row === []) {
40✔
1071
            throw DataException::forEmptyDataset('update');
2✔
1072
        }
1073

1074
        $row = $this->setUpdatedField($row, $this->setDate());
38✔
1075

1076
        $eventData = [
38✔
1077
            'id'   => $id,
38✔
1078
            'data' => $row,
38✔
1079
        ];
38✔
1080

1081
        if ($this->tempAllowCallbacks) {
38✔
1082
            $eventData = $this->trigger('beforeUpdate', $eventData);
38✔
1083
        }
1084

1085
        $eventData = [
38✔
1086
            'id'     => $id,
38✔
1087
            'data'   => $eventData['data'],
38✔
1088
            'result' => $this->doUpdate($id, $eventData['data']),
38✔
1089
        ];
38✔
1090

1091
        if ($this->tempAllowCallbacks) {
37✔
1092
            $this->trigger('afterUpdate', $eventData);
37✔
1093
        }
1094

1095
        $this->tempAllowCallbacks = $this->allowCallbacks;
37✔
1096

1097
        return $eventData['result'];
37✔
1098
    }
1099

1100
    /**
1101
     * Compiles an update and runs the query.
1102
     *
1103
     * @param list<object|row_array>|null $set       An associative array of insert values.
1104
     * @param string|null                 $index     The where key.
1105
     * @param int                         $batchSize The size of the batch to run.
1106
     * @param bool                        $returnSQL `true` means SQL is returned, `false` will execute the query.
1107
     *
1108
     * @return false|int|list<string> Number of rows affected or `false` on failure, SQL array when test mode.
1109
     *
1110
     * @throws DatabaseException
1111
     * @throws ReflectionException
1112
     */
1113
    public function updateBatch(?array $set = null, ?string $index = null, int $batchSize = 100, bool $returnSQL = false)
1114
    {
1115
        if (is_array($set)) {
7✔
1116
            foreach ($set as &$row) {
7✔
1117
                // Save the index value from the original row because
1118
                // transformDataToArray() may strip it when updateOnlyChanged
1119
                // is true.
1120
                $updateIndex = null;
7✔
1121

1122
                if ($this->updateOnlyChanged) {
7✔
1123
                    if (is_array($row)) {
6✔
1124
                        $updateIndex = $row[$index] ?? null;
5✔
1125
                    } elseif ($row instanceof Entity) {
1✔
1126
                        $updateIndex = $row->toRawArray()[$index] ?? null;
1✔
UNCOV
1127
                    } elseif (is_object($row)) {
×
1128
                        $updateIndex = $row->{$index} ?? null;
×
1129
                    }
1130
                }
1131

1132
                $row = $this->transformDataToArray($row, 'update');
7✔
1133

1134
                // Validate data before saving.
1135
                if (! $this->skipValidation && ! $this->validate($row)) {
7✔
1136
                    return false;
1✔
1137
                }
1138

1139
                // When updateOnlyChanged is true, restore the pre-extracted
1140
                // index into $row. Otherwise read it from the transformed row.
1141
                if ($updateIndex !== null) {
6✔
1142
                    $row[$index] = $updateIndex;
4✔
1143
                } else {
1144
                    $updateIndex = $row[$index] ?? null;
2✔
1145
                }
1146

1147
                if ($updateIndex === null) {
6✔
1148
                    throw new InvalidArgumentException(
1✔
1149
                        'The index ("' . $index . '") for updateBatch() is missing in the data: '
1✔
1150
                        . json_encode($row),
1✔
1151
                    );
1✔
1152
                }
1153

1154
                // Must be called first so we don't
1155
                // strip out updated_at values.
1156
                $row = $this->doProtectFields($row);
5✔
1157

1158
                // Restore updateIndex value in case it was wiped out
1159
                $row[$index] = $updateIndex;
5✔
1160

1161
                $row = $this->setUpdatedField($row, $this->setDate());
5✔
1162
            }
1163
        }
1164

1165
        $eventData = ['data' => $set];
5✔
1166

1167
        if ($this->tempAllowCallbacks) {
5✔
1168
            $eventData = $this->trigger('beforeUpdateBatch', $eventData);
5✔
1169
        }
1170

1171
        $result = $this->doUpdateBatch($eventData['data'], $index, $batchSize, $returnSQL);
5✔
1172

1173
        $eventData = [
5✔
1174
            'data'   => $eventData['data'],
5✔
1175
            'result' => $result,
5✔
1176
        ];
5✔
1177

1178
        if ($this->tempAllowCallbacks) {
5✔
1179
            // Trigger afterInsert events with the inserted data and new ID
1180
            $this->trigger('afterUpdateBatch', $eventData);
5✔
1181
        }
1182

1183
        $this->tempAllowCallbacks = $this->allowCallbacks;
5✔
1184

1185
        return $result;
5✔
1186
    }
1187

1188
    /**
1189
     * Deletes a single record from the database where $id matches.
1190
     *
1191
     * @param int|list<int|string>|RawSql|string|null $id    The rows primary key(s).
1192
     * @param bool                                    $purge Allows overriding the soft deletes setting.
1193
     *
1194
     * @return bool|string Returns a SQL string if in test mode.
1195
     *
1196
     * @throws DatabaseException
1197
     */
1198
    public function delete($id = null, bool $purge = false)
1199
    {
1200
        if ($id !== null) {
84✔
1201
            if (! is_array($id)) {
70✔
1202
                $id = [$id];
41✔
1203
            }
1204

1205
            $this->validateID($id);
70✔
1206
        }
1207

1208
        $eventData = [
36✔
1209
            'id'    => $id,
36✔
1210
            'purge' => $purge,
36✔
1211
        ];
36✔
1212

1213
        if ($this->tempAllowCallbacks) {
36✔
1214
            $this->trigger('beforeDelete', $eventData);
35✔
1215
        }
1216

1217
        $eventData = [
36✔
1218
            'id'     => $id,
36✔
1219
            'data'   => null,
36✔
1220
            'purge'  => $purge,
36✔
1221
            'result' => $this->doDelete($id, $purge),
36✔
1222
        ];
36✔
1223

1224
        if ($this->tempAllowCallbacks) {
31✔
1225
            $this->trigger('afterDelete', $eventData);
30✔
1226
        }
1227

1228
        $this->tempAllowCallbacks = $this->allowCallbacks;
31✔
1229

1230
        return $eventData['result'];
31✔
1231
    }
1232

1233
    /**
1234
     * Permanently deletes all rows that have been marked as deleted
1235
     * through soft deletes (value of column $deletedField is not null).
1236
     *
1237
     * @return bool|string Returns a SQL string if in test mode.
1238
     */
1239
    public function purgeDeleted()
1240
    {
1241
        if (! $this->useSoftDeletes) {
2✔
1242
            return true;
1✔
1243
        }
1244

1245
        return $this->doPurgeDeleted();
1✔
1246
    }
1247

1248
    /**
1249
     * Sets $useSoftDeletes value so that we can temporarily override
1250
     * the soft deletes settings. Can be used for all find* methods.
1251
     *
1252
     * @return $this
1253
     */
1254
    public function withDeleted(bool $val = true)
1255
    {
1256
        $this->tempUseSoftDeletes = ! $val;
23✔
1257

1258
        return $this;
23✔
1259
    }
1260

1261
    /**
1262
     * Works with the $this->find* methods to return only the rows that
1263
     * have been deleted.
1264
     *
1265
     * @return $this
1266
     */
1267
    public function onlyDeleted()
1268
    {
1269
        $this->tempUseSoftDeletes = false;
1✔
1270
        $this->doOnlyDeleted();
1✔
1271

1272
        return $this;
1✔
1273
    }
1274

1275
    /**
1276
     * Compiles a replace and runs the query.
1277
     *
1278
     * @param row_array|null $row
1279
     * @param bool           $returnSQL `true` means SQL is returned, `false` will execute the query.
1280
     *
1281
     * @return BaseResult|false|Query|string
1282
     */
1283
    public function replace(?array $row = null, bool $returnSQL = false)
1284
    {
1285
        // Validate data before saving.
1286
        if (($row !== null) && ! $this->skipValidation && ! $this->validate($row)) {
3✔
1287
            return false;
1✔
1288
        }
1289

1290
        $row = (array) $row;
2✔
1291
        $row = $this->setCreatedField($row, $this->setDate());
2✔
1292
        $row = $this->setUpdatedField($row, $this->setDate());
2✔
1293

1294
        return $this->doReplace($row, $returnSQL);
2✔
1295
    }
1296

1297
    /**
1298
     * Grabs the last error(s) that occurred.
1299
     *
1300
     * If data was validated, it will first check for errors there,
1301
     *  otherwise will try to grab the last error from the Database connection.
1302
     *
1303
     * The return array should be in the following format:
1304
     *  `['source' => 'message']`.
1305
     *
1306
     * @param bool $forceDB Always grab the DB error, not validation.
1307
     *
1308
     * @return array<string, string>
1309
     */
1310
    public function errors(bool $forceDB = false)
1311
    {
1312
        if ($this->validation === null) {
24✔
UNCOV
1313
            return $this->doErrors();
×
1314
        }
1315

1316
        // Do we have validation errors?
1317
        if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors()) !== []) {
24✔
1318
            return $errors;
22✔
1319
        }
1320

1321
        return $this->doErrors();
2✔
1322
    }
1323

1324
    /**
1325
     * Works with Pager to get the size and offset parameters.
1326
     * Expects a GET variable (?page=2) that specifies the page of results
1327
     * to display.
1328
     *
1329
     * @param int|null $perPage Items per page.
1330
     * @param string   $group   Will be used by the pagination library to identify a unique pagination set.
1331
     * @param int|null $page    Optional page number (useful when the page number is provided in different way).
1332
     * @param int      $segment Optional URI segment number (if page number is provided by URI segment).
1333
     *
1334
     * @return list<object|row_array>
1335
     */
1336
    public function paginate(?int $perPage = null, string $group = 'default', ?int $page = null, int $segment = 0)
1337
    {
1338
        // Since multiple models may use the Pager, the Pager must be shared.
1339
        $pager = service('pager');
8✔
1340

1341
        if ($segment !== 0) {
8✔
UNCOV
1342
            $pager->setSegment($segment, $group);
×
1343
        }
1344

1345
        $page = $page >= 1 ? $page : $pager->getCurrentPage($group);
8✔
1346
        // Store it in the Pager library, so it can be paginated in the views.
1347
        $this->pager = $pager->store($group, $page, $perPage, $this->countAllResults(false), $segment);
8✔
1348
        $perPage     = $this->pager->getPerPage($group);
8✔
1349
        $offset      = ($pager->getCurrentPage($group) - 1) * $perPage;
8✔
1350

1351
        return $this->findAll($perPage, $offset);
8✔
1352
    }
1353

1354
    /**
1355
     * It could be used when you have to change default or override current allowed fields.
1356
     *
1357
     * @param list<string> $allowedFields Array with names of fields.
1358
     *
1359
     * @return $this
1360
     */
1361
    public function setAllowedFields(array $allowedFields)
1362
    {
1363
        $this->allowedFields = $allowedFields;
10✔
1364

1365
        return $this;
10✔
1366
    }
1367

1368
    /**
1369
     * Sets whether or not we should whitelist data set during
1370
     * updates or inserts against $this->availableFields.
1371
     *
1372
     * @return $this
1373
     */
1374
    public function protect(bool $protect = true)
1375
    {
1376
        $this->protectFields = $protect;
12✔
1377

1378
        return $this;
12✔
1379
    }
1380

1381
    /**
1382
     * Ensures that only the fields that are allowed to be updated are
1383
     * in the data array.
1384
     *
1385
     * @used-by update() to protect against mass assignment vulnerabilities.
1386
     * @used-by updateBatch() to protect against mass assignment vulnerabilities.
1387
     *
1388
     * @param row_array $row
1389
     *
1390
     * @return row_array
1391
     *
1392
     * @throws DataException
1393
     */
1394
    protected function doProtectFields(array $row): array
1395
    {
1396
        if (! $this->protectFields) {
45✔
1397
            return $row;
2✔
1398
        }
1399

1400
        if ($this->allowedFields === []) {
43✔
UNCOV
1401
            throw DataException::forInvalidAllowedFields(static::class);
×
1402
        }
1403

1404
        foreach (array_keys($row) as $key) {
43✔
1405
            if (! in_array($key, $this->allowedFields, true)) {
43✔
1406
                unset($row[$key]);
24✔
1407
            }
1408
        }
1409

1410
        return $row;
43✔
1411
    }
1412

1413
    /**
1414
     * Ensures that only the fields that are allowed to be inserted are in
1415
     * the data array.
1416
     *
1417
     * @used-by insert() to protect against mass assignment vulnerabilities.
1418
     * @used-by insertBatch() to protect against mass assignment vulnerabilities.
1419
     *
1420
     * @param row_array $row
1421
     *
1422
     * @return row_array
1423
     *
1424
     * @throws DataException
1425
     */
1426
    protected function doProtectFieldsForInsert(array $row): array
1427
    {
UNCOV
1428
        return $this->doProtectFields($row);
×
1429
    }
1430

1431
    /**
1432
     * Sets the timestamp or current timestamp if null value is passed.
1433
     *
1434
     * @param int|null $userDate An optional PHP timestamp to be converted
1435
     *
1436
     * @return int|string
1437
     *
1438
     * @throws ModelException
1439
     */
1440
    protected function setDate(?int $userDate = null)
1441
    {
1442
        $currentDate = $userDate ?? Time::now()->getTimestamp();
158✔
1443

1444
        return $this->intToDate($currentDate);
158✔
1445
    }
1446

1447
    /**
1448
     * A utility function to allow child models to use the type of
1449
     * date/time format that they prefer. This is primarily used for
1450
     * setting created_at, updated_at and deleted_at values, but can be
1451
     * used by inheriting classes.
1452
     *
1453
     * The available time formats are:
1454
     *  - 'int'      - Stores the date as an integer timestamp.
1455
     *  - 'datetime' - Stores the data in the SQL datetime format.
1456
     *  - 'date'     - Stores the date (only) in the SQL date format.
1457
     *
1458
     * @return int|string
1459
     *
1460
     * @throws ModelException
1461
     */
1462
    protected function intToDate(int $value)
1463
    {
1464
        return match ($this->dateFormat) {
158✔
1465
            'int'      => $value,
36✔
1466
            'datetime' => date($this->db->dateFormat['datetime'], $value),
120✔
1467
            'date'     => date($this->db->dateFormat['date'], $value),
1✔
1468
            default    => throw ModelException::forNoDateFormat(static::class),
158✔
1469
        };
158✔
1470
    }
1471

1472
    /**
1473
     * Converts Time value to string using $this->dateFormat.
1474
     *
1475
     * The available time formats are:
1476
     *  - 'int'      - Stores the date as an integer timestamp.
1477
     *  - 'datetime' - Stores the data in the SQL datetime format.
1478
     *  - 'date'     - Stores the date (only) in the SQL date format.
1479
     *
1480
     * @return int|string
1481
     */
1482
    protected function timeToDate(Time $value)
1483
    {
1484
        return match ($this->dateFormat) {
5✔
1485
            'datetime' => $value->format($this->db->dateFormat['datetime']),
3✔
1486
            'date'     => $value->format($this->db->dateFormat['date']),
1✔
1487
            'int'      => $value->getTimestamp(),
1✔
1488
            default    => (string) $value,
5✔
1489
        };
5✔
1490
    }
1491

1492
    /**
1493
     * Set the value of the $skipValidation flag.
1494
     *
1495
     * @return $this
1496
     */
1497
    public function skipValidation(bool $skip = true)
1498
    {
1499
        $this->skipValidation = $skip;
2✔
1500

1501
        return $this;
2✔
1502
    }
1503

1504
    /**
1505
     * Allows to set (and reset) validation messages.
1506
     * It could be used when you have to change default or override current validate messages.
1507
     *
1508
     * @param array<string, array<string, string>> $validationMessages
1509
     *
1510
     * @return $this
1511
     */
1512
    public function setValidationMessages(array $validationMessages)
1513
    {
UNCOV
1514
        $this->validationMessages = $validationMessages;
×
1515

UNCOV
1516
        return $this;
×
1517
    }
1518

1519
    /**
1520
     * Allows to set field wise validation message.
1521
     * It could be used when you have to change default or override current validate messages.
1522
     *
1523
     * @param array<string, string> $fieldMessages
1524
     *
1525
     * @return $this
1526
     */
1527
    public function setValidationMessage(string $field, array $fieldMessages)
1528
    {
1529
        $this->validationMessages[$field] = $fieldMessages;
2✔
1530

1531
        return $this;
2✔
1532
    }
1533

1534
    /**
1535
     * Allows to set (and reset) validation rules.
1536
     * It could be used when you have to change default or override current validate rules.
1537
     *
1538
     * @param array<string, array<string, array<string, string>|string>|string> $validationRules
1539
     *
1540
     * @return $this
1541
     */
1542
    public function setValidationRules(array $validationRules)
1543
    {
1544
        $this->validationRules = $validationRules;
2✔
1545

1546
        return $this;
2✔
1547
    }
1548

1549
    /**
1550
     * Allows to set field wise validation rules.
1551
     * It could be used when you have to change default or override current validate rules.
1552
     *
1553
     * @param array<string, array<string, string>|string>|string $fieldRules
1554
     *
1555
     * @return $this
1556
     */
1557
    public function setValidationRule(string $field, $fieldRules)
1558
    {
1559
        $rules = $this->validationRules;
2✔
1560

1561
        // ValidationRules can be either a string, which is the group name,
1562
        // or an array of rules.
1563
        if (is_string($rules)) {
2✔
1564
            $this->ensureValidation();
1✔
1565

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

1568
            $this->validationRules = $rules;
1✔
1569
            $this->validationMessages += $customErrors;
1✔
1570
        }
1571

1572
        $this->validationRules[$field] = $fieldRules;
2✔
1573

1574
        return $this;
2✔
1575
    }
1576

1577
    /**
1578
     * Should validation rules be removed before saving?
1579
     * Most handy when doing updates.
1580
     *
1581
     * @return $this
1582
     */
1583
    public function cleanRules(bool $choice = false)
1584
    {
1585
        $this->cleanValidationRules = $choice;
2✔
1586

1587
        return $this;
2✔
1588
    }
1589

1590
    /**
1591
     * Validate the row data against the validation rules (or the validation group)
1592
     * specified in the class property, $validationRules.
1593
     *
1594
     * @param object|row_array $row
1595
     */
1596
    public function validate($row): bool
1597
    {
1598
        if ($this->skipValidation) {
173✔
UNCOV
1599
            return true;
×
1600
        }
1601

1602
        $rules = $this->getValidationRules();
173✔
1603

1604
        if ($rules === []) {
173✔
1605
            return true;
127✔
1606
        }
1607

1608
        // Validation requires array, so cast away.
1609
        if (is_object($row)) {
46✔
1610
            $row = (array) $row;
2✔
1611
        }
1612

1613
        if ($row === []) {
46✔
UNCOV
1614
            return true;
×
1615
        }
1616

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

1619
        // If no data existed that needs validation
1620
        // our job is done here.
1621
        if ($rules === []) {
46✔
1622
            return true;
2✔
1623
        }
1624

1625
        $this->ensureValidation();
44✔
1626

1627
        $this->validation->reset()->setRules($rules, $this->validationMessages);
44✔
1628

1629
        return $this->validation->run($row, null, $this->DBGroup);
44✔
1630
    }
1631

1632
    /**
1633
     * Returns the model's defined validation rules so that they
1634
     * can be used elsewhere, if needed.
1635
     *
1636
     * @param array{only?: list<string>, except?: list<string>} $options Filter the list of rules
1637
     *
1638
     * @return array<string, array<string, array<string, string>|string>|string>
1639
     */
1640
    public function getValidationRules(array $options = []): array
1641
    {
1642
        $rules = $this->validationRules;
175✔
1643

1644
        // ValidationRules can be either a string, which is the group name,
1645
        // or an array of rules.
1646
        if (is_string($rules)) {
175✔
1647
            $this->ensureValidation();
13✔
1648

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

1651
            $this->validationMessages += $customErrors;
13✔
1652
        }
1653

1654
        if (isset($options['except'])) {
175✔
UNCOV
1655
            $rules = array_diff_key($rules, array_flip($options['except']));
×
1656
        } elseif (isset($options['only'])) {
175✔
UNCOV
1657
            $rules = array_intersect_key($rules, array_flip($options['only']));
×
1658
        }
1659

1660
        return $rules;
175✔
1661
    }
1662

1663
    protected function ensureValidation(): void
1664
    {
1665
        if ($this->validation === null) {
45✔
1666
            $this->validation = service('validation', null, false);
29✔
1667
        }
1668
    }
1669

1670
    /**
1671
     * Returns the model's validation messages, so they
1672
     * can be used elsewhere, if needed.
1673
     *
1674
     * @return array<string, array<string, string>>
1675
     */
1676
    public function getValidationMessages(): array
1677
    {
1678
        return $this->validationMessages;
2✔
1679
    }
1680

1681
    /**
1682
     * Removes any rules that apply to fields that have not been set
1683
     * currently so that rules don't block updating when only updating
1684
     * a partial row.
1685
     *
1686
     * @param array<string, array<string, array<string, string>|string>|string> $rules
1687
     * @param row_array                                                         $row
1688
     *
1689
     * @return array<string, array<string, array<string, string>|string>|string>
1690
     */
1691
    protected function cleanValidationRules(array $rules, array $row): array
1692
    {
1693
        if ($row === []) {
21✔
1694
            return [];
2✔
1695
        }
1696

1697
        foreach (array_keys($rules) as $field) {
19✔
1698
            if (! array_key_exists($field, $row)) {
19✔
1699
                unset($rules[$field]);
8✔
1700
            }
1701
        }
1702

1703
        return $rules;
19✔
1704
    }
1705

1706
    /**
1707
     * Sets $tempAllowCallbacks value so that we can temporarily override
1708
     * the setting. Resets after the next method that uses triggers.
1709
     *
1710
     * @return $this
1711
     */
1712
    public function allowCallbacks(bool $val = true)
1713
    {
1714
        $this->tempAllowCallbacks = $val;
3✔
1715

1716
        return $this;
3✔
1717
    }
1718

1719
    /**
1720
     * A simple event trigger for Model Events that allows additional
1721
     * data manipulation within the model. Specifically intended for
1722
     * usage by child models this can be used to format data,
1723
     * save/load related classes, etc.
1724
     *
1725
     * It is the responsibility of the callback methods to return
1726
     * the data itself.
1727
     *
1728
     * Each $eventData array MUST have a 'data' key with the relevant
1729
     * data for callback methods (like an array of key/value pairs to insert
1730
     * or update, an array of results, etc.)
1731
     *
1732
     * If callbacks are not allowed then returns $eventData immediately.
1733
     *
1734
     * @template TEventData of array<string, mixed>
1735
     *
1736
     * @param string     $event     Valid property of the model event: $this->before*, $this->after*, etc.
1737
     * @param TEventData $eventData
1738
     *
1739
     * @return TEventData
1740
     *
1741
     * @throws DataException
1742
     */
1743
    protected function trigger(string $event, array $eventData)
1744
    {
1745
        // Ensure it's a valid event
1746
        if (! isset($this->{$event}) || $this->{$event} === []) {
211✔
1747
            return $eventData;
196✔
1748
        }
1749

1750
        foreach ($this->{$event} as $callback) {
15✔
1751
            if (! method_exists($this, $callback)) {
15✔
1752
                throw DataException::forInvalidMethodTriggered($callback);
1✔
1753
            }
1754

1755
            $eventData = $this->{$callback}($eventData);
14✔
1756
        }
1757

1758
        return $eventData;
14✔
1759
    }
1760

1761
    /**
1762
     * Sets the return type of the results to be as an associative array.
1763
     *
1764
     * @return $this
1765
     */
1766
    public function asArray()
1767
    {
1768
        $this->tempReturnType = 'array';
31✔
1769

1770
        return $this;
31✔
1771
    }
1772

1773
    /**
1774
     * Sets the return type to be of the specified type of object.
1775
     * Defaults to a simple object, but can be any class that has
1776
     * class vars with the same name as the collection columns,
1777
     * or at least allows them to be created.
1778
     *
1779
     * @param 'object'|class-string $class
1780
     *
1781
     * @return $this
1782
     */
1783
    public function asObject(string $class = 'object')
1784
    {
1785
        $this->tempReturnType = $class;
18✔
1786

1787
        return $this;
18✔
1788
    }
1789

1790
    /**
1791
     * Takes a class and returns an array of its public and protected
1792
     * properties as an array suitable for use in creates and updates.
1793
     * This method uses `$this->objectToRawArray()` internally and does conversion
1794
     * to string on all Time instances.
1795
     *
1796
     * @param object $object
1797
     * @param bool   $onlyChanged Returns only the changed properties.
1798
     * @param bool   $recursive   If `true`, inner entities will be cast as array as well.
1799
     *
1800
     * @return array<string, mixed>
1801
     *
1802
     * @throws ReflectionException
1803
     */
1804
    protected function objectToArray($object, bool $onlyChanged = true, bool $recursive = false): array
1805
    {
1806
        $properties = $this->objectToRawArray($object, $onlyChanged, $recursive);
25✔
1807

1808
        // Convert any Time instances to appropriate $dateFormat
1809
        return $this->timeToString($properties);
25✔
1810
    }
1811

1812
    /**
1813
     * Convert any Time instances to appropriate $dateFormat.
1814
     *
1815
     * @param array<string, mixed> $properties
1816
     *
1817
     * @return array<string, mixed>
1818
     */
1819
    protected function timeToString(array $properties): array
1820
    {
1821
        if ($properties === []) {
164✔
1822
            return [];
1✔
1823
        }
1824

1825
        return array_map(function ($value) {
163✔
1826
            if ($value instanceof Time) {
163✔
1827
                return $this->timeToDate($value);
5✔
1828
            }
1829

1830
            return $value;
163✔
1831
        }, $properties);
163✔
1832
    }
1833

1834
    /**
1835
     * Takes a class and returns an array of its public and protected
1836
     * properties as an array with raw values.
1837
     *
1838
     * @param object $object
1839
     * @param bool   $onlyChanged Returns only the changed properties.
1840
     * @param bool   $recursive   If `true`, inner entities will be cast as array as well.
1841
     *
1842
     * @return array<string, mixed> Array with raw values
1843
     *
1844
     * @throws ReflectionException
1845
     */
1846
    protected function objectToRawArray($object, bool $onlyChanged = true, bool $recursive = false): array
1847
    {
1848
        // Entity::toRawArray() returns array
1849
        if (method_exists($object, 'toRawArray')) {
25✔
1850
            $properties = $object->toRawArray($onlyChanged, $recursive);
23✔
1851
        } else {
1852
            $mirror = new ReflectionClass($object);
2✔
1853
            $props  = $mirror->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
2✔
1854

1855
            $properties = [];
2✔
1856

1857
            // Loop over each property,
1858
            // saving the name/value in a new array we can return
1859
            foreach ($props as $prop) {
2✔
1860
                $properties[$prop->getName()] = $prop->getValue($object);
2✔
1861
            }
1862
        }
1863

1864
        return $properties;
25✔
1865
    }
1866

1867
    /**
1868
     * Transform data to array.
1869
     *
1870
     * @param object|row_array|null $row
1871
     *
1872
     * @return array<int|string, mixed>
1873
     *
1874
     * @throws DataException
1875
     * @throws InvalidArgumentException
1876
     * @throws ReflectionException
1877
     *
1878
     * @used-by insert()
1879
     * @used-by insertBatch()
1880
     * @used-by update()
1881
     * @used-by updateBatch()
1882
     */
1883
    protected function transformDataToArray($row, string $type): array
1884
    {
1885
        if (! in_array($type, ['insert', 'update'], true)) {
168✔
1886
            throw new InvalidArgumentException(sprintf('Invalid type "%s" used upon transforming data to array.', $type));
1✔
1887
        }
1888

1889
        if (! $this->allowEmptyInserts && ($row === null || (array) $row === [])) {
167✔
1890
            throw DataException::forEmptyDataset($type);
6✔
1891
        }
1892

1893
        // If it validates with entire rules, all fields are needed.
1894
        if ($this->skipValidation === false && $this->cleanValidationRules === false) {
164✔
1895
            $onlyChanged = false;
143✔
1896
        } else {
1897
            $onlyChanged = ($type === 'update' && $this->updateOnlyChanged);
50✔
1898
        }
1899

1900
        if ($this->useCasts()) {
164✔
1901
            if (is_array($row)) {
27✔
1902
                $row = $this->converter->toDataSource($row);
27✔
1903
            } elseif ($row instanceof stdClass) {
7✔
1904
                $row = (array) $row;
3✔
1905
                $row = $this->converter->toDataSource($row);
3✔
1906
            } elseif ($row instanceof Entity) {
4✔
1907
                $row = $this->converter->extract($row, $onlyChanged);
2✔
1908
            } elseif (is_object($row)) {
2✔
1909
                $row = $this->converter->extract($row, $onlyChanged);
2✔
1910
            }
1911
        }
1912
        // If $row is using a custom class with public or protected
1913
        // properties representing the collection elements, we need to grab
1914
        // them as an array.
1915
        elseif (is_object($row) && ! $row instanceof stdClass) {
137✔
1916
            $row = $this->objectToArray($row, $onlyChanged, true);
24✔
1917
        }
1918

1919
        // If it's still a stdClass, go ahead and convert to
1920
        // an array so doProtectFields and other model methods
1921
        // don't have to do special checks.
1922
        if (is_object($row)) {
164✔
1923
            $row = (array) $row;
13✔
1924
        }
1925

1926
        // If it's still empty here, means $row is no change or is empty object
1927
        if (! $this->allowEmptyInserts && ($row === null || $row === [])) {
164✔
UNCOV
1928
            throw DataException::forEmptyDataset($type);
×
1929
        }
1930

1931
        // Convert any Time instances to appropriate $dateFormat
1932
        return $this->timeToString($row);
164✔
1933
    }
1934

1935
    /**
1936
     * Provides the DB connection and model's properties.
1937
     *
1938
     * @return mixed
1939
     */
1940
    public function __get(string $name)
1941
    {
1942
        if (property_exists($this, $name)) {
50✔
1943
            return $this->{$name};
50✔
1944
        }
1945

1946
        return $this->db->{$name} ?? null;
1✔
1947
    }
1948

1949
    /**
1950
     * Checks for the existence of properties across this model, and DB connection.
1951
     */
1952
    public function __isset(string $name): bool
1953
    {
1954
        if (property_exists($this, $name)) {
50✔
1955
            return true;
50✔
1956
        }
1957

1958
        return isset($this->db->{$name});
1✔
1959
    }
1960

1961
    /**
1962
     * Provides direct access to method in the database connection.
1963
     *
1964
     * @param array<int|string, mixed> $params
1965
     *
1966
     * @return mixed
1967
     */
1968
    public function __call(string $name, array $params)
1969
    {
UNCOV
1970
        if (method_exists($this->db, $name)) {
×
1971
            return $this->db->{$name}(...$params);
×
1972
        }
1973

UNCOV
1974
        return null;
×
1975
    }
1976

1977
    /**
1978
     * Sets $allowEmptyInserts.
1979
     */
1980
    public function allowEmptyInserts(bool $value = true): self
1981
    {
1982
        $this->allowEmptyInserts = $value;
1✔
1983

1984
        return $this;
1✔
1985
    }
1986

1987
    /**
1988
     * Converts database data array to return type value.
1989
     *
1990
     * @param array<string, mixed>          $row        Raw data from database.
1991
     * @param 'array'|'object'|class-string $returnType
1992
     *
1993
     * @return array<string, mixed>|object
1994
     */
1995
    protected function convertToReturnType(array $row, string $returnType): array|object
1996
    {
1997
        if ($returnType === 'array') {
25✔
1998
            return $this->converter->fromDataSource($row);
10✔
1999
        }
2000

2001
        if ($returnType === 'object') {
17✔
2002
            return (object) $this->converter->fromDataSource($row);
5✔
2003
        }
2004

2005
        return $this->converter->reconstruct($returnType, $row);
12✔
2006
    }
2007
}
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

© 2026 Coveralls, Inc