You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e8cdf6d
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/installation/upgrade_480.md
-4Lines changed: 0 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -144,10 +144,6 @@ public function handle($level, $message, array $context = []): bool
144
144
145
145
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`.
146
146
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
-
151
147
## Project Files
152
148
153
149
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.
Copy file name to clipboardExpand all lines: docs/models/entities.md
+21-9Lines changed: 21 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -781,7 +781,7 @@ For scalar values (strings, integers, floats, booleans, null), the Entity uses d
781
781
782
782
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.
783
783
784
-
###Merging Attributes During Injection
784
+
## Merging Attributes During Injection
785
785
786
786
!!! success "Available from version 4.8.0"
787
787
@@ -791,9 +791,12 @@ Suppose there is no `isBanned` field in the table and the `profile` property is
791
791
792
792
By default, with the `$mergeAttributesOnInject = false` you will get errors because by initialized properties are overwritten in `injectRawData()`.
793
793
794
+
<!-- lint-ignore -->
794
795
```php
795
796
<?php
796
-
class User extends Entity {
797
+
798
+
class User extends Entity
799
+
{
797
800
protected $attributes = [
798
801
'id' => null,
799
802
'role' => 'user',
@@ -808,17 +811,18 @@ class User extends Entity {
808
811
}
809
812
810
813
// Manual user creation
811
-
$user = new User([...]);
814
+
$user = new User();
812
815
813
816
// The initial values are exactly there
814
817
var_dump(
815
818
$user->isBanned, // '0'
816
819
$user->toArray()['isBanned'], // '0'
817
-
$user->profile instanceof \stdClass // true
820
+
$user->profile instanceof stdClass, // true
821
+
$user->hasChanged(), // true
818
822
);
819
823
820
824
// The result does not contain these values: "isBanned", "profile"
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`.
836
842
837
843
<!-- lint-ignore -->
838
844
```php
@@ -843,11 +849,17 @@ $user = (new User())->injectRawData([
843
849
]);
844
850
845
851
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
849
856
);
850
857
```
851
858
852
859
!!! note "Note"
853
860
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