Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 6 additions & 49 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ abstract class BaseModel
protected bool $updateOnlyChanged = true;

/**
* Rules used to validate data in insert(), update(), and save() methods.
* Rules used to validate data in insert(), update(), save(),
* insertBatch(), and updateBatch() methods.
*
* The array must match the format of data passed to the Validation
* library.
Expand Down Expand Up @@ -909,7 +910,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch

if (is_array($set)) {
foreach ($set as &$row) {
$row = $this->transformDataRowToArray($row);
$row = $this->transformDataToArray($row, 'insert');

// Validate every row.
if (! $this->skipValidation && ! $this->validate($row)) {
Expand Down Expand Up @@ -1036,7 +1037,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
{
if (is_array($set)) {
foreach ($set as &$row) {
$row = $this->transformDataRowToArray($row);
$row = $this->transformDataToArray($row, 'update');

// Validate data before saving.
if (! $this->skipValidation && ! $this->validate($row)) {
Expand Down Expand Up @@ -1667,52 +1668,6 @@ protected function trigger(string $event, array $eventData)
return $eventData;
}

/**
* If the model is using casts, this will convert the data
* in $row according to the rules defined in `$casts`.
*
* @param object|row_array|null $row Row data
*
* @return object|row_array|null Converted row data
*
* @used-by insertBatch()
* @used-by updateBatch()
*
* @throws ReflectionException
* @deprecated Since 4.6.4, temporary solution - will be removed in 4.7
*/
protected function transformDataRowToArray(array|object|null $row): array|object|null
{
// If casts are used, convert the data first
if ($this->useCasts()) {
if (is_array($row)) {
$row = $this->converter->toDataSource($row);
} elseif ($row instanceof stdClass) {
$row = (array) $row;
$row = $this->converter->toDataSource($row);
} elseif ($row instanceof Entity) {
$row = $this->converter->extract($row);
} elseif (is_object($row)) {
$row = $this->converter->extract($row);
}
} elseif (is_object($row) && ! $row instanceof stdClass) {
// If $row is using a custom class with public or protected
// properties representing the collection elements, we need to grab
// them as an array.
$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;
}

// Convert any Time instances to appropriate $dateFormat
return $this->timeToString($row);
}

/**
* Sets the return type of the results to be as an associative array.
*
Expand Down Expand Up @@ -1830,7 +1785,9 @@ protected function objectToRawArray($object, bool $onlyChanged = true, bool $rec
* @throws ReflectionException
*
* @used-by insert()
* @used-by insertBatch()
* @used-by update()
* @used-by updateBatch()
*/
protected function transformDataToArray($row, string $type): array
{
Expand Down
4 changes: 3 additions & 1 deletion tests/system/Models/UpdateModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ public function testUpdateBatchWithEntity(): void
$entity2->deleted = 0;
$entity2->syncOriginal();

$this->assertSame(2, $this->createModel(UserModel::class)->updateBatch([$entity1, $entity2], 'id'));
$model = $this->createModel(UserModel::class);
$this->setPrivateProperty($model, 'updateOnlyChanged', false);
$this->assertSame(2, $model->updateBatch([$entity1, $entity2], 'id'));
}

public function testUpdateNoPrimaryKey(): void
Expand Down
12 changes: 10 additions & 2 deletions user_guide_src/source/changelogs/v4.7.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ update it to use double braces: ``regex_match[/^{{placeholder}}$/]``.
This change was introduced to avoid ambiguity with regular expression syntax,
where single curly braces (e.g., ``{1,3}``) are used for quantifiers.

BaseModel
---------

The ``insertBatch()`` and ``updateBatch()`` methods now honor model settings like
``updateOnlyChanged`` and ``allowEmptyInserts``. This change ensures consistent handling
across all insert/update operations.

Interface Changes
=================

Expand All @@ -45,6 +52,7 @@ Removed Deprecated Items
========================

- **Text Helper:** The deprecated types in ``random_string()`` function: ``basic``, ``md5``, and ``sha1`` has been removed.
- **BaseModel:** The deprecated method ``transformDataRowToArray()`` has been removed.

************
Enhancements
Expand All @@ -55,7 +63,7 @@ Libraries

- **CURLRequest:** Added ``shareConnection`` config item to change default share connection.
- **CURLRequest:** Added ``dns_cache_timeout`` option to change default DNS cache timeout.
- **CURLRequest:** Added ``fresh_connect`` options to enable/disabled request fresh connection.
- **CURLRequest:** Added ``fresh_connect`` options to enable/disable request fresh connection.
- **Email:** Added support for choosing the SMTP authorization method. You can change it via ``Config\Email::$SMTPAuthMethod`` option.
- **Image:** The ``ImageMagickHandler`` has been rewritten to rely solely on the PHP ``imagick`` extension.
- **Image:** Added ``ImageMagickHandler::clearMetadata()`` method to remove image metadata for privacy protection.
Expand All @@ -70,7 +78,7 @@ Testing
Database
========

- **Exception Logging:** All DB drivers now log database exceptions uniformly. Previously, each driver has its own log format.
- **Exception Logging:** All DB drivers now log database exceptions uniformly. Previously, each driver had its own log format.

Query Builder
-------------
Expand Down
16 changes: 8 additions & 8 deletions utils/phpstan-baseline/missingType.property.neon
Original file line number Diff line number Diff line change
Expand Up @@ -473,42 +473,42 @@ parameters:
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$_options has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$_options has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$country has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$country has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$created_at has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$created_at has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$deleted has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$deleted has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$email has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$email has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$id has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$id has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$name has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$name has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

-
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:351\:\:\$updated_at has no type specified\.$#'
message: '#^Property CodeIgniter\\Entity\\Entity@anonymous/tests/system/Models/UpdateModelTest\.php\:353\:\:\$updated_at has no type specified\.$#'
count: 1
path: ../../tests/system/Models/UpdateModelTest.php

Expand Down
Loading