@@ -111,12 +111,12 @@ is used with methods like ``find()`` to know what column to match the specified
111
111
$useAutoIncrement
112
112
-----------------
113
113
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 ``
115
115
then you are responsible for providing primary key value for every record in the table. This
116
116
feature may be handy when we want to implement 1:1 relation or use UUIDs for our model. The
117
117
default value is ``true ``.
118
118
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
120
120
key in the database to ``unique ``. This way you will make sure that all of Model's features
121
121
will still work the same as before.
122
122
@@ -142,8 +142,8 @@ part of a security trail. If true, the **find*()** methods will only return non-
142
142
the ``withDeleted() `` method is called prior to calling the **find*() ** method.
143
143
144
144
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.
147
147
148
148
.. important :: The ``deleted_at`` field must be nullable.
149
149
@@ -155,7 +155,7 @@ This array should be updated with the field names that can be set during ``save(
155
155
against just taking input from a form and throwing it all at the model, resulting in
156
156
potential mass assignment vulnerabilities.
157
157
158
- .. note :: The `` $primaryKey`` field should never be an allowed field.
158
+ .. note :: The `$primaryKey`_ field should never be an allowed field.
159
159
160
160
Dates
161
161
-----
@@ -164,29 +164,29 @@ $useTimestamps
164
164
^^^^^^^^^^^^^^
165
165
166
166
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
168
168
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 `_.
170
170
171
171
$dateFormat
172
172
^^^^^^^^^^^
173
173
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
175
175
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.
178
178
179
179
$createdField
180
180
^^^^^^^^^^^^^
181
181
182
182
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).
184
184
185
185
$updatedField
186
186
^^^^^^^^^^^^^
187
187
188
188
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).
190
190
191
191
$deletedField
192
192
^^^^^^^^^^^^^
@@ -238,24 +238,16 @@ Callbacks
238
238
$allowCallbacks
239
239
^^^^^^^^^^^^^^^
240
240
241
- Whether the callbacks defined below should be used.
241
+ Whether the callbacks defined below should be used. See :ref: ` model-events `.
242
242
243
243
$beforeInsert
244
244
^^^^^^^^^^^^^
245
245
$afterInsert
246
246
^^^^^^^^^^^^
247
- $beforeInsertBatch
248
- ^^^^^^^^^^^^^^^^^^
249
- $afterInsertBatch
250
- ^^^^^^^^^^^^^^^^^
251
247
$beforeUpdate
252
248
^^^^^^^^^^^^^
253
249
$afterUpdate
254
250
^^^^^^^^^^^^^
255
- $beforeUpdateBatch
256
- ^^^^^^^^^^^^^^^^^^
257
- $afterUpdateBatch
258
- ^^^^^^^^^^^^^^^^^
259
251
$beforeFind
260
252
^^^^^^^^^^^
261
253
$afterFind
@@ -264,9 +256,17 @@ $beforeDelete
264
256
^^^^^^^^^^^^^
265
257
$afterDelete
266
258
^^^^^^^^^^^^
259
+ $beforeInsertBatch
260
+ ^^^^^^^^^^^^^^^^^^
261
+ $afterInsertBatch
262
+ ^^^^^^^^^^^^^^^^^
263
+ $beforeUpdateBatch
264
+ ^^^^^^^^^^^^^^^^^^
265
+ $afterUpdateBatch
266
+ ^^^^^^^^^^^^^^^^^
267
267
268
268
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 `.
270
270
271
271
Working with Data
272
272
*****************
@@ -284,7 +284,7 @@ Returns a single row where the primary key matches the value passed in as the fi
284
284
285
285
.. literalinclude :: model/006.php
286
286
287
- The value is returned in the format specified in `` $returnType `` .
287
+ The value is returned in the format specified in `$returnType `_ .
288
288
289
289
You can specify more than one row to return by passing an array of primaryKey values instead
290
290
of just one:
@@ -329,7 +329,7 @@ Returns the first row in the result set. This is best used in combination with t
329
329
withDeleted()
330
330
-------------
331
331
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 ``.
333
333
To temporarily override this, you can use the ``withDeleted() `` method prior to calling the **find*() ** method.
334
334
335
335
.. literalinclude :: model/013.php
@@ -351,7 +351,7 @@ insert()
351
351
The first parameter is an associative array of data to create a new row of data in the database.
352
352
If an object is passed instead of an array, it will attempt to convert it to an array.
353
353
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.
355
355
356
356
The optional second parameter is of type boolean, and if it is set to false, the method will return a boolean value,
357
357
which indicates the success or failure of the query.
@@ -376,15 +376,15 @@ You can enable the check again by calling ``allowEmptyInserts(false)``.
376
376
update()
377
377
--------
378
378
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.
380
380
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:
382
382
383
383
.. literalinclude :: model/016.php
384
384
385
385
.. important :: Since v4.3.0, this method raises a ``DatabaseException``
386
386
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
388
388
an SQL statement was generated without a WHERE clause, the query would still
389
389
execute and all records in the table would be updated.
390
390
@@ -440,7 +440,7 @@ Takes a primary key value as the first parameter and deletes the matching record
440
440
441
441
.. literalinclude :: model/023.php
442
442
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
444
444
date and time. You can force a permanent delete by setting the second parameter as true.
445
445
446
446
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()
481
481
Setting Validation Rules
482
482
------------------------
483
483
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:
486
486
487
487
.. literalinclude :: model/027.php
488
488
489
489
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:
491
491
492
492
.. literalinclude :: model/034.php
493
493
@@ -614,7 +614,7 @@ Protecting Fields
614
614
=================
615
615
616
616
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
618
618
in addition to these will be removed prior to hitting the database. This is great for ensuring that timestamps,
619
619
or primary keys do not get changed.
620
620
@@ -629,7 +629,7 @@ Runtime Return Type Changes
629
629
===========================
630
630
631
631
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
633
633
provides methods that allow you to do just that.
634
634
635
635
.. 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
674
674
675
675
.. literalinclude :: model/043.php
676
676
677
- This builder is already set up with the model's `` $table `` .
677
+ This builder is already set up with the model's `$table `_ .
678
678
679
679
.. note :: Once you get the Query Builder instance, you can call methods of the
680
680
:doc: `Query Builder <../database/query_builder >`.
@@ -714,14 +714,23 @@ and specify the model's method at the end of the method chaining.
714
714
715
715
.. literalinclude :: model/046.php
716
716
717
+ .. _model-events :
718
+
717
719
Model Events
718
720
************
719
721
720
722
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 `_
725
734
726
735
.. note :: ``$beforeInsertBatch``, ``$afterInsertBatch``, ``$beforeUpdateBatch`` and
727
736
``$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
741
750
Specifying Callbacks To Run
742
751
===========================
743
752
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 `_ ,
745
754
etc). Multiple callbacks can be added to a single event and they will be processed one after the other. You can
746
755
use the same callback in multiple events:
747
756
748
757
.. literalinclude :: model/051.php
749
758
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:
751
760
752
761
.. literalinclude :: model/052.php
753
762
@@ -769,20 +778,12 @@ beforeInsert **data** = the key/value pairs that are being inserted. If an
769
778
afterInsert **id ** = the primary key of the new row, or 0 on failure.
770
779
**data ** = the key/value pairs being inserted.
771
780
**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.
776
781
beforeUpdate **id ** = the array of primary keys of the rows being updated.
777
782
**data ** = the key/value pairs that are being updated. If an object or Entity class is passed to the
778
783
update method, it is first converted to an array.
779
784
afterUpdate **id ** = the array of primary keys of the rows being updated.
780
785
**data ** = the key/value pairs being updated.
781
786
**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.
786
787
beforeFind The name of the calling **method **, whether a **singleton ** was requested, and these additional fields:
787
788
- first() No additional fields
788
789
- find() **id ** = the primary key of the row being searched for.
@@ -796,6 +797,14 @@ afterDelete **id** = primary key of row being deleted.
796
797
**purge ** = boolean whether soft-delete rows should be hard deleted.
797
798
**result ** = the result of the delete() call on the Query Builder.
798
799
**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.
799
808
================= =========================================================================================================
800
809
801
810
Modifying Find* Data
0 commit comments