-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Model.php
951 lines (818 loc) · 30.2 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter;
use BadMethodCallException;
use Closure;
use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Database\Query;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Exceptions\ModelException;
use CodeIgniter\Validation\ValidationInterface;
use Config\Database;
use Config\Feature;
use ReflectionException;
use stdClass;
/**
* The Model class extends BaseModel and provides additional
* convenient features that makes working with a SQL database
* table less painful.
*
* It will:
* - automatically connect to database
* - allow intermingling calls to the builder
* - removes the need to use Result object directly in most cases
*
* @property-read BaseConnection $db
*
* @method $this groupBy($by, ?bool $escape = null)
* @method $this groupEnd()
* @method $this groupStart()
* @method $this having($key, $value = null, ?bool $escape = null)
* @method $this havingGroupEnd()
* @method $this havingGroupStart()
* @method $this havingIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this havingLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this havingNotIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this join(string $table, string $cond, string $type = '', ?bool $escape = null)
* @method $this like($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this limit(?int $value = null, ?int $offset = 0)
* @method $this notGroupStart()
* @method $this notHavingGroupStart()
* @method $this notHavingLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this notLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this offset(int $offset)
* @method $this orderBy(string $orderBy, string $direction = '', ?bool $escape = null)
* @method $this orGroupStart()
* @method $this orHaving($key, $value = null, ?bool $escape = null)
* @method $this orHavingGroupStart()
* @method $this orHavingIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this orHavingLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this orHavingNotIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this orLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this orNotGroupStart()
* @method $this orNotHavingGroupStart()
* @method $this orNotHavingLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this orNotLike($field, string $match = '', string $side = 'both', ?bool $escape = null, bool $insensitiveSearch = false)
* @method $this orWhere($key, $value = null, ?bool $escape = null)
* @method $this orWhereIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this orWhereNotIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this select($select = '*', ?bool $escape = null)
* @method $this selectAvg(string $select = '', string $alias = '')
* @method $this selectCount(string $select = '', string $alias = '')
* @method $this selectMax(string $select = '', string $alias = '')
* @method $this selectMin(string $select = '', string $alias = '')
* @method $this selectSum(string $select = '', string $alias = '')
* @method $this when($condition, callable $callback, ?callable $defaultCallback = null)
* @method $this whenNot($condition, callable $callback, ?callable $defaultCallback = null)
* @method $this where($key, $value = null, ?bool $escape = null)
* @method $this whereIn(?string $key = null, $values = null, ?bool $escape = null)
* @method $this whereNotIn(?string $key = null, $values = null, ?bool $escape = null)
*
* @phpstan-import-type row_array from BaseModel
*/
class Model extends BaseModel
{
/**
* Name of database table
*
* @var string
*/
protected $table;
/**
* The table's primary key.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Whether primary key uses auto increment.
*
* @var bool
*/
protected $useAutoIncrement = true;
/**
* Query Builder object
*
* @var BaseBuilder|null
*/
protected $builder;
/**
* Holds information passed in via 'set'
* so that we can capture it (not the builder)
* and ensure it gets validated first.
*
* @var array{escape: array, data: array}|array{}
* @phpstan-var array{escape: array<int|string, bool|null>, data: row_array}|array{}
*/
protected $tempData = [];
/**
* Escape array that maps usage of escape
* flag for every parameter.
*
* @var array
*/
protected $escape = [];
/**
* Builder method names that should not be used in the Model.
*
* @var list<string> method name
*/
private array $builderMethodsNotAvailable = [
'getCompiledInsert',
'getCompiledSelect',
'getCompiledUpdate',
];
public function __construct(?ConnectionInterface $db = null, ?ValidationInterface $validation = null)
{
/**
* @var BaseConnection|null $db
*/
$db ??= Database::connect($this->DBGroup);
$this->db = $db;
parent::__construct($validation);
}
/**
* Specify the table associated with a model
*
* @param string $table Table
*
* @return $this
*/
public function setTable(string $table)
{
$this->table = $table;
return $this;
}
/**
* Fetches the row(s) of database from $this->table with a primary key
* matching $id.
* This method works only with dbCalls.
*
* @param bool $singleton Single or multiple results
* @param array|int|string|null $id One primary key or an array of primary keys
*
* @return array|object|null The resulting row of data, or null.
* @phpstan-return ($singleton is true ? row_array|null|object : list<row_array|object>)
*/
protected function doFind(bool $singleton, $id = null)
{
$builder = $this->builder();
$useCast = $this->useCasts();
if ($useCast) {
$returnType = $this->tempReturnType;
$this->asArray();
}
if ($this->tempUseSoftDeletes) {
$builder->where($this->table . '.' . $this->deletedField, null);
}
$row = null;
$rows = [];
if (is_array($id)) {
$rows = $builder->whereIn($this->table . '.' . $this->primaryKey, $id)
->get()
->getResult($this->tempReturnType);
} elseif ($singleton) {
$row = $builder->where($this->table . '.' . $this->primaryKey, $id)
->get()
->getFirstRow($this->tempReturnType);
} else {
$rows = $builder->get()->getResult($this->tempReturnType);
}
if ($useCast) {
$this->tempReturnType = $returnType;
if ($singleton) {
if ($row === null) {
return null;
}
return $this->convertToReturnType($row, $returnType);
}
foreach ($rows as $i => $row) {
$rows[$i] = $this->convertToReturnType($row, $returnType);
}
return $rows;
}
if ($singleton) {
return $row;
}
return $rows;
}
/**
* Fetches the column of database from $this->table.
* This method works only with dbCalls.
*
* @param string $columnName Column Name
*
* @return array|null The resulting row of data, or null if no data found.
* @phpstan-return list<row_array>|null
*/
protected function doFindColumn(string $columnName)
{
return $this->select($columnName)->asArray()->find();
}
/**
* Works with the current Query Builder instance to return
* all results, while optionally limiting them.
* This method works only with dbCalls.
*
* @param int|null $limit Limit
* @param int $offset Offset
*
* @return array
* @phpstan-return list<row_array|object>
*/
protected function doFindAll(?int $limit = null, int $offset = 0)
{
$limitZeroAsAll = config(Feature::class)->limitZeroAsAll ?? true;
if ($limitZeroAsAll) {
$limit ??= 0;
}
$builder = $this->builder();
$useCast = $this->useCasts();
if ($useCast) {
$returnType = $this->tempReturnType;
$this->asArray();
}
if ($this->tempUseSoftDeletes) {
$builder->where($this->table . '.' . $this->deletedField, null);
}
$results = $builder->limit($limit, $offset)
->get()
->getResult($this->tempReturnType);
if ($useCast) {
foreach ($results as $i => $row) {
$results[$i] = $this->convertToReturnType($row, $returnType);
}
$this->tempReturnType = $returnType;
}
return $results;
}
/**
* Returns the first row of the result set. Will take any previous
* Query Builder calls into account when determining the result set.
* This method works only with dbCalls.
*
* @return array|object|null
* @phpstan-return row_array|object|null
*/
protected function doFirst()
{
$builder = $this->builder();
$useCast = $this->useCasts();
if ($useCast) {
$returnType = $this->tempReturnType;
$this->asArray();
}
if ($this->tempUseSoftDeletes) {
$builder->where($this->table . '.' . $this->deletedField, null);
} elseif ($this->useSoftDeletes && ($builder->QBGroupBy === []) && $this->primaryKey) {
$builder->groupBy($this->table . '.' . $this->primaryKey);
}
// Some databases, like PostgreSQL, need order
// information to consistently return correct results.
if ($builder->QBGroupBy && ($builder->QBOrderBy === []) && $this->primaryKey) {
$builder->orderBy($this->table . '.' . $this->primaryKey, 'asc');
}
$row = $builder->limit(1, 0)->get()->getFirstRow($this->tempReturnType);
if ($useCast && $row !== null) {
$row = $this->convertToReturnType($row, $returnType);
$this->tempReturnType = $returnType;
}
return $row;
}
/**
* Inserts data into the current table.
* This method works only with dbCalls.
*
* @param array $row Row data
* @phpstan-param row_array $row
*
* @return bool
*/
protected function doInsert(array $row)
{
$escape = $this->escape;
$this->escape = [];
// Require non-empty primaryKey when
// not using auto-increment feature
if (! $this->useAutoIncrement && ! isset($row[$this->primaryKey])) {
throw DataException::forEmptyPrimaryKey('insert');
}
$builder = $this->builder();
// Must use the set() method to ensure to set the correct escape flag
foreach ($row as $key => $val) {
$builder->set($key, $val, $escape[$key] ?? null);
}
if ($this->allowEmptyInserts && $row === []) {
$table = $this->db->protectIdentifiers($this->table, true, null, false);
if ($this->db->getPlatform() === 'MySQLi') {
$sql = 'INSERT INTO ' . $table . ' VALUES ()';
} elseif ($this->db->getPlatform() === 'OCI8') {
$allFields = $this->db->protectIdentifiers(
array_map(
static fn ($row) => $row->name,
$this->db->getFieldData($this->table)
),
false,
true
);
$sql = sprintf(
'INSERT INTO %s (%s) VALUES (%s)',
$table,
implode(',', $allFields),
substr(str_repeat(',DEFAULT', count($allFields)), 1)
);
} else {
$sql = 'INSERT INTO ' . $table . ' DEFAULT VALUES';
}
$result = $this->db->query($sql);
} else {
$result = $builder->insert();
}
// If insertion succeeded then save the insert ID
if ($result) {
$this->insertID = ! $this->useAutoIncrement ? $row[$this->primaryKey] : $this->db->insertID();
}
return $result;
}
/**
* Compiles batch insert strings and runs the queries, validating each row prior.
* This method works only with dbCalls.
*
* @param array|null $set An associative array of insert values
* @param bool|null $escape Whether to escape values
* @param int $batchSize The size of the batch to run
* @param bool $testing True means only number of records is returned, false will execute the query
*
* @return bool|int Number of rows inserted or FALSE on failure
*/
protected function doInsertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false)
{
if (is_array($set)) {
foreach ($set as $row) {
// Require non-empty primaryKey when
// not using auto-increment feature
if (! $this->useAutoIncrement && ! isset($row[$this->primaryKey])) {
throw DataException::forEmptyPrimaryKey('insertBatch');
}
}
}
return $this->builder()->testMode($testing)->insertBatch($set, $escape, $batchSize);
}
/**
* Updates a single record in $this->table.
* This method works only with dbCalls.
*
* @param array|int|string|null $id
* @param array|null $row Row data
* @phpstan-param row_array|null $row
*/
protected function doUpdate($id = null, $row = null): bool
{
$escape = $this->escape;
$this->escape = [];
$builder = $this->builder();
if ($id) {
$builder = $builder->whereIn($this->table . '.' . $this->primaryKey, $id);
}
// Must use the set() method to ensure to set the correct escape flag
foreach ($row as $key => $val) {
$builder->set($key, $val, $escape[$key] ?? null);
}
if ($builder->getCompiledQBWhere() === []) {
throw new DatabaseException(
'Updates are not allowed unless they contain a "where" or "like" clause.'
);
}
return $builder->update();
}
/**
* Compiles an update string and runs the query
* This method works only with dbCalls.
*
* @param array|null $set An associative array of update values
* @param string|null $index The where key
* @param int $batchSize The size of the batch to run
* @param bool $returnSQL True means SQL is returned, false will execute the query
*
* @return false|int|list<string> Number of rows affected or FALSE on failure, SQL array when testMode
*
* @throws DatabaseException
*/
protected function doUpdateBatch(?array $set = null, ?string $index = null, int $batchSize = 100, bool $returnSQL = false)
{
return $this->builder()->testMode($returnSQL)->updateBatch($set, $index, $batchSize);
}
/**
* Deletes a single record from $this->table where $id matches
* the table's primaryKey
* This method works only with dbCalls.
*
* @param array|int|string|null $id The rows primary key(s)
* @param bool $purge Allows overriding the soft deletes setting.
*
* @return bool|string SQL string when testMode
*
* @throws DatabaseException
*/
protected function doDelete($id = null, bool $purge = false)
{
$set = [];
$builder = $this->builder();
if ($id) {
$builder = $builder->whereIn($this->primaryKey, $id);
}
if ($this->useSoftDeletes && ! $purge) {
if ($builder->getCompiledQBWhere() === []) {
throw new DatabaseException(
'Deletes are not allowed unless they contain a "where" or "like" clause.'
);
}
$builder->where($this->deletedField);
$set[$this->deletedField] = $this->setDate();
if ($this->useTimestamps && $this->updatedField !== '') {
$set[$this->updatedField] = $this->setDate();
}
return $builder->update($set);
}
return $builder->delete();
}
/**
* Permanently deletes all rows that have been marked as deleted
* through soft deletes (deleted = 1)
* This method works only with dbCalls.
*
* @return bool|string Returns a SQL string if in test mode.
*/
protected function doPurgeDeleted()
{
return $this->builder()
->where($this->table . '.' . $this->deletedField . ' IS NOT NULL')
->delete();
}
/**
* Works with the find* methods to return only the rows that
* have been deleted.
* This method works only with dbCalls.
*
* @return void
*/
protected function doOnlyDeleted()
{
$this->builder()->where($this->table . '.' . $this->deletedField . ' IS NOT NULL');
}
/**
* Compiles a replace into string and runs the query
* This method works only with dbCalls.
*
* @param array|null $row Data
* @phpstan-param row_array|null $row
* @param bool $returnSQL Set to true to return Query String
*
* @return BaseResult|false|Query|string
*/
protected function doReplace(?array $row = null, bool $returnSQL = false)
{
return $this->builder()->testMode($returnSQL)->replace($row);
}
/**
* Grabs the last error(s) that occurred from the Database connection.
* The return array should be in the following format:
* ['source' => 'message']
* This method works only with dbCalls.
*
* @return array<string, string>
*/
protected function doErrors()
{
// $error is always ['code' => string|int, 'message' => string]
$error = $this->db->error();
if ((int) $error['code'] === 0) {
return [];
}
return [$this->db::class => $error['message']];
}
/**
* Returns the id value for the data array or object
*
* @param array|object $row Row data
* @phpstan-param row_array|object $row
*
* @return array|int|string|null
*/
public function getIdValue($row)
{
if (is_object($row) && isset($row->{$this->primaryKey})) {
// Get the raw primary key value of the Entity.
if ($row instanceof Entity) {
$cast = $row->cast();
// Disable Entity casting, because raw primary key value is needed for database.
$row->cast(false);
$primaryKey = $row->{$this->primaryKey};
// Restore Entity casting setting.
$row->cast($cast);
return $primaryKey;
}
return $row->{$this->primaryKey};
}
if (is_array($row) && isset($row[$this->primaryKey])) {
return $row[$this->primaryKey];
}
return null;
}
/**
* Loops over records in batches, allowing you to operate on them.
* Works with $this->builder to get the Compiled select to
* determine the rows to operate on.
* This method works only with dbCalls.
*
* @return void
*
* @throws DataException
*/
public function chunk(int $size, Closure $userFunc)
{
$total = $this->builder()->countAllResults(false);
$offset = 0;
while ($offset <= $total) {
$builder = clone $this->builder();
$rows = $builder->get($size, $offset);
if (! $rows) {
throw DataException::forEmptyDataset('chunk');
}
$rows = $rows->getResult($this->tempReturnType);
$offset += $size;
if ($rows === []) {
continue;
}
foreach ($rows as $row) {
if ($userFunc($row) === false) {
return;
}
}
}
}
/**
* Override countAllResults to account for soft deleted accounts.
*
* @return int|string
*/
public function countAllResults(bool $reset = true, bool $test = false)
{
if ($this->tempUseSoftDeletes) {
$this->builder()->where($this->table . '.' . $this->deletedField, null);
}
// When $reset === false, the $tempUseSoftDeletes will be
// dependent on $useSoftDeletes value because we don't
// want to add the same "where" condition for the second time
$this->tempUseSoftDeletes = $reset
? $this->useSoftDeletes
: ($this->useSoftDeletes ? false : $this->useSoftDeletes);
return $this->builder()->testMode($test)->countAllResults($reset);
}
/**
* Provides a shared instance of the Query Builder.
*
* @param non-empty-string|null $table
*
* @return BaseBuilder
*
* @throws ModelException
*/
public function builder(?string $table = null)
{
// Check for an existing Builder
if ($this->builder instanceof BaseBuilder) {
// Make sure the requested table matches the builder
if ($table && $this->builder->getTable() !== $table) {
return $this->db->table($table);
}
return $this->builder;
}
// We're going to force a primary key to exist
// so we don't have overly convoluted code,
// and future features are likely to require them.
if ($this->primaryKey === '') {
throw ModelException::forNoPrimaryKey(static::class);
}
$table = ($table === null || $table === '') ? $this->table : $table;
// Ensure we have a good db connection
if (! $this->db instanceof BaseConnection) {
$this->db = Database::connect($this->DBGroup);
}
$builder = $this->db->table($table);
// Only consider it "shared" if the table is correct
if ($table === $this->table) {
$this->builder = $builder;
}
return $builder;
}
/**
* Captures the builder's set() method so that we can validate the
* data here. This allows it to be used with any of the other
* builder methods and still get validated data, like replace.
*
* @param array|object|string $key Field name, or an array of field/value pairs, or an object
* @param bool|float|int|object|string|null $value Field value, if $key is a single field
* @param bool|null $escape Whether to escape values
*
* @return $this
*/
public function set($key, $value = '', ?bool $escape = null)
{
if (is_object($key)) {
$key = $key instanceof stdClass ? (array) $key : $this->objectToArray($key);
}
$data = is_array($key) ? $key : [$key => $value];
foreach (array_keys($data) as $k) {
$this->tempData['escape'][$k] = $escape;
}
$this->tempData['data'] = array_merge($this->tempData['data'] ?? [], $data);
return $this;
}
/**
* This method is called on save to determine if entry have to be updated
* If this method return false insert operation will be executed
*
* @param array|object $row Data
*/
protected function shouldUpdate($row): bool
{
if (parent::shouldUpdate($row) === false) {
return false;
}
if ($this->useAutoIncrement === true) {
return true;
}
// When useAutoIncrement feature is disabled, check
// in the database if given record already exists
return $this->where($this->primaryKey, $this->getIdValue($row))->countAllResults() === 1;
}
/**
* Inserts data into the database. If an object is provided,
* it will attempt to convert it to an array.
*
* @param array|object|null $row
* @phpstan-param row_array|object|null $row
* @param bool $returnID Whether insert ID should be returned or not.
*
* @return bool|int|string
* @phpstan-return ($returnID is true ? int|string|false : bool)
*
* @throws ReflectionException
*/
public function insert($row = null, bool $returnID = true)
{
if (isset($this->tempData['data'])) {
if ($row === null) {
$row = $this->tempData['data'];
} else {
$row = $this->transformDataToArray($row, 'insert');
$row = array_merge($this->tempData['data'], $row);
}
}
$this->escape = $this->tempData['escape'] ?? [];
$this->tempData = [];
return parent::insert($row, $returnID);
}
/**
* Ensures that only the fields that are allowed to be inserted are in
* the data array.
*
* @used-by insert() to protect against mass assignment vulnerabilities.
* @used-by insertBatch() to protect against mass assignment vulnerabilities.
*
* @param array $row Row data
* @phpstan-param row_array $row
*
* @throws DataException
*/
protected function doProtectFieldsForInsert(array $row): array
{
if (! $this->protectFields) {
return $row;
}
if ($this->allowedFields === []) {
throw DataException::forInvalidAllowedFields(static::class);
}
foreach (array_keys($row) as $key) {
// Do not remove the non-auto-incrementing primary key data.
if ($this->useAutoIncrement === false && $key === $this->primaryKey) {
continue;
}
if (! in_array($key, $this->allowedFields, true)) {
unset($row[$key]);
}
}
return $row;
}
/**
* Updates a single record in the database. If an object is provided,
* it will attempt to convert it into an array.
*
* @param array|int|string|null $id
* @param array|object|null $row
* @phpstan-param row_array|object|null $row
*
* @throws ReflectionException
*/
public function update($id = null, $row = null): bool
{
if (isset($this->tempData['data'])) {
if ($row === null) {
$row = $this->tempData['data'];
} else {
$row = $this->transformDataToArray($row, 'update');
$row = array_merge($this->tempData['data'], $row);
}
}
$this->escape = $this->tempData['escape'] ?? [];
$this->tempData = [];
return parent::update($id, $row);
}
/**
* Takes a class and returns an array of its public and protected
* properties as an array with raw values.
*
* @param object $object Object
* @param bool $recursive If true, inner entities will be cast as array as well
*
* @return array<string, mixed> Array with raw values.
*
* @throws ReflectionException
*/
protected function objectToRawArray($object, bool $onlyChanged = true, bool $recursive = false): array
{
return parent::objectToRawArray($object, $onlyChanged);
}
/**
* Provides/instantiates the builder/db connection and model's table/primary key names and return type.
*
* @param string $name Name
*
* @return array|BaseBuilder|bool|float|int|object|string|null
*/
public function __get(string $name)
{
if (parent::__isset($name)) {
return parent::__get($name);
}
return $this->builder()->{$name} ?? null;
}
/**
* Checks for the existence of properties across this model, builder, and db connection.
*
* @param string $name Name
*/
public function __isset(string $name): bool
{
if (parent::__isset($name)) {
return true;
}
return isset($this->builder()->{$name});
}
/**
* Provides direct access to method in the builder (if available)
* and the database connection.
*
* @return $this|array|BaseBuilder|bool|float|int|object|string|null
*/
public function __call(string $name, array $params)
{
$builder = $this->builder();
$result = null;
if (method_exists($this->db, $name)) {
$result = $this->db->{$name}(...$params);
} elseif (method_exists($builder, $name)) {
$this->checkBuilderMethod($name);
$result = $builder->{$name}(...$params);
} else {
throw new BadMethodCallException('Call to undefined method ' . static::class . '::' . $name);
}
if ($result instanceof BaseBuilder) {
return $this;
}
return $result;
}
/**
* Checks the Builder method name that should not be used in the Model.
*/
private function checkBuilderMethod(string $name): void
{
if (in_array($name, $this->builderMethodsNotAvailable, true)) {
throw ModelException::forMethodNotAvailable(static::class, $name . '()');
}
}
}