Skip to content

Commit 287d5f6

Browse files
committed
fix: Apply suggestions
1 parent 68cd45a commit 287d5f6

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

user_guide_src/source/models/entities.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,15 @@ make the method recursive, in case of nested Entities.
121121
Hidden properties
122122
=================
123123

124-
It is possible to set properties that are only available in raw form. The property must start with an underscore. Casting cannot be applied to such fields.
125-
This is created so that we allow our magic methods a chance to do their thing, but you can use it in another way.
124+
An Entity may have hidden attributes - their names start with an underscore (``_``). By default, these hidden properties
125+
are not included when you call ``toArray()``. However, you can still access them directly if needed.
126+
127+
If you want hidden properties to appear in the ``toArray()`` output, you'll need to use the ``datamap`` feature - it makes
128+
those properties "visible".
129+
130+
Keep in mind that the smart ``__get()`` and ``__set()`` methods (described in the next section) ignore leading underscores
131+
in property names. This means that two attributes with the same name (one starting with ``_`` and one without) will both use
132+
the same getter and setter, so you have to choose which one to handle by default.
126133

127134
.. literalinclude:: entities/028.php
128135

user_guide_src/source/models/entities/028.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,36 @@
66

77
class User extends Entity
88
{
9+
protected $datamap = [
10+
'_role' => '_role',
11+
];
12+
913
protected $attributes = [
1014
'__secure' => 'On',
15+
'_role' => 'user',
1116
'about' => '',
1217
];
1318
}
1419

15-
$user = new User(['__secure' => 'Off', 'about' => 'Hi, I am John!']);
20+
$user = new User(['__secure' => 'Off', 'about' => 'Hi, I am John!', '_role' => 'admin']);
1621

22+
echo 'Secure: ' . $user->__secure;
1723
print_r($user->toArray());
1824
print_r($user->toRawArray());
1925

2026
/**
2127
* Output:
28+
*
29+
* Secure: Off
30+
* Array
2231
* (
2332
* [about] => Hi, I am John!
33+
* [_role] => admin
2434
* )
2535
* Array
2636
* (
2737
* [__secure] => Off
28-
* [about] => Hi, I am John!
38+
* [_role] => admin
39+
* [about] => Hi, I am John!
2940
* )
3041
*/

0 commit comments

Comments
 (0)