Skip to content

Commit 9a0caa2

Browse files
committed
docs: Note of hidden properties
1 parent 8c6fb5f commit 9a0caa2

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

tests/system/Entity/EntityTest.php

Lines changed: 47 additions & 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
{
@@ -1094,6 +1094,52 @@ public function testJsonSerializableEntity(): void
10941094
$this->assertSame(json_encode($entity->toArray()), json_encode($entity));
10951095
}
10961096

1097+
public function testInjectRawArray(): void
1098+
{
1099+
$entity = new class () extends Entity {
1100+
// The "user" property is not for DB
1101+
protected $attributes = [
1102+
'type' => 'Normal',
1103+
'limit' => 10,
1104+
'user' => 'John',
1105+
'_secure' => 'High',
1106+
];
1107+
protected $original = [
1108+
'type' => 'None',
1109+
'limit' => 0,
1110+
'user' => null,
1111+
'_secure' => 'Low',
1112+
];
1113+
};
1114+
1115+
$entity->injectRawData([
1116+
'type' => 'High',
1117+
'limit' => 15,
1118+
'_secure' => 'Normal',
1119+
'extra' => 'undefined',
1120+
]);
1121+
1122+
$this->assertSame(
1123+
[
1124+
'type' => 'High',
1125+
'limit' => 15,
1126+
'user' => 'John',
1127+
'_secure' => 'Normal',
1128+
'extra' => 'undefined',
1129+
],
1130+
$entity->toRawArray(),
1131+
);
1132+
$this->assertSame(
1133+
[
1134+
'type' => 'High',
1135+
'limit' => 15,
1136+
'user' => 'John',
1137+
'extra' => 'undefined',
1138+
],
1139+
$entity->toArray(),
1140+
);
1141+
}
1142+
10971143
private function getEntity(): object
10981144
{
10991145
return new class () extends Entity {

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)