Skip to content

Commit 24dcc8b

Browse files
committed
fix: Update docs
1 parent 2ca3667 commit 24dcc8b

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎docs/installation/upgrade_480.md‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ public function handle($level, $message, array $context = []): bool
144144

145145
The context array may contain the CI global context data under the `HandlerInterface::GLOBAL_CONTEXT_KEY` (`'_ci_context'`) key when `$logGlobalContext` is enabled in `Config\Logger`.
146146

147-
### Entity
148-
149-
If you redefine the Entity base class or modify the `injectRawData()` method, you need to add a new `$mergeAttributesOnInject` property. See [Merging Attributes During Injection](../models/entities.md#merging-attributes-during-injection).
150-
151147
## Project Files
152148

153149
Some files in the **project space** (root, app, public, writable) received updates. Due to these files being outside of the **system** scope they will not be changed without your intervention.

‎docs/models/entities.md‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ For scalar values (strings, integers, floats, booleans, null), the Entity uses d
781781

782782
For objects and arrays, the Entity JSON-encodes and normalizes the values for comparison. This means that modifications to nested structures, object properties, array elements, nested entities (using `toRawArray()`), enums (`BackedEnum` and `UnitEnum`), datetime objects (`DateTimeInterface`), collections (`Traversable`), value objects with `__toString()`, and objects implementing `JsonSerializable` or `toArray()` will be properly detected.
783783

784-
### Merging Attributes During Injection
784+
## Merging Attributes During Injection
785785

786786
!!! success "Available from version 4.8.0"
787787

@@ -791,9 +791,12 @@ Suppose there is no `isBanned` field in the table and the `profile` property is
791791

792792
By default, with the `$mergeAttributesOnInject = false` you will get errors because by initialized properties are overwritten in `injectRawData()`.
793793

794+
<!-- lint-ignore -->
794795
```php
795796
<?php
796-
class User extends Entity {
797+
798+
class User extends Entity
799+
{
797800
protected $attributes = [
798801
'id' => null,
799802
'role' => 'user',
@@ -808,17 +811,18 @@ class User extends Entity {
808811
}
809812

810813
// Manual user creation
811-
$user = new User([...]);
814+
$user = new User();
812815

813816
// The initial values are exactly there
814817
var_dump(
815818
$user->isBanned, // '0'
816819
$user->toArray()['isBanned'], // '0'
817-
$user->profile instanceof \stdClass // true
820+
$user->profile instanceof stdClass, // true
821+
$user->hasChanged(), // true
818822
);
819823

820824
// The result does not contain these values: "isBanned", "profile"
821-
$user2 = model(UserModel::class)->find(10);
825+
$user2 = model(UserModel::class)->select('`id`, `role`')->find(10);
822826
// or manually
823827
$user2 = (new User())->injectRawData([
824828
'id' => 10,
@@ -828,11 +832,13 @@ $user2 = (new User())->injectRawData([
828832
var_dump(
829833
$user2->isBanned, // null
830834
array_key_exists('isBanned', $user2->toArray()), // false
831-
$user2->profile instanceof \stdClass // false
835+
$user2->profile instanceof \stdClass, // false
836+
$user2->hasChanged(), // false
832837
);
833838
```
834839

835840
Setting the `$mergeAttributesOnInject = true` flag changes the behavior — it keeps the defaults from `$attributes`.
841+
After `injectRawData()`, the entire merged state becomes the entity's original state, including retained defaults. Consequently, `hasChanged()` returns `false` immediately after injection, even if an injected value differs from the default declared in `$attributes`.
836842

837843
<!-- lint-ignore -->
838844
```php
@@ -843,11 +849,17 @@ $user = (new User())->injectRawData([
843849
]);
844850

845851
var_dump(
846-
$user->isBanned, // '0'
847-
$user->toArray()['isBanned'], // '0'
848-
$user->profile instanceof \stdClass // true
852+
$user->isBanned, // '0'
853+
$user->toArray()['isBanned'], // '0'
854+
$user->profile instanceof \stdClass, // true
855+
$user->hasChanged() // false
849856
);
850857
```
851858

852859
!!! note "Note"
853860
The flag does not matter if you have not defined the keys in `$attributes`.
861+
862+
Enabling this option also affects saving partially loaded entities. Suppose the entity defines `'isBanned' => '0'`, but the database row contains `'isBanned' = '1'`. If the model selects only `id` and `name`, the hydrated entity retains the default `'isBanned' = '0'`. With `Model::$updateOnlyChanged = false` and `isBanned` included in `$allowedFields`, changing `name` and saving this entity also overwrites the database `isBanned` with `'0'`, even though that column was never selected.
863+
864+
In both cases, all additional keys that are passed in will still be accessible on the
865+
entity object, even if they are not listed in `$attributes`.

0 commit comments

Comments
 (0)