CodeIgniter supports Entity classes as a first-class citizen in it’s database layer, while keeping
them completely optional to use. They are commonly used as part of the Repository pattern, but can
be used directly with the Model if that fits your needs better.

At its core, an Entity class is simply a class that represents a single database row. It has class properties
to represent the database columns, and provides any additional methods to implement the business logic for
that row. The core feature, though, is that it doesn’t know anything about how to persist itself. That’s the
responsibility of the model or the repository class. That way, if anything changes on how you need to save the
object, you don’t have to change how that object is used throughout the application. This makes it possible to
use JSON or XML files to store the objects during a rapid prototyping stage, and then easily switch to a
database when you’ve proven the concept works.

Let’s walk through a very simple User Entity and how we’d work with it to help make things clear.

Assume you have a database table named users that has the following schema:

Now create a new Entity class. Since there’s no default location to store these classes, and it doesn’t fit
in with the existing directory structure, create a new directory at app/Entities. Create the
Entity itself at app/Entities/User.php.

The model uses the users table in the database for all of its activities. We’ve set the $allowedFields property
to include all of the fields that we want outside classes to change. The id, created_at, and updated_at fields
are handled automatically by the class or the database, so we don’t want to change those. Finally, we’ve set our Entity
class as the $returnType. This ensures that all methods on the model that return rows from the database will return
instances of our User Entity class instead of an object or array like normal.

You may have noticed that the User class has not set any properties for the columns, but you can still
access them as if they were public properties. The base class, CodeIgniterEntity, takes care of this for you, as
well as providing the ability to check the properties with isset(), or unset() the property, and keep track
of what columns have changed since the object was created or pulled from the database.

When the User is passed to the model’s save() method, it automatically takes care of reading the properties
and saving any changes to columns listed in the model’s $allowedFields property. It also knows whether to create
a new row, or update an existing one.

The Entity class also provides a method, fill() that allows you to shove an array of key/value pairs into the class
and populate the class properties. Any property in the array will be set on the Entity. However, when saving through
the model, only the fields in $allowedFields will actually be saved to the database, so you can store additional data
on your entities without worrying much about stray fields getting saved incorrectly.

While the examples above are convenient, they don’t help enforce any business logic. The base Entity class implements
some smart __get() and __set() methods that will check for special methods and use those instead of using
the attributes directly, allowing you to enforce any business logic or data conversion that you need.

Here’s an updated User entity to provide some examples of how this could be used:

The first thing to notice is the name of the methods we’ve added. For each one, the class expects the snake_case
column name to be converted into PascalCase, and prefixed with either set or get. These methods will then
be automatically called whenever you set or retrieve the class property using the direct syntax (i.e. $user->email).
The methods do not need to be public unless you want them accessed from other classes. For example, the created_at
class property will be accessed through the setCreatedAt() and getCreatedAt() methods.

Note

This only works when trying to access the properties from outside of the class. Any methods internal to the
class must call the setX() and getX() methods directly.

In the setPassword() method we ensure that the password is always hashed.

In setCreatedAt() we convert the string we receive from the model into a DateTime object, ensuring that our timezone
is UTC so we can easily convert the viewer’s current timezone. In getCreatedAt(), it converts the time to
a formatted string in the application’s current timezone.

While fairly simple, these examples show that using Entity classes can provide a very flexible way to enforce
business logic and create objects that are pleasant to use.

// Auto-hash the password - both do the same thing$user->password='my great password';$user->setPassword('my great password');

At many points in your career, you will run into situations where the use of an application has changed and the
original column names in the database no longer make sense. Or you find that your coding style prefers camelCase
class properties, but your database schema required snake_case names. These situations can be easily handled
with the Entity class’ data mapping features.

As an example, imagine you have the simplified User Entity that is used throughout your application:

Your boss comes to you and says that no one uses usernames anymore, so you’re switching to just use emails for login.
But they do want to personalize the application a bit, so they want you to change the name field to represent a user’s
full name now, not their username like it does currently. To keep things tidy and ensure things continue making sense
in the database you whip up a migration to rename the name field to full_name for clarity.

Ignoring how contrived this example is, we now have two choices on how to fix the User class. We could modify the class
property from $name to $full_name, but that would require changes throughout the application. Instead, we can
simply map the full_name column in the database to the $name property, and be done with the Entity changes:

By adding our new database name to the $datamap array, we can tell the class what class property the database column
should be accessible through. The key of the array is the name of the column in the database, where the value in the array
is class property to map it to.

In this example, when the model sets the full_name field on the User class, it actually assigns that value to the
class’ $name property, so it can be set and retrieved through $user->name. The value will still be accessible
through the original $user->full_name, also, as this is needed for the model to get the data back out and save it
to the database. However, unset and isset only work on the mapped property, $name, not on the original name,
full_name.

By default, the Entity class will convert fields named created_at, updated_at, or deleted_at into
Time instances whenever they are set or retrieved. The Time class provides a large number
of helpful methods in an immutable, localized way.

You can define which properties are automatically converted by adding the name to the options[‘dates’] array:

Now, when any of those properties are set, they will be converted to a Time instance, using the application’s
current timezone, as set in app/Config/App.php:

$user=newApp\Entities\User();// Converted to Time instance$user->created_at='April 15, 2017 10:30:00';// Can now use any Time methods:echo$user->created_at->humanize();echo$user->created_at->setTimezone('Europe/London')->toDateString();

You can specify that properties in your Entity should be converted to common data types with the casts property.
This option should be an array where the key is the name of the class property, and the value is the data type it
should be cast to. Casting only affects when values are read. No conversions happen that affect the permanent value in
either the entity or the database. Properties can be cast to any of the following data types:
integer, float, double, string, boolean, object, array, datetime, and timestamp.
Add a question mark at the beginning of type to mark property as nullable, i.e. ?string, ?integer.

For example, if you had a User entity with an is_banned property, you can cast it as a boolean: