From 68cd45a6483eff54315c0967fdd8fe782bbfdac1 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Sat, 11 Oct 2025 11:49:36 +0300 Subject: [PATCH 1/2] docs: Note of hidden properties --- tests/system/Entity/EntityTest.php | 2 +- user_guide_src/source/models/entities.rst | 10 ++++++- user_guide_src/source/models/entities/028.php | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 user_guide_src/source/models/entities/028.php diff --git a/tests/system/Entity/EntityTest.php b/tests/system/Entity/EntityTest.php index 92ed11aa36b6..d37229d8f5be 100644 --- a/tests/system/Entity/EntityTest.php +++ b/tests/system/Entity/EntityTest.php @@ -46,7 +46,7 @@ public function testSetStringToPropertyNamedAttributes(): void } /** - * @see https://github.com/codeigniter4/CodeIgniter4/issues + * @see https://github.com/codeigniter4/CodeIgniter4/issues/5762 */ public function testSetArrayToPropertyNamedAttributes(): void { diff --git a/user_guide_src/source/models/entities.rst b/user_guide_src/source/models/entities.rst index ce5313f4115d..174e84033a48 100644 --- a/user_guide_src/source/models/entities.rst +++ b/user_guide_src/source/models/entities.rst @@ -40,7 +40,7 @@ Assume you have a database table named ``users`` that has the following schema:: password - string created_at - datetime -.. important:: ``attributes`` is a reserved word for internal use. If you use it as a column name, the Entity does not work correctly. +.. 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. Create the Entity Class ======================= @@ -118,6 +118,14 @@ Using the raw version will bypass magic "getter" methods and casts. Both methods to specify whether returned values should be filtered by those that have changed, and a boolean final parameter to make the method recursive, in case of nested Entities. +Hidden properties +================= + +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. +This is created so that we allow our magic methods a chance to do their thing, but you can use it in another way. + +.. literalinclude:: entities/028.php + *********************** Handling Business Logic *********************** diff --git a/user_guide_src/source/models/entities/028.php b/user_guide_src/source/models/entities/028.php new file mode 100644 index 000000000000..1cceb5e5974a --- /dev/null +++ b/user_guide_src/source/models/entities/028.php @@ -0,0 +1,30 @@ + 'On', + 'about' => '', + ]; +} + +$user = new User(['__secure' => 'Off', 'about' => 'Hi, I am John!']); + +print_r($user->toArray()); +print_r($user->toRawArray()); + +/** + * Output: + * ( + * [about] => Hi, I am John! + * ) + * Array + * ( + * [__secure] => Off + * [about] => Hi, I am John! + * ) + */ From 287d5f69d27221ed3f05d6319f56761efde4a3d7 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Mon, 13 Oct 2025 17:16:19 +0300 Subject: [PATCH 2/2] fix: Apply suggestions --- user_guide_src/source/models/entities.rst | 11 +++++++++-- user_guide_src/source/models/entities/028.php | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/models/entities.rst b/user_guide_src/source/models/entities.rst index 174e84033a48..05afeae947b8 100644 --- a/user_guide_src/source/models/entities.rst +++ b/user_guide_src/source/models/entities.rst @@ -121,8 +121,15 @@ make the method recursive, in case of nested Entities. Hidden properties ================= -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. -This is created so that we allow our magic methods a chance to do their thing, but you can use it in another way. +An Entity may have hidden attributes - their names start with an underscore (``_``). By default, these hidden properties +are not included when you call ``toArray()``. However, you can still access them directly if needed. + +If you want hidden properties to appear in the ``toArray()`` output, you'll need to use the ``datamap`` feature - it makes +those properties "visible". + +Keep in mind that the smart ``__get()`` and ``__set()`` methods (described in the next section) ignore leading underscores +in property names. This means that two attributes with the same name (one starting with ``_`` and one without) will both use +the same getter and setter, so you have to choose which one to handle by default. .. literalinclude:: entities/028.php diff --git a/user_guide_src/source/models/entities/028.php b/user_guide_src/source/models/entities/028.php index 1cceb5e5974a..c45bbb8597c5 100644 --- a/user_guide_src/source/models/entities/028.php +++ b/user_guide_src/source/models/entities/028.php @@ -6,25 +6,36 @@ class User extends Entity { + protected $datamap = [ + '_role' => '_role', + ]; + protected $attributes = [ '__secure' => 'On', + '_role' => 'user', 'about' => '', ]; } -$user = new User(['__secure' => 'Off', 'about' => 'Hi, I am John!']); +$user = new User(['__secure' => 'Off', 'about' => 'Hi, I am John!', '_role' => 'admin']); +echo 'Secure: ' . $user->__secure; print_r($user->toArray()); print_r($user->toRawArray()); /** * Output: + * + * Secure: Off + * Array * ( * [about] => Hi, I am John! + * [_role] => admin * ) * Array * ( * [__secure] => Off - * [about] => Hi, I am John! + * [_role] => admin + * [about] => Hi, I am John! * ) */