-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseModel.php
1812 lines (1592 loc) · 41.7 KB
/
BaseModel.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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is part of the 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 Closure;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Exceptions\ModelException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Pager\Pager;
use CodeIgniter\Validation\Validation;
use CodeIgniter\Validation\ValidationInterface;
use Config\Services;
use InvalidArgumentException;
use ReflectionClass;
use ReflectionException;
use ReflectionProperty;
use stdClass;
/**
* Class Model
*
* The BaseModel class provides a number of convenient features that
* makes working with a databases less painful. Extending this class
* provide means of implementing various database systems
*
* It will:
* - simplifies pagination
* - allow specifying the return type (array, object, etc) with each call
* - automatically set and update timestamps
* - handle soft deletes
* - ensure validation is run against objects when saving items
* - process various callbacks
* - allow intermingling calls to the db connection
*/
abstract class BaseModel
{
/**
* Pager instance.
* Populated after calling $this->paginate()
*
* @var Pager
*/
public $pager;
/**
* Last insert ID
*
* @var integer|string
*/
protected $insertID = 0;
/**
* The Database connection group that
* should be instantiated.
*
* @var string
*/
protected $DBGroup;
/**
* The format that the results should be returned as.
* Will be overridden if the as* methods are used.
*
* @var string
*/
protected $returnType = 'array';
/**
* If this model should use "softDeletes" and
* simply set a date when rows are deleted, or
* do hard deletes.
*
* @var boolean
*/
protected $useSoftDeletes = false;
/**
* An array of field names that are allowed
* to be set by the user in inserts/updates.
*
* @var array
*/
protected $allowedFields = [];
/**
* If true, will set created_at, and updated_at
* values during insert and update routines.
*
* @var boolean
*/
protected $useTimestamps = false;
/**
* The type of column that created_at and updated_at
* are expected to.
*
* Allowed: 'datetime', 'date', 'int'
*
* @var string
*/
protected $dateFormat = 'datetime';
/**
* The column used for insert timestamps
*
* @var string
*/
protected $createdField = 'created_at';
/**
* The column used for update timestamps
*
* @var string
*/
protected $updatedField = 'updated_at';
/**
* Used by withDeleted to override the
* model's softDelete setting.
*
* @var boolean
*/
protected $tempUseSoftDeletes;
/**
* The column used to save soft delete state
*
* @var string
*/
protected $deletedField = 'deleted_at';
/**
* Used by asArray and asObject to provide
* temporary overrides of model default.
*
* @var string
*/
protected $tempReturnType;
/**
* Whether we should limit fields in inserts
* and updates to those available in $allowedFields or not.
*
* @var boolean
*/
protected $protectFields = true;
/**
* Database Connection
*
* @var BaseConnection
*/
protected $db;
/**
* Rules used to validate data in insert, update, and save methods.
* The array must match the format of data passed to the Validation
* library.
*
* @var array|string
*/
protected $validationRules = [];
/**
* Contains any custom error messages to be
* used during data validation.
*
* @var array
*/
protected $validationMessages = [];
/**
* Skip the model's validation. Used in conjunction with skipValidation()
* to skip data validation for any future calls.
*
* @var boolean
*/
protected $skipValidation = false;
/**
* Whether rules should be removed that do not exist
* in the passed in data. Used between inserts/updates.
*
* @var boolean
*/
protected $cleanValidationRules = true;
/**
* Our validator instance.
*
* @var Validation
*/
protected $validation;
/*
* Callbacks.
*
* Each array should contain the method names (within the model)
* that should be called when those events are triggered.
*
* "Update" and "delete" methods are passed the same items that
* are given to their respective method.
*
* "Find" methods receive the ID searched for (if present), and
* 'afterFind' additionally receives the results that were found.
*/
/**
* Whether to trigger the defined callbacks
*
* @var boolean
*/
protected $allowCallbacks = true;
/**
* Used by allowCallbacks() to override the
* model's allowCallbacks setting.
*
* @var boolean
*/
protected $tempAllowCallbacks;
/**
* Callbacks for beforeInsert
*
* @var array
*/
protected $beforeInsert = [];
/**
* Callbacks for afterInsert
*
* @var array
*/
protected $afterInsert = [];
/**
* Callbacks for beforeUpdate
*
* @var array
*/
protected $beforeUpdate = [];
/**
* Callbacks for afterUpdate
*
* @var array
*/
protected $afterUpdate = [];
/**
* Callbacks for beforeFind
*
* @var array
*/
protected $beforeFind = [];
/**
* Callbacks for afterFind
*
* @var array
*/
protected $afterFind = [];
/**
* Callbacks for beforeDelete
*
* @var array
*/
protected $beforeDelete = [];
/**
* Callbacks for afterDelete
*
* @var array
*/
protected $afterDelete = [];
/**
* BaseModel constructor.
*
* @param ValidationInterface|null $validation Validation
*/
public function __construct(ValidationInterface $validation = null)
{
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;
/**
* @var Validation $validation
*/
$validation = $validation ?? Services::validation(null, false);
$this->validation = $validation;
$this->initialize();
}
/**
* Initializes the instance with any additional steps.
* Optionally implemented by child classes.
*/
protected function initialize()
{
}
/**
* Fetches the row of database
* This methods works only with dbCalls
*
* @param boolean $singleton Single or multiple results
* @param array|integer|string|null $id One primary key or an array of primary keys
*
* @return array|object|null The resulting row of data, or null.
*/
abstract protected function doFind(bool $singleton, $id = null);
/**
* Fetches the column of database
* This methods works only with dbCalls
*
* @param string $columnName Column Name
*
* @return array|null The resulting row of data, or null if no data found.
*
* @throws DataException
*/
abstract protected function doFindColumn(string $columnName);
/**
* Fetches all results, while optionally limiting them.
* This methods works only with dbCalls
*
* @param integer $limit Limit
* @param integer $offset Offset
*
* @return array
*/
abstract protected function doFindAll(int $limit = 0, int $offset = 0);
/**
* Returns the first row of the result set.
* This methods works only with dbCalls
*
* @return array|object|null
*/
abstract protected function doFirst();
/**
* Inserts data into the current database
* This methods works only with dbCalls
*
* @param array $data Data
*
* @return integer|string|boolean
*/
abstract protected function doInsert(array $data);
/**
* Compiles batch insert and runs the queries, validating each row prior.
* This methods works only with dbCalls
*
* @param array|null $set An associative array of insert values
* @param boolean|null $escape Whether to escape values and identifiers
* @param integer $batchSize The size of the batch to run
* @param boolean $testing True means only number of records is returned, false will execute the query
*
* @return integer|boolean Number of rows inserted or FALSE on failure
*/
abstract protected function doInsertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false);
/**
* Updates a single record in the database.
* This methods works only with dbCalls
*
* @param integer|array|string|null $id ID
* @param array|null $data Data
*
* @return boolean
*/
abstract protected function doUpdate($id = null, $data = null): bool;
/**
* Compiles an update and runs the query
* This methods works only with dbCalls
*
* @param array|null $set An associative array of update values
* @param string|null $index The where key
* @param integer $batchSize The size of the batch to run
* @param boolean $returnSQL True means SQL is returned, false will execute the query
*
* @return mixed Number of rows affected or FALSE on failure
*
* @throws DatabaseException
*/
abstract protected function doUpdateBatch(array $set = null, string $index = null, int $batchSize = 100, bool $returnSQL = false);
/**
* Deletes a single record from the database where $id matches
* This methods works only with dbCalls
*
* @param integer|string|array|null $id The rows primary key(s)
* @param boolean $purge Allows overriding the soft deletes setting.
*
* @return string|boolean
*
* @throws DatabaseException
*/
abstract protected function doDelete($id = null, bool $purge = false);
/**
* Permanently deletes all rows that have been marked as deleted
* through soft deletes (deleted = 1)
* This methods works only with dbCalls
*
* @return boolean|mixed
*/
abstract protected function doPurgeDeleted();
/**
* Works with the find* methods to return only the rows that
* have been deleted.
* This methods works only with dbCalls
*
* @return void
*/
abstract protected function doOnlyDeleted();
/**
* Compiles a replace and runs the query
* This methods works only with dbCalls
*
* @param array|null $data Data
* @param boolean $returnSQL Set to true to return Query String
*
* @return mixed
*/
abstract protected function doReplace(array $data = null, bool $returnSQL = false);
/**
* Grabs the last error(s) that occurred from the Database connection.
* This methods works only with dbCalls
*
* @return array|null
*/
abstract protected function doErrors();
/**
* Returns the id value for the data array or object
*
* @param array|object $data Data
*
* @return integer|array|string|null
*
* @deprecated Add an override on getIdValue() instead. Will be removed in version 5.0.
*/
abstract protected function idValue($data);
/**
* Public getter to return the id value using the idValue() method
* For example with SQL this will return $data->$this->primaryKey
*
* @param array|object $data
*
* @return array|integer|string|null
*
* @todo: Make abstract in version 5.0
*/
public function getIdValue($data)
{
return $this->idValue($data);
}
/**
* Override countAllResults to account for soft deleted accounts.
* This methods works only with dbCalls
*
* @param boolean $reset Reset
* @param boolean $test Test
*
* @return mixed
*/
abstract public function countAllResults(bool $reset = true, bool $test = false);
/**
* Loops over records in batches, allowing you to operate on them.
* This methods works only with dbCalls
*
* @param integer $size Size
* @param Closure $userFunc Callback Function
*
* @return void
*
* @throws DataException
*/
abstract public function chunk(int $size, Closure $userFunc);
/**
* Fetches the row of database
*
* @param array|integer|string|null $id One primary key or an array of primary keys
*
* @return array|object|null The resulting row of data, or null.
*/
public function find($id = null)
{
$singleton = is_numeric($id) || is_string($id);
if ($this->tempAllowCallbacks)
{
// Call the before event and check for a return
$eventData = $this->trigger('beforeFind', [
'id' => $id,
'method' => 'find',
'singleton' => $singleton,
]);
if (! empty($eventData['returnData']))
{
return $eventData['data'];
}
}
$eventData = [
'id' => $id,
'data' => $this->doFind($singleton, $id),
'method' => 'find',
'singleton' => $singleton,
];
if ($this->tempAllowCallbacks)
{
$eventData = $this->trigger('afterFind', $eventData);
}
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;
return $eventData['data'];
}
/**
* Fetches the column of database
*
* @param string $columnName Column Name
*
* @return array|null The resulting row of data, or null if no data found.
*
* @throws DataException
*/
public function findColumn(string $columnName)
{
if (strpos($columnName, ',') !== false)
{
throw DataException::forFindColumnHaveMultipleColumns();
}
$resultSet = $this->doFindColumn($columnName);
return $resultSet ? array_column($resultSet, $columnName) : null;
}
/**
* Fetches all results, while optionally limiting them.
*
* @param integer $limit Limit
* @param integer $offset Offset
*
* @return array
*/
public function findAll(int $limit = 0, int $offset = 0)
{
if ($this->tempAllowCallbacks)
{
// Call the before event and check for a return
$eventData = $this->trigger('beforeFind', [
'method' => 'findAll',
'limit' => $limit,
'offset' => $offset,
'singleton' => false,
]);
if (! empty($eventData['returnData']))
{
return $eventData['data'];
}
}
$eventData = [
'data' => $this->doFindAll($limit, $offset),
'limit' => $limit,
'offset' => $offset,
'method' => 'findAll',
'singleton' => false,
];
if ($this->tempAllowCallbacks)
{
$eventData = $this->trigger('afterFind', $eventData);
}
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;
return $eventData['data'];
}
/**
* Returns the first row of the result set.
*
* @return array|object|null
*/
public function first()
{
if ($this->tempAllowCallbacks)
{
// Call the before event and check for a return
$eventData = $this->trigger('beforeFind', [
'method' => 'first',
'singleton' => true,
]);
if (! empty($eventData['returnData']))
{
return $eventData['data'];
}
}
$eventData = [
'data' => $this->doFirst(),
'method' => 'first',
'singleton' => true,
];
if ($this->tempAllowCallbacks)
{
$eventData = $this->trigger('afterFind', $eventData);
}
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;
return $eventData['data'];
}
/**
* A convenience method that will attempt to determine whether the
* data should be inserted or updated. Will work with either
* an array or object. When using with custom class objects,
* you must ensure that the class will provide access to the class
* variables, even if through a magic method.
*
* @param array|object $data Data
*
* @return boolean
*
* @throws ReflectionException
*/
public function save($data): bool
{
if (empty($data))
{
return true;
}
if ($this->shouldUpdate($data))
{
$response = $this->update($this->getIdValue($data), $data);
}
else
{
$response = $this->insert($data, false);
if ($response !== false)
{
$response = true;
}
}
return $response;
}
/**
* 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 $data Data
*
* @return boolean
*/
protected function shouldUpdate($data) : bool
{
return ! empty($this->getIdValue($data));
}
/**
* Returns last insert ID or 0.
*
* @return integer|string
*/
public function getInsertID()
{
return is_numeric($this->insertID) ? (int) $this->insertID : $this->insertID;
}
/**
* Inserts data into the database. If an object is provided,
* it will attempt to convert it to an array.
*
* @param array|object|null $data Data
* @param boolean $returnID Whether insert ID should be returned or not.
*
* @return integer|string|boolean
*
* @throws ReflectionException
*/
public function insert($data = null, bool $returnID = true)
{
$this->insertID = 0;
$data = $this->transformDataToArray($data, 'insert');
// Validate data before saving.
if (! $this->skipValidation && ! $this->cleanRules()->validate($data))
{
return false;
}
// Must be called first so we don't
// strip out created_at values.
$data = $this->doProtectFields($data);
// doProtectFields() can further remove elements from
// $data so we need to check for empty dataset again
if (empty($data))
{
throw DataException::forEmptyDataset('insert');
}
// Set created_at and updated_at with same time
$date = $this->setDate();
if ($this->useTimestamps && $this->createdField && ! array_key_exists($this->createdField, $data))
{
$data[$this->createdField] = $date;
}
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data))
{
$data[$this->updatedField] = $date;
}
$eventData = ['data' => $data];
if ($this->tempAllowCallbacks)
{
$eventData = $this->trigger('beforeInsert', $eventData);
}
$result = $this->doInsert($eventData['data']);
$eventData = [
'id' => $this->insertID,
'data' => $eventData['data'],
'result' => $result,
];
if ($this->tempAllowCallbacks)
{
// Trigger afterInsert events with the inserted data and new ID
$this->trigger('afterInsert', $eventData);
}
$this->tempAllowCallbacks = $this->allowCallbacks;
// If insertion failed, get out of here
if (! $result)
{
return $result;
}
// otherwise return the insertID, if requested.
return $returnID ? $this->insertID : $result;
}
/**
* Compiles batch insert runs the queries, validating each row prior.
*
* @param array|null $set an associative array of insert values
* @param boolean|null $escape Whether to escape values and identifiers
* @param integer $batchSize The size of the batch to run
* @param boolean $testing True means only number of records is returned, false will execute the query
*
* @return integer|boolean Number of rows inserted or FALSE on failure
*
* @throws ReflectionException
*/
public function insertBatch(?array $set = null, ?bool $escape = null, int $batchSize = 100, bool $testing = false)
{
if (is_array($set))
{
foreach ($set as &$row)
{
// If $data is using a custom class with public or protected
// properties representing the collection elements, we need to grab
// them as an array.
if (is_object($row) && ! $row instanceof stdClass)
{
$row = $this->objectToArray($row, false, true);
}
// If it's still a stdClass, go ahead and convert to
// an array so doProtectFields and other model methods
// don't have to do special checks.
if (is_object($row))
{
$row = (array) $row;
}
// Validate every row..
if (! $this->skipValidation && ! $this->cleanRules()->validate($row))
{
return false;
}
// Must be called first so we don't
// strip out created_at values.
$row = $this->doProtectFields($row);
// Set created_at and updated_at with same time
$date = $this->setDate();
if ($this->useTimestamps && $this->createdField && ! array_key_exists($this->createdField, $row))
{
$row[$this->createdField] = $date;
}
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $row))
{
$row[$this->updatedField] = $date;
}
}
}
return $this->doInsertBatch($set, $escape, $batchSize, $testing);
}
/**
* Updates a single record in the database. If an object is provided,
* it will attempt to convert it into an array.
*
* @param integer|array|string|null $id ID
* @param array|object|null $data Data
*
* @return boolean
*
* @throws ReflectionException
*/
public function update($id = null, $data = null): bool
{
if (is_numeric($id) || is_string($id))
{
$id = [$id];
}
$data = $this->transformDataToArray($data, 'update');
// Validate data before saving.
if (! $this->skipValidation && ! $this->cleanRules(true)->validate($data))
{
return false;
}
// Must be called first so we don't
// strip out updated_at values.
$data = $this->doProtectFields($data);
// doProtectFields() can further remove elements from
// $data so we need to check for empty dataset again
if (empty($data))
{
throw DataException::forEmptyDataset('update');
}
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $data))
{
$data[$this->updatedField] = $this->setDate();
}
$eventData = [
'id' => $id,
'data' => $data,
];
if ($this->tempAllowCallbacks)
{
$eventData = $this->trigger('beforeUpdate', $eventData);
}
$eventData = [
'id' => $id,
'data' => $eventData['data'],
'result' => $this->doUpdate($id, $eventData['data']),
];
if ($this->tempAllowCallbacks)
{
$this->trigger('afterUpdate', $eventData);
}
$this->tempAllowCallbacks = $this->allowCallbacks;
return $eventData['result'];
}
/**
* Compiles an update and runs the query
*
* @param array|null $set An associative array of update values
* @param string|null $index The where key
* @param integer $batchSize The size of the batch to run
* @param boolean $returnSQL True means SQL is returned, false will execute the query
*
* @return mixed Number of rows affected or FALSE on failure
*
* @throws DatabaseException
* @throws ReflectionException
*/
public function updateBatch(array $set = null, string $index = null, int $batchSize = 100, bool $returnSQL = false)
{
if (is_array($set))
{
foreach ($set as &$row)
{
// If $data is using a custom class with public or protected
// properties representing the collection elements, we need to grab
// them as an array.
if (is_object($row) && ! $row instanceof stdClass)
{
$row = $this->objectToArray($row, true, true);
}
// If it's still a stdClass, go ahead and convert to
// an array so doProtectFields and other model methods
// don't have to do special checks.
if (is_object($row))
{
$row = (array) $row;
}
// Validate data before saving.
if (! $this->skipValidation && ! $this->cleanRules(true)->validate($row))
{
return false;
}
// Save updateIndex for later
$updateIndex = $row[$index] ?? null;
// Must be called first so we don't
// strip out updated_at values.
$row = $this->doProtectFields($row);
// Restore updateIndex value in case it was wiped out
if ($updateIndex !== null)
{
$row[$index] = $updateIndex;
}
if ($this->useTimestamps && $this->updatedField && ! array_key_exists($this->updatedField, $row))
{
$row[$this->updatedField] = $this->setDate();
}
}
}
return $this->doUpdateBatch($set, $index, $batchSize, $returnSQL);
}
/**
* Deletes a single record from the database where $id matches
*
* @param integer|string|array|null $id The rows primary key(s)
* @param boolean $purge Allows overriding the soft deletes setting.
*
* @return BaseResult|boolean