| Fully Qualified Name: | CodeIgniter\Model |
Class Model
The Model class provides a number of convenient features that makes working with a database table less painful.
It will:
- automatically connect to database
- allow intermingling calls between db connection, the builder,
and methods in this class.
- simplifies pagination
- removes the need to use Result object directly in most cases
- allow specifying the return type (array, object, etc) with each call
- ensure validation is run against objects when saving items
| Name | Description | Defined By |
|---|---|---|
| __call() | Provides direct access to method in the builder (if available) and the database connection. | Model |
| __construct() | Model constructor. | Model |
| __get() | Provides/instantiates the builder/db connection and model's table/primary key names and return type. | Model |
| __isset() | Checks for the existence of properties across this model, builder, and db connection. | Model |
| asArray() | Sets the return type of the results to be as an associative array. | Model |
| asObject() | Sets the return type to be of the specified type of object. | Model |
| chunk() | Loops over records in batches, allowing you to operate on them. | Model |
| classToArray() | Takes a class an returns an array of it's public and protected properties as an array suitable for use in creates and updates. | Model |
| cleanRules() | Should validation rules be removed before saving? Most handy when doing updates. | Model |
| countAllResults() | Override countAllResults to account for soft deleted accounts. | Model |
| delete() | Deletes a single record from $this->table where $id matches the table's primaryKey | Model |
| errors() | Grabs the last error(s) that occurred. If data was validated, it will first check for errors there, otherwise will try to grab the last error from the Database connection. | Model |
| find() | Fetches the row of database from $this->table with a primary key matching $id. | Model |
| findAll() | Works with the current Query Builder instance to return all results, while optionally limiting them. | Model |
| findColumn() | Fetches the column of database from $this->table | Model |
| first() | Returns the first row of the result set. Will take any previous Query Builder calls into account when determining the result set. | Model |
| getInsertID() | Returns last insert ID or 0. | Model |
| getValidationMessages() | Returns the model's define validation messages so they can be used elsewhere, if needed. | Model |
| getValidationRules() | Returns the model's defined validation rules so that they can be used elsewhere, if needed. | Model |
| insert() | Inserts data into the current table. If an object is provided, it will attempt to convert it to an array. | Model |
| insertBatch() | Compiles batch insert strings and runs the queries, validating each row prior. | Model |
| onlyDeleted() | Works with the find* methods to return only the rows that have been deleted. | Model |
| paginate() | Works with $this->builder to get the Compiled Select to operate on. | Model |
| protect() | Sets whether or not we should whitelist data set during updates or inserts against $this->availableFields. | Model |
| purgeDeleted() | Permanently deletes all rows that have been marked as deleted through soft deletes (deleted = 1) | Model |
| replace() | Replace | Model |
| save() | A convenience method that will attempt to determine whether the data should be inserted or updated. Will work with either an array or object. When using with custom class objects, you must ensure that the class will provide access to the class variables, even if through a magic method. | Model |
| set() | Captures the builder's set() method so that we can validate the data here. This allows it to be used with any of the other builder methods and still get validated data, like replace. | Model |
| setTable() | Specify the table associated with a model | Model |
| setValidationMessage() | Allows to set field wise validation message. | Model |
| setValidationMessages() | Allows to set validation messages. | Model |
| skipValidation() | Set the value of the skipValidation flag. | Model |
| update() | Updates a single record in $this->table. If an object is provided, it will attempt to convert it into an array. | Model |
| updateBatch() | Update_Batch | Model |
| validate() | Validate the data against the validation rules (or the validation group) specified in the class property, $validationRules. | Model |
| withDeleted() | Sets $useSoftDeletes value so that we can temporarily override the softdeletes settings. Can be used for all find* methods. | Model |
Provides direct access to method in the builder (if available) and the database connection.
| Parameter Name | Type | Description |
|---|---|---|
| $name | string | |
| $params | array |
Returns: \Model|null
Model constructor.
| Parameter Name | Type | Description |
|---|---|---|
| $db | \ConnectionInterface | |
| $validation | \ValidationInterface |
Returns:
Provides/instantiates the builder/db connection and model's table/primary key names and return type.
| Parameter Name | Type | Description |
|---|---|---|
| $name | string |
Returns: mixed
Checks for the existence of properties across this model, builder, and db connection.
| Parameter Name | Type | Description |
|---|---|---|
| $name | string |
Returns: bool
Sets the return type of the results to be as an associative array.
Returns: \Model
Sets the return type to be of the specified type of object.
Defaults to a simple object, but can be any class that has class vars with the same name as the table columns, or at least allows them to be created.
| Parameter Name | Type | Description |
|---|---|---|
| $class | string |
Returns: \Model
Loops over records in batches, allowing you to operate on them.
Works with $this->builder to get the Compiled select to determine the rows to operate on.
| Parameter Name | Type | Description |
|---|---|---|
| $size | int | |
| $userFunc | \Closure |
Returns:
Takes a class an returns an array of it's public and protected properties as an array suitable for use in creates and updates.
| Parameter Name | Type | Description |
|---|---|---|
| $data | string|object | |
| $primaryKey | string|null | |
| $dateFormat | string | |
| $onlyChanged | bool |
Returns: array
Should validation rules be removed before saving? Most handy when doing updates.
| Parameter Name | Type | Description |
|---|---|---|
| $choice | bool |
Returns: $this
Override countAllResults to account for soft deleted accounts.
| Parameter Name | Type | Description |
|---|---|---|
| $reset | bool | |
| $test | bool |
Returns: mixed
Deletes a single record from $this->table where $id matches the table's primaryKey
| Parameter Name | Type | Description |
|---|---|---|
| $id | int|string|array|null | The |
| $purge | bool | Allows |
Returns: mixed
Grabs the last error(s) that occurred. If data was validated, it will first check for errors there, otherwise will try to grab the last error from the Database connection.
| Parameter Name | Type | Description |
|---|---|---|
| $forceDB | bool | Always |
Returns: array|null
Fetches the row of database from $this->table with a primary key matching $id.
| Parameter Name | Type | Description |
|---|---|---|
| $id | mixed|array|null | One |
Returns: array|object|null The resulting row of data, or null.
Works with the current Query Builder instance to return all results, while optionally limiting them.
| Parameter Name | Type | Description |
|---|---|---|
| $limit | int | |
| $offset | int |
Returns: array
Fetches the column of database from $this->table
| Parameter Name | Type | Description |
|---|---|---|
| $columnName | string |
Returns: array|null The resulting row of data, or null if no data found.
Returns the first row of the result set. Will take any previous Query Builder calls into account when determining the result set.
Returns: array|object|null
Returns last insert ID or 0.
Returns: int
Returns the model's define validation messages so they can be used elsewhere, if needed.
Returns: array
Returns the model's defined validation rules so that they can be used elsewhere, if needed.
| Parameter Name | Type | Description |
|---|---|---|
| $options | array |
Returns: array
Inserts data into the current table. If an object is provided, it will attempt to convert it to an array.
| Parameter Name | Type | Description |
|---|---|---|
| $data | array|object | |
| $returnID | bool | Whether |
Returns: int|string|bool
Compiles batch insert strings and runs the queries, validating each row prior.
| Parameter Name | Type | Description |
|---|---|---|
| $set | array | An |
| $escape | bool | Whether |
| $batchSize | int | |
| $testing | bool |
Returns: int|bool Number of rows inserted or FALSE on failure
Works with the find* methods to return only the rows that have been deleted.
Returns: \Model
Works with $this->builder to get the Compiled Select to operate on.
Expects a GET variable (?page=2) that specifies the page of results to display.
| Parameter Name | Type | Description |
|---|---|---|
| $perPage | int | |
| $group | string | Will |
| $page | int | Optional |
| $segment | int | Optional |
Returns: array|null
Sets whether or not we should whitelist data set during updates or inserts against $this->availableFields.
| Parameter Name | Type | Description |
|---|---|---|
| $protect | bool |
Returns: \Model
Permanently deletes all rows that have been marked as deleted through soft deletes (deleted = 1)
Returns: bool|mixed
Replace
Compiles an replace into string and runs the query
| Parameter Name | Type | Description |
|---|---|---|
| $data | null | |
| $returnSQL | bool |
Returns: mixed
A convenience method that will attempt to determine whether the data should be inserted or updated. Will work with either an array or object. When using with custom class objects, you must ensure that the class will provide access to the class variables, even if through a magic method.
| Parameter Name | Type | Description |
|---|---|---|
| $data | array|object |
Returns: bool
Captures the builder's set() method so that we can validate the data here. This allows it to be used with any of the other builder methods and still get validated data, like replace.
| Parameter Name | Type | Description |
|---|---|---|
| $key | mixed | Field |
| $value | string | Field |
| $escape | bool | Whether |
Returns: $this
Specify the table associated with a model
| Parameter Name | Type | Description |
|---|---|---|
| $table | string |
Returns: \Model
Allows to set field wise validation message.
It could be used when you have to change default or override current validate messages.
| Parameter Name | Type | Description |
|---|---|---|
| $field | string | |
| $fieldMessages | array |
Returns: void
Allows to set validation messages.
It could be used when you have to change default or override current validate messages.
| Parameter Name | Type | Description |
|---|---|---|
| $validationMessages | array |
Returns: void
Set the value of the skipValidation flag.
| Parameter Name | Type | Description |
|---|---|---|
| $skip | bool |
Returns: \Model
Updates a single record in $this->table. If an object is provided, it will attempt to convert it into an array.
| Parameter Name | Type | Description |
|---|---|---|
| $id | int|array|string | |
| $data | array|object |
Returns: bool
Update_Batch
Compiles an update string and runs the query
| Parameter Name | Type | Description |
|---|---|---|
| $set | array | An |
| $index | string | The |
| $batchSize | int | The |
| $returnSQL | bool | True |
Returns: mixed Number of rows affected or FALSE on failure
Validate the data against the validation rules (or the validation group) specified in the class property, $validationRules.
| Parameter Name | Type | Description |
|---|---|---|
| $data | array|object |
Returns: bool
Sets $useSoftDeletes value so that we can temporarily override the softdeletes settings. Can be used for all find* methods.
| Parameter Name | Type | Description |
|---|---|---|
| $val | bool |
Returns: \Model