Skip to content

Commit d558616

Browse files
authored
Merge pull request #8200 from kenjis/docs-improve-Model
docs: improve Model
2 parents 47551ae + de0f67f commit d558616

File tree

1 file changed

+58
-49
lines changed

1 file changed

+58
-49
lines changed

user_guide_src/source/models/model.rst

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ is used with methods like ``find()`` to know what column to match the specified
111111
$useAutoIncrement
112112
-----------------
113113

114-
Specifies if the table uses an auto-increment feature for ``$primaryKey``. If set to ``false``
114+
Specifies if the table uses an auto-increment feature for `$primaryKey`_. If set to ``false``
115115
then you are responsible for providing primary key value for every record in the table. This
116116
feature may be handy when we want to implement 1:1 relation or use UUIDs for our model. The
117117
default value is ``true``.
118118

119-
.. note:: If you set ``$useAutoIncrement`` to ``false``, then make sure to set your primary
119+
.. note:: If you set `$useAutoIncrement`_ to ``false``, then make sure to set your primary
120120
key in the database to ``unique``. This way you will make sure that all of Model's features
121121
will still work the same as before.
122122

@@ -142,8 +142,8 @@ part of a security trail. If true, the **find*()** methods will only return non-
142142
the ``withDeleted()`` method is called prior to calling the **find*()** method.
143143

144144
This requires either a DATETIME or INTEGER field in the database as per the model's
145-
``$dateFormat`` setting. The default field name is ``deleted_at`` however this name can be
146-
configured to any name of your choice by using ``$deletedField`` property.
145+
`$dateFormat`_ setting. The default field name is ``deleted_at`` however this name can be
146+
configured to any name of your choice by using `$deletedField`_ property.
147147

148148
.. important:: The ``deleted_at`` field must be nullable.
149149

