Skip to content

Commit ce71e60

Browse files
committed
docs: Note of hidden properties
1 parent 01a45be commit ce71e60

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
@@ -50,7 +50,7 @@ public function testSetStringToPropertyNamedAttributes(): void
5050
}
5151

5252
/**
53-
* @see https://github.com/codeigniter4/CodeIgniter4/issues
53+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5762
5454
*/
5555
public function testSetArrayToPropertyNamedAttributes(): void
5656
{
@@ -1296,6 +1296,52 @@ public function testJsonSerializableEntity(): void
12961296
$this->assertSame(json_encode($entity->toArray()), json_encode($entity));
12971297
}
12981298

1299+
public function testInjectRawArray(): void
1300+
{
1301+
$entity = new class () extends Entity {
1302+
// The "user" property is not for DB
1303+
protected $attributes = [
1304+
'type' => 'Normal',
1305+
'limit' => 10,
1306+
'user' => 'John',
1307+
'_secure' => 'High',
1308+
];
1309+
protected $original = [
1310+
'type' => 'None',
1311+
'limit' => 0,
1312+
'user' => null,
1313+
'_secure' => 'Low',
1314+
];
1315+
};
1316+
1317+
$entity->injectRawData([
1318+
'type' => 'High',
1319+
'limit' => 15,
1320+
'_secure' => 'Normal',
1321+
'extra' => 'undefined',
1322+
]);
1323+
1324+
$this->assertSame(
1325+
[
1326+
'type' => 'High',
1327+
'limit' => 15,
1328+
'user' => 'John',
1329+
'_secure' => 'Normal',
1330+
'extra' => 'undefined',
1331+
],
1332+
$entity->toRawArray(),
1333+
);
1334+
$this->assertSame(
1335+
[
1336+
'type' => 'High',
1337+
'limit' => 15,
1338+
'user' => 'John',
1339+
'extra' => 'undefined',
1340+
],
1341+
$entity->toArray(),
1342+
);
1343+
}
1344+
12991345
private function getEntity(): object
13001346
{
13011347
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)