@@ -189,7 +189,7 @@ abstract class BaseModel
189
189
190
190
/**
191
191
* Whether rules should be removed that do not exist
192
- * in the passed in data. Used between inserts/ updates.
192
+ * in the passed data. Used in updates.
193
193
*
194
194
* @var bool
195
195
*/
@@ -326,9 +326,9 @@ abstract protected function doFind(bool $singleton, $id = null);
326
326
*
327
327
* @param string $columnName Column Name
328
328
*
329
- * @throws DataException
330
- *
331
329
* @return array|null The resulting row of data, or null if no data found.
330
+ *
331
+ * @throws DataException
332
332
*/
333
333
abstract protected function doFindColumn (string $ columnName );
334
334
@@ -392,9 +392,9 @@ abstract protected function doUpdate($id = null, $data = null): bool;
392
392
* @param int $batchSize The size of the batch to run
393
393
* @param bool $returnSQL True means SQL is returned, false will execute the query
394
394
*
395
- * @throws DatabaseException
396
- *
397
395
* @return mixed Number of rows affected or FALSE on failure
396
+ *
397
+ * @throws DatabaseException
398
398
*/
399
399
abstract protected function doUpdateBatch (?array $ set = null , ?string $ index = null , int $ batchSize = 100 , bool $ returnSQL = false );
400
400
@@ -405,9 +405,9 @@ abstract protected function doUpdateBatch(?array $set = null, ?string $index = n
405
405
* @param array|int|string|null $id The rows primary key(s)
406
406
* @param bool $purge Allows overriding the soft deletes setting.
407
407
*
408
- * @throws DatabaseException
409
- *
410
408
* @return bool|string
409
+ *
410
+ * @throws DatabaseException
411
411
*/
412
412
abstract protected function doDelete ($ id = null , bool $ purge = false );
413
413
@@ -541,9 +541,9 @@ public function find($id = null)
541
541
*
542
542
* @param string $columnName Column Name
543
543
*
544
- * @throws DataException
545
- *
546
544
* @return array|null The resulting row of data, or null if no data found.
545
+ *
546
+ * @throws DataException
547
547
*/
548
548
public function findColumn (string $ columnName )
549
549
{
@@ -693,21 +693,31 @@ public function getInsertID()
693
693
* @param array|object|null $data Data
694
694
* @param bool $returnID Whether insert ID should be returned or not.
695
695
*
696
- * @throws ReflectionException
697
- *
698
696
* @return bool|int|string insert ID or true on success. false on failure.
697
+ *
698
+ * @throws ReflectionException
699
699
*/
700
700
public function insert ($ data = null , bool $ returnID = true )
701
701
{
702
702
$ this ->insertID = 0 ;
703
703
704
+ // Set $cleanValidationRules to false temporary.
705
+ $ cleanValidationRules = $ this ->cleanValidationRules ;
706
+ $ this ->cleanValidationRules = false ;
707
+
704
708
$ data = $ this ->transformDataToArray ($ data , 'insert ' );
705
709
706
710
// Validate data before saving.
707
- if (! $ this ->skipValidation && ! $ this ->cleanRules ()->validate ($ data )) {
711
+ if (! $ this ->skipValidation && ! $ this ->validate ($ data )) {
712
+ // Restore $cleanValidationRules
713
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
714
+
708
715
return false ;
709
716
}
710
717
718
+ // Restore $cleanValidationRules
719
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
720
+
711
721
// Must be called first so we don't
712
722
// strip out created_at values.
713
723
$ data = $ this ->doProtectFields ($ data );
@@ -767,12 +777,16 @@ public function insert($data = null, bool $returnID = true)
767
777
* @param int $batchSize The size of the batch to run
768
778
* @param bool $testing True means only number of records is returned, false will execute the query
769
779
*
770
- * @throws ReflectionException
771
- *
772
780
* @return bool|int Number of rows inserted or FALSE on failure
781
+ *
782
+ * @throws ReflectionException
773
783
*/
774
784
public function insertBatch (?array $ set = null , ?bool $ escape = null , int $ batchSize = 100 , bool $ testing = false )
775
785
{
786
+ // Set $cleanValidationRules to false temporary.
787
+ $ cleanValidationRules = $ this ->cleanValidationRules ;
788
+ $ this ->cleanValidationRules = false ;
789
+
776
790
if (is_array ($ set )) {
777
791
foreach ($ set as &$ row ) {
778
792
// If $data is using a custom class with public or protected
@@ -789,8 +803,11 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
789
803
$ row = (array ) $ row ;
790
804
}
791
805
792
- // Validate every row..
793
- if (! $ this ->skipValidation && ! $ this ->cleanRules ()->validate ($ row )) {
806
+ // Validate every row.
807
+ if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
808
+ // Restore $cleanValidationRules
809
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
810
+
794
811
return false ;
795
812
}
796
813
@@ -811,6 +828,9 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
811
828
}
812
829
}
813
830
831
+ // Restore $cleanValidationRules
832
+ $ this ->cleanValidationRules = $ cleanValidationRules ;
833
+
814
834
return $ this ->doInsertBatch ($ set , $ escape , $ batchSize , $ testing );
815
835
}
816
836
@@ -832,7 +852,7 @@ public function update($id = null, $data = null): bool
832
852
$ data = $ this ->transformDataToArray ($ data , 'update ' );
833
853
834
854
// Validate data before saving.
835
- if (! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ data )) {
855
+ if (! $ this ->skipValidation && ! $ this ->validate ($ data )) {
836
856
return false ;
837
857
}
838
858
@@ -882,10 +902,10 @@ public function update($id = null, $data = null): bool
882
902
* @param int $batchSize The size of the batch to run
883
903
* @param bool $returnSQL True means SQL is returned, false will execute the query
884
904
*
905
+ * @return mixed Number of rows affected or FALSE on failure
906
+ *
885
907
* @throws DatabaseException
886
908
* @throws ReflectionException
887
- *
888
- * @return mixed Number of rows affected or FALSE on failure
889
909
*/
890
910
public function updateBatch (?array $ set = null , ?string $ index = null , int $ batchSize = 100 , bool $ returnSQL = false )
891
911
{
@@ -906,7 +926,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
906
926
}
907
927
908
928
// Validate data before saving.
909
- if (! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ row )) {
929
+ if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
910
930
return false ;
911
931
}
912
932
@@ -937,9 +957,9 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
937
957
* @param array|int|string|null $id The rows primary key(s)
938
958
* @param bool $purge Allows overriding the soft deletes setting.
939
959
*
940
- * @throws DatabaseException
941
- *
942
960
* @return BaseResult|bool
961
+ *
962
+ * @throws DatabaseException
943
963
*/
944
964
public function delete ($ id = null , bool $ purge = false )
945
965
{
@@ -1027,7 +1047,7 @@ public function onlyDeleted()
1027
1047
public function replace (?array $ data = null , bool $ returnSQL = false )
1028
1048
{
1029
1049
// Validate data before saving.
1030
- if ($ data && ! $ this ->skipValidation && ! $ this ->cleanRules ( true )-> validate ($ data )) {
1050
+ if ($ data && ! $ this ->skipValidation && ! $ this ->validate ($ data )) {
1031
1051
return false ;
1032
1052
}
1033
1053
@@ -1153,9 +1173,9 @@ protected function doProtectFields(array $data): array
1153
1173
*
1154
1174
* @param int|null $userData An optional PHP timestamp to be converted.
1155
1175
*
1156
- * @throws ModelException
1157
- *
1158
1176
* @return mixed
1177
+ *
1178
+ * @throws ModelException
1159
1179
*/
1160
1180
protected function setDate (?int $ userData = null )
1161
1181
{
@@ -1177,9 +1197,9 @@ protected function setDate(?int $userData = null)
1177
1197
*
1178
1198
* @param int $value value
1179
1199
*
1180
- * @throws ModelException
1181
- *
1182
1200
* @return int|string
1201
+ *
1202
+ * @throws ModelException
1183
1203
*/
1184
1204
protected function intToDate (int $ value )
1185
1205
{
@@ -1440,9 +1460,9 @@ public function allowCallbacks(bool $val = true)
1440
1460
* @param string $event Event
1441
1461
* @param array $eventData Event Data
1442
1462
*
1443
- * @throws DataException
1444
- *
1445
1463
* @return mixed
1464
+ *
1465
+ * @throws DataException
1446
1466
*/
1447
1467
protected function trigger (string $ event , array $ eventData )
1448
1468
{
@@ -1501,9 +1521,9 @@ public function asObject(string $class = 'object')
1501
1521
* @param bool $onlyChanged Only Changed Property
1502
1522
* @param bool $recursive If true, inner entities will be casted as array as well
1503
1523
*
1504
- * @throws ReflectionException
1505
- *
1506
1524
* @return array Array
1525
+ *
1526
+ * @throws ReflectionException
1507
1527
*/
1508
1528
protected function objectToArray ($ data , bool $ onlyChanged = true , bool $ recursive = false ): array
1509
1529
{
@@ -1531,9 +1551,9 @@ protected function objectToArray($data, bool $onlyChanged = true, bool $recursiv
1531
1551
* @param bool $onlyChanged Only Changed Property
1532
1552
* @param bool $recursive If true, inner entities will be casted as array as well
1533
1553
*
1534
- * @throws ReflectionException
1535
- *
1536
1554
* @return array|null Array
1555
+ *
1556
+ * @throws ReflectionException
1537
1557
*/
1538
1558
protected function objectToRawArray ($ data , bool $ onlyChanged = true , bool $ recursive = false ): ?array
1539
1559
{
@@ -1581,7 +1601,11 @@ protected function transformDataToArray($data, string $type): array
1581
1601
// properties representing the collection elements, we need to grab
1582
1602
// them as an array.
1583
1603
if (is_object ($ data ) && ! $ data instanceof stdClass) {
1584
- $ data = $ this ->objectToArray ($ data , ($ type === 'update ' ), true );
1604
+ // If it validates with entire rules, all fields are needed.
1605
+ $ onlyChanged = ($ this ->skipValidation === false && $ this ->cleanValidationRules === false )
1606
+ ? false : ($ type === 'update ' );
1607
+
1608
+ $ data = $ this ->objectToArray ($ data , $ onlyChanged , true );
1585
1609
}
1586
1610
1587
1611
// If it's still a stdClass, go ahead and convert to
0 commit comments