@@ -7,7 +7,7 @@ Using CodeIgniter's Model
77 :depth: 2
88
99Models
10- ======
10+ ******
1111
1212The CodeIgniter's Model provides convenience features and additional functionality
1313that people commonly use to make working with a **single table ** in your database more convenient.
@@ -17,7 +17,7 @@ methods for much of the standard ways you would need to interact with a database
1717updating records, deleting records, and more.
1818
1919Accessing Models
20- ================
20+ ****************
2121
2222Models are typically stored in the ``app/Models `` directory. They should have a namespace that matches their
2323location within the directory, like ``namespace App\Models ``.
@@ -27,7 +27,7 @@ You can access models within your classes by creating a new instance or using th
2727.. literalinclude :: model/001.php
2828
2929CodeIgniter's Model
30- ===================
30+ *******************
3131
3232CodeIgniter does provide a model class that provides a few nice features, including:
3333
@@ -41,7 +41,7 @@ This class provides a solid base from which to build your own models, allowing y
4141rapidly build out your application's model layer.
4242
4343Creating Your Model
44- ===================
44+ *******************
4545
4646To take advantage of CodeIgniter's model, you would simply create a new model class
4747that extends ``CodeIgniter\Model ``:
@@ -58,7 +58,7 @@ extra steps without repeating the constructor parameters, for example extending
5858.. literalinclude :: model/003.php
5959
6060Connecting to the Database
61- --------------------------
61+ ==========================
6262
6363When the class is first instantiated, if no database connection instance is passed to the constructor,
6464it will automatically connect to the default database group, as set in the configuration. You can
@@ -72,7 +72,7 @@ You would replace "group_name" with the name of a defined database group from th
7272configuration file.
7373
7474Configuring Your Model
75- ----------------------
75+ ======================
7676
7777The model class has a few configuration options that can be set to allow the class' methods
7878to work seamlessly for you. The first two are used by all of the CRUD methods to determine
@@ -190,10 +190,10 @@ time specified in the property name.
190190Whether the callbacks defined above should be used.
191191
192192Working With Data
193- =================
193+ *****************
194194
195195Finding Data
196- ------------
196+ ============
197197
198198Several functions are provided for doing basic CRUD work on your tables, including ``find() ``,
199199``insert() ``, ``update() ``, ``delete() `` and more.
@@ -258,7 +258,7 @@ the next **find*()** methods to return only soft deleted rows:
258258.. literalinclude :: model/014.php
259259
260260Saving Data
261- -----------
261+ ===========
262262
263263**insert() **
264264
@@ -316,7 +316,7 @@ model's ``save()`` method to inspect the class, grab any public and private prop
316316 that provides several handy features that make developing Entities simpler.
317317
318318Deleting Data
319- -------------
319+ =============
320320
321321**delete() **
322322
@@ -343,7 +343,7 @@ Cleans out the database table by permanently removing all rows that have 'delete
343343.. literalinclude :: model/026.php
344344
345345Validating Data
346- ---------------
346+ ===============
347347
348348For many people, validating data in the model is the preferred way to ensure the data is kept to a single
349349standard, without duplicating code. The Model class provides a way to automatically have all data validated
@@ -416,7 +416,7 @@ and simply set ``$validationRules`` to the name of the validation rule group you
416416.. literalinclude :: model/034.php
417417
418418Retrieving Validation Rules
419- ---------------------------
419+ ===========================
420420
421421You can retrieve a model's validation rules by accessing its ``validationRules ``
422422property:
@@ -435,7 +435,7 @@ value an array of fieldnames of interest:
435435.. literalinclude :: model/037.php
436436
437437Validation Placeholders
438- -----------------------
438+ =======================
439439
440440The model provides a simple method to replace parts of your rules based on data that's being passed into it. This
441441sounds fairly obscure but can be especially handy with the ``is_unique `` validation rule. Placeholders are simply
@@ -459,7 +459,7 @@ This can also be used to create more dynamic rules at runtime, as long as you ta
459459keys passed in don't conflict with your form data.
460460
461461Protecting Fields
462- -----------------
462+ =================
463463
464464To help protect against Mass Assignment Attacks, the Model class **requires ** that you list all of the field names
465465that can be changed during inserts and updates in the ``$allowedFields `` class property. Any data provided
@@ -474,7 +474,7 @@ testing, migrations, or seeds. In these cases, you can turn the protection on or
474474.. literalinclude :: model/042.php
475475
476476Working With Query Builder
477- --------------------------
477+ ==========================
478478
479479You can get access to a shared instance of the Query Builder for that model's database connection any time you
480480need it:
@@ -501,7 +501,7 @@ very elegant use:
501501 .. literalinclude :: model/046.php
502502
503503Runtime Return Type Changes
504- ----------------------------
504+ ===========================
505505
506506You can specify the format that data should be returned as when using the **find*() ** methods as the class property,
507507``$returnType ``. There may be times that you would like the data back in a different format, though. The Model
@@ -523,7 +523,7 @@ Returns data from the next **find*()** method as standard objects or custom clas
523523.. literalinclude :: model/048.php
524524
525525Processing Large Amounts of Data
526- --------------------------------
526+ ================================
527527
528528Sometimes, you need to process large amounts of data and would run the risk of running out of memory.
529529To make this simpler, you may use the chunk() method to get smaller chunks of data that you can then
@@ -535,15 +535,15 @@ This is best used during cronjobs, data exports, or other large tasks.
535535.. literalinclude :: model/049.php
536536
537537Model Events
538- ============
538+ ************
539539
540540There are several points within the model's execution that you can specify multiple callback methods to run.
541541These methods can be used to normalize data, hash passwords, save related entities, and much more. The following
542542points in the model's execution can be affected, each through a class property: ``$beforeInsert ``, ``$afterInsert ``,
543543``$beforeUpdate ``, ``$afterUpdate ``, ``$afterFind ``, and ``$afterDelete ``.
544544
545545Defining Callbacks
546- ------------------
546+ ==================
547547
548548You specify the callbacks by first creating a new class method in your model to use. This class will always
549549receive a ``$data `` array as its only parameter. The exact contents of the ``$data `` array will vary between events, but
@@ -555,7 +555,7 @@ must return the original $data array so other callbacks have the full informatio
555555.. literalinclude :: model/050.php
556556
557557Specifying Callbacks To Run
558- ---------------------------
558+ ===========================
559559
560560You specify when to run the callbacks by adding the method name to the appropriate class property (``$beforeInsert ``, ``$afterUpdate ``,
561561etc). Multiple callbacks can be added to a single event and they will be processed one after the other. You can
@@ -572,7 +572,7 @@ You may also change this setting temporarily for a single model call sing the ``
572572.. literalinclude :: model/053.php
573573
574574Event Parameters
575- ----------------
575+ ================
576576
577577Since the exact data passed to each callback varies a bit, here are the details on what is in the ``$data `` parameter
578578passed to each event:
@@ -607,7 +607,7 @@ afterDelete **id** = primary key of row being deleted.
607607================ =========================================================================================================
608608
609609Modifying Find* Data
610- --------------------
610+ ====================
611611
612612The ``beforeFind `` and ``afterFind `` methods can both return a modified set of data to override the normal response
613613from the model. For ``afterFind `` any changes made to ``data `` in the return array will automatically be passed back
@@ -617,7 +617,7 @@ boolean, ``returnData``:
617617.. literalinclude :: model/054.php
618618
619619Manual Model Creation
620- =====================
620+ *********************
621621
622622You do not need to extend any special class to create a model for your application. All you need is to get an
623623instance of the database connection and you're good to go. This allows you to bypass the features CodeIgniter's
0 commit comments