Skip to content

Commit 8e8bfbc

Browse files
authored
docs: Note of hidden properties (codeigniter4#9754)
* docs: Note of hidden properties * fix: Apply suggestions
1 parent 76892a3 commit 8e8bfbc

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

tests/system/Entity/EntityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testSetStringToPropertyNamedAttributes(): void
4646
}
4747

4848
/**
49-
* @see https://github.com/codeigniter4/CodeIgniter4/issues
49+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5762
5050
*/
5151
public function testSetArrayToPropertyNamedAttributes(): void
5252
{

user_guide_src/source/models/entities.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Assume you have a database table named ``users`` that has the following schema::
4040
password - string
4141
created_at - datetime
4242

43-
.. important:: ``attributes`` is a reserved word for internal use. If you use it as a column name, the Entity does not work correctly.
43+
.. important:: ``attributes`` is a reserved word for internal use. Prior to v4.4.0, if you use it as a column name, the Entity does not work correctly.
4444

4545
Create the Entity Class
4646
=======================
@@ -118,6 +118,21 @@ Using the raw version will bypass magic "getter" methods and casts. Both methods
118118
to specify whether returned values should be filtered by those that have changed, and a boolean final parameter to
119119
make the method recursive, in case of nested Entities.
120120

121+
Hidden properties
122+
=================
123+
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.
133+
134+
.. literalinclude:: entities/028.php
135+
121136
***********************
122137
Handling Business Logic
123138
***********************
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Entities;
4+
5+
use CodeIgniter\Entity\Entity;
6+
7+
class User extends Entity
8+
{
9+
protected $datamap = [
10+
'_role' => '_role',
11+
];
12+
13+
protected $attributes = [
14+
'__secure' => 'On',
15+
'_role' => 'user',
16+
'about' => '',
17+
];
18+
}
19+
20+
$user = new User(['__secure' => 'Off', 'about' => 'Hi, I am John!', '_role' => 'admin']);
21+
22+
echo 'Secure: ' . $user->__secure;
23+
print_r($user->toArray());
24+
print_r($user->toRawArray());
25+
26+
/**
27+
* Output:
28+
*
29+
* Secure: Off
30+
* Array
31+
* (
32+
* [about] => Hi, I am John!
33+
* [_role] => admin
34+
* )
35+
* Array
36+
* (
37+
* [__secure] => Off
38+
* [_role] => admin
39+
* [about] => Hi, I am John!
40+
* )
41+
*/

0 commit comments

Comments
 (0)