Skip to content

Commit 68cd45a

Browse files
committed
docs: Note of hidden properties
1 parent 8c6fb5f commit 68cd45a

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-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: 9 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,14 @@ 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+
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.
126+
127+
.. literalinclude:: entities/028.php
128+
121129
***********************
122130
Handling Business Logic
123131
***********************
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Entities;
4+
5+
use CodeIgniter\Entity\Entity;
6+
7+
class User extends Entity
8+
{
9+
protected $attributes = [
10+
'__secure' => 'On',
11+
'about' => '',
12+
];
13+
}
14+
15+
$user = new User(['__secure' => 'Off', 'about' => 'Hi, I am John!']);
16+
17+
print_r($user->toArray());
18+
print_r($user->toRawArray());
19+
20+
/**
21+
* Output:
22+
* (
23+
* [about] => Hi, I am John!
24+
* )
25+
* Array
26+
* (
27+
* [__secure] => Off
28+
* [about] => Hi, I am John!
29+
* )
30+
*/

0 commit comments

Comments
 (0)