Skip to content

Commit cd06c43

Browse files
committed
Replaced "casting only" function to more generic one.
1 parent 2a5b1e4 commit cd06c43

File tree

1 file changed

+27
-50
lines changed

1 file changed

+27
-50
lines changed

system/BaseModel.php

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -909,25 +909,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
909909

910910
if (is_array($set)) {
911911
foreach ($set as &$row) {
912-
// If casts are used, convert the data first
913-
if ($this->useCasts()) {
914-
$row = $this->performCasting($row);
915-
} elseif (is_object($row) && ! $row instanceof stdClass) {
916-
// If $row is using a custom class with public or protected
917-
// properties representing the collection elements, we need to grab
918-
// them as an array.
919-
$row = $this->objectToArray($row, false, true);
920-
}
921-
922-
// If it's still a stdClass, go ahead and convert to
923-
// an array so doProtectFields and other model methods
924-
// don't have to do special checks.
925-
if (is_object($row)) {
926-
$row = (array) $row;
927-
}
928-
929-
// Convert any Time instances to appropriate $dateFormat
930-
$row = $this->timeToString($row);
912+
$row = $this->transformDataRowToArray($row);
931913

932914
// Validate every row.
933915
if (! $this->skipValidation && ! $this->validate($row)) {
@@ -1054,25 +1036,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10541036
{
10551037
if (is_array($set)) {
10561038
foreach ($set as &$row) {
1057-
// If casts are used, convert the data first
1058-
if ($this->useCasts()) {
1059-
$row = $this->performCasting($row);
1060-
} elseif (is_object($row) && ! $row instanceof stdClass) {
1061-
// If $row is using a custom class with public or protected
1062-
// properties representing the collection elements, we need to grab
1063-
// them as an array.
1064-
1065-
// For updates the index field is needed even if it is not changed.
1066-
// So set $onlyChanged to false.
1067-
$row = $this->objectToArray($row, false, true);
1068-
}
1069-
1070-
// If it's still a stdClass, go ahead and convert to
1071-
// an array so doProtectFields and other model methods
1072-
// don't have to do special checks.
1073-
if (is_object($row)) {
1074-
$row = (array) $row;
1075-
}
1039+
$row = $this->transformDataRowToArray($row);
10761040

10771041
// Validate data before saving.
10781042
if (! $this->skipValidation && ! $this->validate($row)) {
@@ -1714,26 +1678,39 @@ protected function trigger(string $event, array $eventData)
17141678
* @used-by insertBatch()
17151679
* @used-by updateBatch()
17161680
*
1681+
* @throws ReflectionException
17171682
* @deprecated Since 4.6.4, temporary solution - will be removed in 4.7
17181683
*/
1719-
protected function performCasting(array|object|null $row = null): array|object|null
1684+
protected function transformDataRowToArray(array|object|null $row): array|object|null
17201685
{
1721-
if (! $this->useCasts()) {
1722-
return $row;
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);
17231703
}
17241704

1725-
if (is_array($row)) {
1726-
$row = $this->converter->toDataSource($row);
1727-
} elseif ($row instanceof stdClass) {
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)) {
17281709
$row = (array) $row;
1729-
$row = $this->converter->toDataSource($row);
1730-
} elseif ($row instanceof Entity) {
1731-
$row = $this->converter->extract($row);
1732-
} elseif (is_object($row)) {
1733-
$row = $this->converter->extract($row);
17341710
}
17351711

1736-
return $row;
1712+
// Convert any Time instances to appropriate $dateFormat
1713+
return $this->timeToString($row);
17371714
}
17381715

17391716
/**

0 commit comments

Comments
 (0)