Skip to content

Commit

Permalink
Merge pull request #8587 from kenjis/docs-improve-model-entity
Browse files Browse the repository at this point in the history
docs: improve Model and Entity
  • Loading branch information
kenjis authored Mar 5, 2024
2 parents 67436e3 + a5be00b commit 4c7ca62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
17 changes: 13 additions & 4 deletions user_guide_src/source/models/entities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ Create the model first at **app/Models/UserModel.php** so that we can interact w

.. literalinclude:: entities/002.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
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
built-in 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.

.. note::
Of course, if you add a custom method to your model, you must implement it
so that instances of ``$returnType`` are returned.

Working with the Entity Class
=============================

Expand Down
18 changes: 11 additions & 7 deletions user_guide_src/source/models/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,16 @@ default value is ``true``.
$returnType
-----------

The Model's CRUD methods will take a step of work away from you and automatically return
the resulting data, instead of the Result object. This setting allows you to define
the type of data that is returned. Valid values are '**array**' (the default), '**object**', or the **fully
qualified name of a class** that can be used with the Result object's ``getCustomResultObject()``
method. Using the special ``::class`` constant of the class will allow most IDEs to
auto-complete the name and allow functions like refactoring to better understand your code.
The Model's **find*()** methods will take a step of work away from you and automatically
return the resulting data, instead of the Result object.

This setting allows you to define the type of data that is returned. Valid values
are '**array**' (the default), '**object**', or the **fully qualified name of a class**
that can be used with the Result object's ``getCustomResultObject()`` method.

Using the special ``::class`` constant of the class will allow most IDEs to
auto-complete the name and allow functions like refactoring to better understand
your code.

.. _model-use-soft-deletes:

Expand All @@ -145,7 +149,7 @@ This requires either a DATETIME or INTEGER field in the database as per the mode
`$dateFormat`_ setting. The default field name is ``deleted_at`` however this name can be
configured to any name of your choice by using `$deletedField`_ property.

.. important:: The ``deleted_at`` field must be nullable.
.. important:: The ``deleted_at`` field in the database must be nullable.

$allowedFields
--------------
Expand Down

0 comments on commit 4c7ca62

Please sign in to comment.