@@ -155,7 +155,7 @@ This array should be updated with the field names that can be set during ``save(
155155
against just taking input from a form and throwing it all at the model, resulting in
156156
potential mass assignment vulnerabilities.
157157

158-
.. note:: The ``$primaryKey`` field should never be an allowed field.
158+
.. note:: The `$primaryKey`_ field should never be an allowed field.
159159

160160
Dates
161161
-----
@@ -164,29 +164,29 @@ $useTimestamps
164164
^^^^^^^^^^^^^^
165165

166166
This boolean value determines whether the current date is automatically added to all inserts
167-
and updates. If true, will set the current time in the format specified by ``$dateFormat``. This
167+
and updates. If ``true``, will set the current time in the format specified by `$dateFormat`_. This
168168
requires that the table have columns named **created_at**, **updated_at** and **deleted_at** in the appropriate
169-
data type.
169+
data type. See also `$createdField`_, `$updatedField`_, and `$deletedField`_.
170170

171171
$dateFormat
172172
^^^^^^^^^^^
173173

174-
This value works with ``$useTimestamps`` and ``$useSoftDeletes`` to ensure that the correct type of
174+
This value works with `$useTimestamps`_ and `$useSoftDeletes`_ to ensure that the correct type of
175175
date value gets inserted into the database. By default, this creates DATETIME values, but
176-
valid options are: ``'datetime'``, ``'date'``, or ``'int'`` (a PHP timestamp). Using **useSoftDeletes** or
177-
**useTimestamps** with an invalid or missing **dateFormat** will cause an exception.
176+
valid options are: ``'datetime'``, ``'date'``, or ``'int'`` (a PHP timestamp). Using `$useSoftDeletes`_ or
177+
`$useTimestamps`_ with an invalid or missing `$dateFormat`_ will cause an exception.
178178

179179
$createdField
180180
^^^^^^^^^^^^^
181181

182182
Specifies which database field to use for data record create timestamp.
183-
Leave it empty (``''``) to avoid updating it (even if ``$useTimestamps`` is enabled).
183+
Set to an empty string (``''``) to avoid updating it (even if `$useTimestamps`_ is enabled).
184184

185185
$updatedField
186186
^^^^^^^^^^^^^
187187

188188
Specifies which database field should use for keep data record update timestamp.
189-
Leave it empty (``''``) to avoid updating it (even ``$useTimestamps`` is enabled).
189+
Set to an empty string (``''``) to avoid updating it (even `$useTimestamps`_ is enabled).
190190

191191
$deletedField
192192
^^^^^^^^^^^^^
@@ -238,24 +238,16 @@ Callbacks
238238
$allowCallbacks
239239
^^^^^^^^^^^^^^^
240240

241-
Whether the callbacks defined below should be used.
241+
Whether the callbacks defined below should be used. See :ref:`model-events`.
242242

243243
$beforeInsert
244244
^^^^^^^^^^^^^
245245
$afterInsert
246246
^^^^^^^^^^^^
247-
$beforeInsertBatch
248-
^^^^^^^^^^^^^^^^^^
249-
$afterInsertBatch
250-
^^^^^^^^^^^^^^^^^
251247
$beforeUpdate
252248
^^^^^^^^^^^^^
253249
$afterUpdate
254250
^^^^^^^^^^^^^
255-
$beforeUpdateBatch
256-
^^^^^^^^^^^^^^^^^^
257-
$afterUpdateBatch
258-
^^^^^^^^^^^^^^^^^
259251
$beforeFind
260252
^^^^^^^^^^^
261253
$afterFind
@@ -264,9 +256,17 @@ $beforeDelete
264256
^^^^^^^^^^^^^
265257
$afterDelete
266258
^^^^^^^^^^^^
259+
$beforeInsertBatch
260+
^^^^^^^^^^^^^^^^^^
261+
$afterInsertBatch
262+
^^^^^^^^^^^^^^^^^
263+
$beforeUpdateBatch
264+
^^^^^^^^^^^^^^^^^^
265+
$afterUpdateBatch
266+
^^^^^^^^^^^^^^^^^
267267

268268
These arrays allow you to specify callback methods that will be run on the data at the
269-
time specified in the property name.
269+
time specified in the property name. See :ref:`model-events`.
270270

271271
Working with Data
272272
*****************
@@ -284,7 +284,7 @@ Returns a single row where the primary key matches the value passed in as the fi
284284

285285
.. literalinclude:: model/006.php
286286

287-
The value is returned in the format specified in ``$returnType``.
287+
The value is returned in the format specified in `$returnType`_.
288288

289289
You can specify more than one row to return by passing an array of primaryKey values instead
290290
of just one:
@@ -329,7 +329,7 @@ Returns the first row in the result set. This is best used in combination with t
329329
withDeleted()
330330
-------------
331331

332-
If ``$useSoftDeletes`` is true, then the **find*()** methods will not return any rows where ``deleted_at IS NOT NULL``.
332+
If `$useSoftDeletes`_ is true, then the **find*()** methods will not return any rows where ``deleted_at IS NOT NULL``.
333333
To temporarily override this, you can use the ``withDeleted()`` method prior to calling the **find*()** method.
334334

335335
.. literalinclude:: model/013.php
@@ -351,7 +351,7 @@ insert()
351351
The first parameter is an associative array of data to create a new row of data in the database.
352352
If an object is passed instead of an array, it will attempt to convert it to an array.
353353

354-
The array's keys must match the name of the columns in the ``$table``, while the array's values are the values to save for that key.
354+
The array's keys must match the name of the columns in the `$table`_, while the array's values are the values to save for that key.
355355

356356
The optional second parameter is of type boolean, and if it is set to false, the method will return a boolean value,
357357
which indicates the success or failure of the query.
@@ -376,15 +376,15 @@ You can enable the check again by calling ``allowEmptyInserts(false)``.
376376
update()
377377
--------
378378

379-
Updates an existing record in the database. The first parameter is the ``$primaryKey`` of the record to update.
379+
Updates an existing record in the database. The first parameter is the `$primaryKey`_ of the record to update.
380380
An associative array of data is passed into this method as the second parameter. The array's keys must match the name
381-
of the columns in a ``$table``, while the array's values are the values to save for that key:
381+
of the columns in a `$table`_, while the array's values are the values to save for that key:
382382

383383
.. literalinclude:: model/016.php
384384

385385
.. important:: Since v4.3.0, this method raises a ``DatabaseException``
386386
if it generates an SQL statement without a WHERE clause.
387-
In previous versions, if it is called without ``$primaryKey`` specified and
387+
In previous versions, if it is called without `$primaryKey`_ specified and
388388
an SQL statement was generated without a WHERE clause, the query would still
389389
execute and all records in the table would be updated.
390390

@@ -440,7 +440,7 @@ Takes a primary key value as the first parameter and deletes the matching record
440440

441441
.. literalinclude:: model/023.php
442442

443-
If the model's ``$useSoftDeletes`` value is true, this will update the row to set ``deleted_at`` to the current
443+
If the model's `$useSoftDeletes`_ value is true, this will update the row to set ``deleted_at`` to the current
444444
date and time. You can force a permanent delete by setting the second parameter as true.
445445

446446
An array of primary keys can be passed in as the first parameter to delete multiple records at once:
@@ -481,13 +481,13 @@ prior to saving to the database with the ``insert()``, ``update()``, or ``save()
481481
Setting Validation Rules
482482
------------------------
483483

484-
The first step is to fill out the ``$validationRules`` class property with the fields and rules that should
485-
be applied. If you have custom error message that you want to use, place them in the ``$validationMessages`` array:
484+
The first step is to fill out the `$validationRules`_ class property with the fields and rules that should
485+
be applied. If you have custom error message that you want to use, place them in the `$validationMessages`_ array:
486486

487487
.. literalinclude:: model/027.php
488488

489489
If you'd rather organize your rules and error messages within the Validation configuration file, you can do that
490-
and simply set ``$validationRules`` to the name of the validation rule group you created:
490+
and simply set `$validationRules`_ to the name of the validation rule group you created:
491491

492492
.. literalinclude:: model/034.php
493493

@@ -614,7 +614,7 @@ Protecting Fields
614614
=================
615615

616616
To help protect against Mass Assignment Attacks, the Model class **requires** that you list all of the field names
617-
that can be changed during inserts and updates in the ``$allowedFields`` class property. Any data provided
617+
that can be changed during inserts and updates in the `$allowedFields`_ class property. Any data provided
618618
in addition to these will be removed prior to hitting the database. This is great for ensuring that timestamps,
619619
or primary keys do not get changed.
620620

@@ -629,7 +629,7 @@ Runtime Return Type Changes
629629
===========================
630630

631631
You can specify the format that data should be returned as when using the **find*()** methods as the class property,
632-
``$returnType``. There may be times that you would like the data back in a different format, though. The Model
632+
`$returnType`_. There may be times that you would like the data back in a different format, though. The Model
633633
provides methods that allow you to do just that.
634634

635635
.. note:: These methods only change the return type for the next **find*()** method call. After that,
@@ -674,7 +674,7 @@ You can get access to the **shared** instance of the Query Builder any time you
674674

675675
.. literalinclude:: model/043.php
676676

677-
This builder is already set up with the model's ``$table``.
677+
This builder is already set up with the model's `$table`_.
678678

679679
.. note:: Once you get the Query Builder instance, you can call methods of the
680680
:doc:`Query Builder <../database/query_builder>`.
@@ -714,14 +714,23 @@ and specify the model's method at the end of the method chaining.
714714

715715
.. literalinclude:: model/046.php
716716

717+
.. _model-events:
718+
717719
Model Events
718720
************
719721

720722
There are several points within the model's execution that you can specify multiple callback methods to run.
721-
These methods can be used to normalize data, hash passwords, save related entities, and much more. The following
722-
points in the model's execution can be affected, each through a class property: ``$beforeInsert``, ``$afterInsert``,
723-
``$beforeInsertBatch``, ``$afterInsertBatch``, ``$beforeUpdate``, ``$afterUpdate``, ``$beforeUpdateBatch``,
724-
``$afterUpdateBatch``, ``$afterFind``, and ``$afterDelete``.
723+
These methods can be used to normalize data, hash passwords, save related entities, and much more.
724+
725+
The following
726+
points in the model's execution can be affected, each through a class property:
727+
728+
- `$beforeInsert`_, `$afterInsert`_
729+
- `$beforeUpdate`_, `$afterUpdate`_
730+
- `$beforeFind`_, `$afterFind`_
731+
- `$beforeDelete`_, `$afterDelete`_
732+
- `$beforeInsertBatch`_, `$afterInsertBatch`_
733+
- `$beforeUpdateBatch`_, `$afterUpdateBatch`_
725734

726735
.. note:: ``$beforeInsertBatch``, ``$afterInsertBatch``, ``$beforeUpdateBatch`` and
727736
``$afterUpdateBatch`` can be used since v4.3.0.
@@ -741,13 +750,13 @@ must return the original $data array so other callbacks have the full informatio
741750
Specifying Callbacks To Run
742751
===========================
743752

744-
You specify when to run the callbacks by adding the method name to the appropriate class property (``$beforeInsert``, ``$afterUpdate``,
753+
You specify when to run the callbacks by adding the method name to the appropriate class property (`$beforeInsert`_, `$afterUpdate`_,
745754
etc). Multiple callbacks can be added to a single event and they will be processed one after the other. You can
746755
use the same callback in multiple events:
747756

748757
.. literalinclude:: model/051.php
749758

750-
Additionally, each model may allow (default) or deny callbacks class-wide by setting its ``$allowCallbacks`` property:
759+
Additionally, each model may allow (default) or deny callbacks class-wide by setting its `$allowCallbacks`_ property:
751760

752761
.. literalinclude:: model/052.php
753762

@@ -769,20 +778,12 @@ beforeInsert **data** = the key/value pairs that are being inserted. If an
769778
afterInsert **id** = the primary key of the new row, or 0 on failure.
770779
**data** = the key/value pairs being inserted.
771780
**result** = the results of the insert() method used through the Query Builder.
772-
beforeInsertBatch **data** = associative array of values that are being inserted. If an object or Entity class is passed to the
773-
insertBatch method, it is first converted to an array.
774-
afterInsertBatch **data** = the associative array of values being inserted.
775-
**result** = the results of the insertbatch() method used through the Query Builder.
776781
beforeUpdate **id** = the array of primary keys of the rows being updated.
777782
**data** = the key/value pairs that are being updated. If an object or Entity class is passed to the
778783
update method, it is first converted to an array.
779784
afterUpdate **id** = the array of primary keys of the rows being updated.
780785
**data** = the key/value pairs being updated.
781786
**result** = the results of the update() method used through the Query Builder.
782-
beforeUpdateBatch **data** = associative array of values that are being updated. If an object or Entity class is passed to the
783-
updateBatch method, it is first converted to an array.
784-
afterUpdateBatch **data** = the key/value pairs being updated.
785-
**result** = the results of the updateBatch() method used through the Query Builder.
786787
beforeFind The name of the calling **method**, whether a **singleton** was requested, and these additional fields:
787788
- first() No additional fields
788789
- find() **id** = the primary key of the row being searched for.
@@ -796,6 +797,14 @@ afterDelete **id** = primary key of row being deleted.
796797
**purge** = boolean whether soft-delete rows should be hard deleted.
797798
**result** = the result of the delete() call on the Query Builder.
798799
**data** = unused.
800+
beforeInsertBatch **data** = associative array of values that are being inserted. If an object or Entity class is passed to the
801+
insertBatch method, it is first converted to an array.
802+
afterInsertBatch **data** = the associative array of values being inserted.
803+
**result** = the results of the insertbatch() method used through the Query Builder.
804+
beforeUpdateBatch **data** = associative array of values that are being updated. If an object or Entity class is passed to the
805+
updateBatch method, it is first converted to an array.
806+
afterUpdateBatch **data** = the key/value pairs being updated.
807+
**result** = the results of the updateBatch() method used through the Query Builder.
799808
================= =========================================================================================================
800809

801810
Modifying Find* Data

0 commit comments

Comments
 (0)