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

codeigniter4 / CodeIgniter4 / 22253868923

21 Feb 2026 08:48AM UTC coverage: 85.977%. Remained the same
22253868923

Pull #9962

github

web-flow
Merge 02a3ff15f into a7c9a2f89
Pull Request #9962: feat: Chunk array method in models

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

20 existing lines in 2 files now uncovered.

22275 of 25908 relevant lines covered (85.98%)

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

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

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

385
    /**
386
     * Creates DataConverter instance.
387
     */
388
    protected function createDataConverter(): void
389
    {
390
        if ($this->useCasts()) {
403✔
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 !== [];
403✔
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
    }
402✔
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
     * @throws InvalidArgumentException if $size is not a positive integer
586
     */
587
    abstract public function chunk(int $size, Closure $userFunc);
588

589
    /**
590
     * Loops over records in batches, allowing you to operate on each chunk at a time.
591
     * This method works only with DB calls.
592
     *
593
     * This method calls the `$userFunc` with the chunk, instead of a single record as in `chunk()`.
594
     * This allows you to operate on multiple records at once, which can be more efficient for certain operations.
595
     *
596
     * @param Closure(list<array<string, string>>|list<object>): mixed $userFunc
597
     *
598
     * @return void
599
     *
600
     * @throws DataException
601
     */
602
    abstract public function chunkArray(int $size, Closure $userFunc);
603

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

774
        return $response;
26✔
775
    }
776

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

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

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

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

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

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

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

836
            return;
62✔
837
        }
838

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

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

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

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

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

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

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

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

887
            return false;
19✔
888
        }
889

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

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

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

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

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

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

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

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

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

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

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

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

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

952
        return $row;
119✔
953
    }
954

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

969
        return $row;
136✔
970
    }
971

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

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

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

999
                    return false;
3✔
1000
                }
1001

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

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

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

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

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

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

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

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

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

1036
        return $result;
11✔
1037
    }
1038

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1186
        return $result;
5✔
1187
    }
1188

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

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

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

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

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

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

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

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

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

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

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

1259
        return $this;
23✔
1260
    }
1261

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

1273
        return $this;
1✔
1274
    }
1275

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

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

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

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

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

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

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

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

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

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

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

1366
        return $this;
10✔
1367
    }
1368

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

1379
        return $this;
12✔
1380
    }
1381

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

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

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

1411
        return $row;
43✔
1412
    }
1413

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

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

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

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

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

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

1502
        return $this;
2✔
1503
    }
1504

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

UNCOV
1517
        return $this;
×
1518
    }
1519

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

1532
        return $this;
2✔
1533
    }
1534

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

1547
        return $this;
2✔
1548
    }
1549

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

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

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

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

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

1575
        return $this;
2✔
1576
    }
1577

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

1588
        return $this;
2✔
1589
    }
1590

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1661
        return $rules;
175✔
1662
    }
1663

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

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

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

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

1704
        return $rules;
19✔
1705
    }
1706

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

1717
        return $this;
3✔
1718
    }
1719

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

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

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

1759
        return $eventData;
14✔
1760
    }
1761

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

1771
        return $this;
31✔
1772
    }
1773

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

1788
        return $this;
18✔
1789
    }
1790

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

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

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

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

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

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

1856
            $properties = [];
2✔
1857

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

1865
        return $properties;
25✔
1866
    }
1867

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1975
        return null;
×
1976
    }
1977

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

1985
        return $this;
1✔
1986
    }
1987

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

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

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