Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve Model and Entity #8587

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find()* : Do you mean find(), findColumn(), findAll() ? If yes, I think mentioning these methods makes the explanation clear.is ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and no. Strictly speaking, all built-in Read (in CRUD) methods in the Model.

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