@@ -909,22 +909,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
909
909
910
910
if (is_array ($ set )) {
911
911
foreach ($ set as &$ row ) {
912
- // If $row is using a custom class with public or protected
913
- // properties representing the collection elements, we need to grab
914
- // them as an array.
915
- if (is_object ($ row ) && ! $ row instanceof stdClass) {
916
- $ row = $ this ->objectToArray ($ row , false , true );
917
- }
918
-
919
- // If it's still a stdClass, go ahead and convert to
920
- // an array so doProtectFields and other model methods
921
- // don't have to do special checks.
922
- if (is_object ($ row )) {
923
- $ row = (array ) $ row ;
924
- }
925
-
926
- // Convert any Time instances to appropriate $dateFormat
927
- $ row = $ this ->timeToString ($ row );
912
+ $ row = $ this ->transformDataRowToArray ($ row );
928
913
929
914
// Validate every row.
930
915
if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
@@ -1051,21 +1036,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
1051
1036
{
1052
1037
if (is_array ($ set )) {
1053
1038
foreach ($ set as &$ row ) {
1054
- // If $row is using a custom class with public or protected
1055
- // properties representing the collection elements, we need to grab
1056
- // them as an array.
1057
- if (is_object ($ row ) && ! $ row instanceof stdClass) {
1058
- // For updates the index field is needed even if it is not changed.
1059
- // So set $onlyChanged to false.
1060
- $ row = $ this ->objectToArray ($ row , false , true );
1061
- }
1062
-
1063
- // If it's still a stdClass, go ahead and convert to
1064
- // an array so doProtectFields and other model methods
1065
- // don't have to do special checks.
1066
- if (is_object ($ row )) {
1067
- $ row = (array ) $ row ;
1068
- }
1039
+ $ row = $ this ->transformDataRowToArray ($ row );
1069
1040
1070
1041
// Validate data before saving.
1071
1042
if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
@@ -1220,7 +1191,9 @@ public function replace(?array $row = null, bool $returnSQL = false)
1220
1191
return false ;
1221
1192
}
1222
1193
1223
- $ row = $ this ->setUpdatedField ((array ) $ row , $ this ->setDate ());
1194
+ $ row = (array ) $ row ;
1195
+ $ row = $ this ->setCreatedField ($ row , $ this ->setDate ());
1196
+ $ row = $ this ->setUpdatedField ($ row , $ this ->setDate ());
1224
1197
1225
1198
return $ this ->doReplace ($ row , $ returnSQL );
1226
1199
}
@@ -1694,6 +1667,52 @@ protected function trigger(string $event, array $eventData)
1694
1667
return $ eventData ;
1695
1668
}
1696
1669
1670
+ /**
1671
+ * If the model is using casts, this will convert the data
1672
+ * in $row according to the rules defined in `$casts`.
1673
+ *
1674
+ * @param object|row_array|null $row Row data
1675
+ *
1676
+ * @return object|row_array|null Converted row data
1677
+ *
1678
+ * @used-by insertBatch()
1679
+ * @used-by updateBatch()
1680
+ *
1681
+ * @throws ReflectionException
1682
+ * @deprecated Since 4.6.4, temporary solution - will be removed in 4.7
1683
+ */
1684
+ protected function transformDataRowToArray (array |object |null $ row ): array |object |null
1685
+ {
1686
+ // If casts are used, convert the data first
1687
+ if ($ this ->useCasts ()) {
1688
+ if (is_array ($ row )) {
1689
+ $ row = $ this ->converter ->toDataSource ($ row );
1690
+ } elseif ($ row instanceof stdClass) {
1691
+ $ row = (array ) $ row ;
1692
+ $ row = $ this ->converter ->toDataSource ($ row );
1693
+ } elseif ($ row instanceof Entity) {
1694
+ $ row = $ this ->converter ->extract ($ row );
1695
+ } elseif (is_object ($ row )) {
1696
+ $ row = $ this ->converter ->extract ($ row );
1697
+ }
1698
+ } elseif (is_object ($ row ) && ! $ row instanceof stdClass) {
1699
+ // If $row is using a custom class with public or protected
1700
+ // properties representing the collection elements, we need to grab
1701
+ // them as an array.
1702
+ $ row = $ this ->objectToArray ($ row , false , true );
1703
+ }
1704
+
1705
+ // If it's still a stdClass, go ahead and convert to
1706
+ // an array so doProtectFields and other model methods
1707
+ // don't have to do special checks.
1708
+ if (is_object ($ row )) {
1709
+ $ row = (array ) $ row ;
1710
+ }
1711
+
1712
+ // Convert any Time instances to appropriate $dateFormat
1713
+ return $ this ->timeToString ($ row );
1714
+ }
1715
+
1697
1716
/**
1698
1717
* Sets the return type of the results to be as an associative array.
1699
1718
*
0 commit comments