Skip to content

Commit e243037

Browse files
committed
fix: Apply suggestions
1 parent 936545c commit e243037

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

‎docs/models/entities.md‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -785,57 +785,69 @@ For objects and arrays, the Entity JSON-encodes and normalizes the values for co
785785

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

788-
By default, your entity can contain the initial state of the attributes. When working with objects, you can rely on the fact that all the necessary properties are available. However, after receiving the object from the database, you may find that the entity is not complete and some properties are missing.
788+
Your entity can have the initial state and contains all the necessary keys in the `$attributes`. When working with objects, you can rely on the fact that all properties are available. However, attributes missing from the query result are dropped, so reading them later fails or returns `null`.
789789

790-
Suppose there is no `isBanned` field in the table, and the `profile` property is virtual and filled in another way.
790+
Suppose there is no `isBanned` field in the table and the `profile` property is virtual and filled in another way.
791791

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

794794
```php
795795
<?php
796796
class User extends Entity {
797797
protected $attributes = [
798-
'id' => null,
799-
'role' => 'user',
798+
'id' => null,
799+
'role' => 'user',
800800
'isBanned' => '0',
801-
'profile' => null,
801+
'profile' => null,
802802
];
803803
public function __construct(?array $data = null)
804804
{
805805
parent::__construct($data);
806-
$this->attributes['profile'] = new Profile();
806+
$this->attributes['profile'] = new \stdClass();
807807
}
808808
}
809809

810810
// Manual user creation
811811
$user = new User([...]);
812812

813-
// The values are exactly there
814-
echo $user->isBanned; // '0'
815-
echo $user->toArray()['isBanned']; // '0'
816-
var_dump($user->profile instanceof Profile); // true
813+
// The initial values are exactly there
814+
var_dump(
815+
$user->isBanned, // '0'
816+
$user->toArray()['isBanned'], // '0'
817+
$user->profile instanceof \stdClass // true
818+
);
817819

818-
// Getting the user from the database
819-
$user2 = model(UserModel::class)->find(1);
820+
// The result does not contain these values: "isBanned", "profile"
821+
$user2 = model(UserModel::class)->find(10);
822+
// or manually
823+
$user2 = (new User())->injectRawData([
824+
'id' => 10,
825+
'role' => 'user',
826+
]);
820827

821-
// The values will be empty
822-
echo $user2->isBanned; // null
823-
echo $user2->toArray()['isBanned']; // Undefined
824-
var_dump($user2->profile instanceof Profile); // false
828+
var_dump(
829+
$user2->isBanned, // null
830+
array_key_exists('isBanned', $user2->toArray()), // false
831+
$user2->profile instanceof \stdClass // false
832+
);
825833
```
826834

827-
To change the behavior, toggle the `$mergeAttributesOnInject = true` flag. As a result, you will get the expected list of attributes.
835+
Setting the `$mergeAttributesOnInject = true` flag changes the behavior — it keeps the defaults from `$attributes`.
828836

829837
```php
830838
<?php
831839

832-
// Getting the user from the database
833-
$user2 = model(UserModel::class)->find(1);
840+
$user = (new User())->injectRawData([
841+
'id' => 15,
842+
'role' => 'user',
843+
]);
834844

835-
echo $user2->isBanned; // '0'
836-
echo $user2->toArray()['isBanned']; // '0'
837-
var_dump($user2->profile instanceof Profile); // true
845+
var_dump(
846+
$user->isBanned, // '0'
847+
$user->toArray()['isBanned'], // '0'
848+
$user->profile instanceof \stdClass // true
849+
);
838850
```
839851

840852
!!! note "Note"
841-
The flag does not matter if you have not defined the keys in `$attributes`.
853+
The flag does not matter if you have not defined the keys in `$attributes`.

‎system/Entity/Entity.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class Entity implements JsonSerializable
127127
protected ?DataCaster $dataCaster = null;
128128

129129
/**
130-
* Merge the input data (from the Database) with the initial attributes.
130+
* Merge the raw input data with the initial attributes.
131131
*/
132132
protected bool $mergeAttributesOnInject = false;
133133

0 commit comments

Comments
 (0)