Skip to content

Commit 72e8db0

Browse files
committed
docs: An exceptional case for the model
1 parent aea3f9d commit 72e8db0

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎docs/models/entities.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,3 +863,5 @@ The direct call to `injectRawData()` illustrates what happens when a model confi
863863
An attribute retained after loading may differ from the value stored in the database. For example, suppose the `isBanned` column contains `'1'`. If the query selects only `id` and `role`, the loaded entity retains its default of `'0'` for `isBanned`.
864864

865865
With [`$updateOnlyChanged`](model.md#updateonlychanged) enabled (the default), changing `role` and updating the entity leaves the stored `isBanned` value alone. With `$updateOnlyChanged` disabled, the same update can also write `'0'` to `isBanned` if the field is in [`$allowedFields`](model.md#allowedfields). To update just the role, pass `['role' => $newRole]` to the model's `update()` method.
866+
867+
When `$skipValidation = false` and [`$cleanValidationRules = false`](model.md#cleanvalidationrules), `BaseModel::transformDataToArray()` extracts all attributes regardless of `$updateOnlyChanged`. Retained defaults can therefore overwrite omitted database columns even with `$updateOnlyChanged = true`.

‎tests/system/Models/UpdateModelTest.php‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ public function testUpdateOnlyChangedAndMergeAttributesOnInjectFlags(UserModel $
691691
{
692692
$member = $model->select('id, name')
693693
->select('\'new property\' AS ' . $model->db->escapeIdentifier('virtual'), false)
694-
->withDeleted()->find(1);
694+
->find(1);
695695

696696
$this->assertInstanceOf(Member::class, $member);
697697
$this->assertSame($selectedMember, $member->toRawArray());
@@ -702,7 +702,7 @@ public function testUpdateOnlyChangedAndMergeAttributesOnInjectFlags(UserModel $
702702
$result = $model->update(1, $member);
703703

704704
$this->assertTrue($result);
705-
$memberSilentOverwrite = $model->withDeleted()->find(1);
705+
$memberSilentOverwrite = $model->find(1);
706706

707707
$this->assertSame($updatedMember, $memberSilentOverwrite->toRawArray());
708708
}
@@ -821,6 +821,43 @@ public static function provideUpdateOnlyChangedAndMergeAttributesOnInjectFlags()
821821
'profile' => 'profile',
822822
],
823823
];
824+
825+
yield 'An exceptional case: updateOnlyChanged=false, mergeAttributesOnInject=true, with validation fields' => [
826+
new class () extends UserModel {
827+
protected $returnType = MemberStrict::class;
828+
protected bool $updateOnlyChanged = false;
829+
protected array $casts = [
830+
'id' => '?int',
831+
];
832+
protected $skipValidation = false;
833+
protected $cleanValidationRules = false;
834+
protected $validationRules = [
835+
'id' => 'integer',
836+
'name' => 'string',
837+
];
838+
},
839+
[
840+
'id' => 1,
841+
'name' => 'Derek Jones',
842+
'email' => 'unknown',
843+
'country' => 'unknown',
844+
'created_at' => '2026-11-10 14:50:00',
845+
'updated_at' => '2026-11-10 14:50:00',
846+
'deleted_at' => null,
847+
'profile' => 'profile',
848+
'virtual' => 'new property',
849+
],
850+
[
851+
'id' => 1,
852+
'name' => 'Truman Burbank',
853+
'email' => 'unknown',
854+
'country' => 'unknown',
855+
'created_at' => null,
856+
'updated_at' => null,
857+
'deleted_at' => null,
858+
'profile' => 'profile',
859+
],
860+
];
824861
}
825862

826863
public function testUpdateBatchWithCasts(): void

0 commit comments

Comments
 (0)