Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 9 additions & 1 deletion user_guide_src/source/models/entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======================
Expand Down Expand Up @@ -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
***********************
Expand Down
30 changes: 30 additions & 0 deletions user_guide_src/source/models/entities/028.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class User extends Entity
{
protected $attributes = [
'__secure' => '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!
* )
*/
Loading