Skip to content

Commit 6815f3f

Browse files
authored
Merge pull request #7807 from kenjis/fix-docs-entities.rst
docs: fix entities.rst
2 parents 4bc7a02 + 2820fe4 commit 6815f3f

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

user_guide_src/source/models/entities.rst

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ be used directly with the :doc:`Model </models/model>` if that fits your needs b
1010
:local:
1111
:depth: 2
1212

13+
************
1314
Entity Usage
14-
============
15+
************
1516

1617
At its core, an Entity class is simply a class that represents a single database row. It has class properties
1718
to represent the database columns, and provides any additional methods to implement the business logic for
@@ -34,7 +35,7 @@ Assume you have a database table named ``users`` that has the following schema::
3435
.. important:: ``attributes`` is a reserved word for internal use. If you use it as a column name, the Entity does not work correctly.
3536

3637
Create the Entity Class
37-
-----------------------
38+
=======================
3839

3940
Now create a new Entity class. Since there's no default location to store these classes, and it doesn't fit
4041
in with the existing directory structure, create a new directory at **app/Entities**. Create the
@@ -45,7 +46,7 @@ Entity itself at **app/Entities/User.php**.
4546
At its simplest, this is all you need to do, though we'll make it more useful in a minute.
4647

4748
Create the Model
48-
----------------
49+
================
4950

5051
Create the model first at **app/Models/UserModel.php** so that we can interact with it:
5152

@@ -58,7 +59,7 @@ class as the ``$returnType``. This ensures that all methods on the model that re
5859
instances of our User Entity class instead of an object or array like normal.
5960

6061
Working with the Entity Class
61-
-----------------------------
62+
=============================
6263

6364
Now that all of the pieces are in place, you would work with the Entity class as you would any other class:
6465

@@ -79,7 +80,7 @@ a new row, or update an existing one.
7980
call the ``update()``, then only values that have changed are passed.
8081

8182
Filling Properties Quickly
82-
--------------------------
83+
==========================
8384

8485
The Entity class also provides a method, ``fill()`` that allows you to shove an array of key/value pairs into the class
8586
and populate the class properties. Any property in the array will be set on the Entity. However, when saving through
@@ -93,15 +94,16 @@ You can also pass the data in the constructor and the data will be passed throug
9394
.. literalinclude:: entities/005.php
9495

9596
Bulk Accessing Properties
96-
-------------------------
97+
=========================
9798

9899
The Entity class has two methods to extract all available properties into an array: ``toArray()`` and ``toRawArray()``.
99100
Using the raw version will bypass magic "getter" methods and casts. Both methods can take a boolean first parameter
100101
to specify whether returned values should be filtered by those that have changed, and a boolean final parameter to
101102
make the method recursive, in case of nested Entities.
102103

104+
***********************
103105
Handling Business Logic
104-
=======================
106+
***********************
105107

106108
While the examples above are convenient, they don't help enforce any business logic. The base Entity class implements
107109
some smart ``__get()`` and ``__set()`` methods that will check for special methods and use those instead of using
@@ -131,8 +133,9 @@ business logic and create objects that are pleasant to use.
131133

132134
.. literalinclude:: entities/007.php
133135

136+
************
134137
Data Mapping
135-
============
138+
************
136139

137140
At many points in your career, you will run into situations where the use of an application has changed and the
138141
original column names in the database no longer make sense. Or you find that your coding style prefers camelCase
@@ -168,11 +171,12 @@ to the database. However, ``unset()`` and ``isset()`` only work on the mapped pr
168171
for the database column name. In this example, you must define ``setFullName()`` and
169172
``getFullName()``.
170173

174+
********
171175
Mutators
172-
========
176+
********
173177

174178
Date Mutators
175-
-------------
179+
=============
176180

177181
By default, the Entity class will convert fields named `created_at`, `updated_at`, or `deleted_at` into
178182
:doc:`Time </libraries/time>` instances whenever they are set or retrieved. The Time class provides a large number
@@ -190,12 +194,19 @@ current timezone, as set in **app/Config/App.php**:
190194
.. _entities-property-casting:
191195

192196
Property Casting
193-
----------------
197+
================
194198

195199
You can specify that properties in your Entity should be converted to common data types with the ``$casts`` property.
196200
This option should be an array where the key is the name of the class property, and the value is the data type it
197-
should be cast to. Casting only affects when values are read. No conversions happen that affect the permanent value in
198-
either the entity or the database. Properties can be cast to any of the following data types:
201+
should be cast to.
202+
203+
Property casting affects both read (get) and write (set), but some types affect
204+
only read (get).
205+
206+
Scalar Type Casting
207+
-------------------
208+
209+
Properties can be cast to any of the following data types:
199210
**integer**, **float**, **double**, **string**, **boolean**, **object**, **array**, **datetime**, **timestamp**, **uri** and **int-bool**.
200211
Add a question mark at the beginning of type to mark property as nullable, i.e., **?string**, **?integer**.
201212

@@ -242,7 +253,7 @@ Stored in the database as "red,yellow,green":
242253

243254
.. note:: Casting as CSV uses PHP's internal ``implode`` and ``explode`` methods and assumes all values are string-safe and free of commas. For more complex data casts try ``array`` or ``json``.
244255

245-
Custom casting
256+
Custom Casting
246257
--------------
247258

248259
You can define your own conversion types for getting and setting data.
@@ -260,12 +271,12 @@ If you don't need to change values when getting or setting a value. Then just do
260271

261272
.. literalinclude:: entities/019.php
262273

263-
**Parameters**
274+
Parameters
275+
----------
264276

265277
In some cases, one type is not enough. In this situation, you can use additional parameters.
266-
Additional parameters are indicated in square brackets and listed with a comma.
267-
268-
**type[param1, param2]**
278+
Additional parameters are indicated in square brackets and listed with a comma
279+
like ``type[param1, param2]``.
269280

270281
.. literalinclude:: entities/020.php
271282

@@ -275,8 +286,9 @@ Additional parameters are indicated in square brackets and listed with a comma.
275286
the value ``nullable`` will be passed to the casting type handler.
276287
If casting type has predefined parameters, then ``nullable`` will be added to the end of the list.
277288

289+
*******************************
278290
Checking for Changed Attributes
279-
===============================
291+
*******************************
280292

281293
You can check if an Entity attribute has changed since it was created. The only parameter is the name of the
282294
attribute to check:

0 commit comments

Comments
 (0)