$pager
$pager : \CodeIgniter\Pager\Pager
Pager instance.
Populated after calling $this->paginate()
Class Model
The Model class provides a number of convenient features that makes working with a database table less painful.
It will:
$db : \CodeIgniter\Database\ConnectionInterface
Database Connection
$validation : \CodeIgniter\Validation\Validation
Our validator instance.
__construct(\CodeIgniter\Database\ConnectionInterface $db = null, \CodeIgniter\Validation\ValidationInterface $validation = null)
Model constructor.
\CodeIgniter\Database\ConnectionInterface | $db | |
\CodeIgniter\Validation\ValidationInterface | $validation |
set(mixed $key, string $value = '', boolean|null $escape = null) : $this
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.
mixed | $key | |
string | $value | |
boolean|null | $escape |
save(array|object $data) : boolean
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.
array|object | $data |
classToArray(string|object $data, string|null $primaryKey = null, string $dateFormat = 'datetime', boolean $onlyChanged = true) : array
Takes a class an returns an array of it's public and protected properties as an array suitable for use in creates and updates.
string|object | $data | |
string|null | $primaryKey | |
string | $dateFormat | |
boolean | $onlyChanged |
insert(array|object $data = null, boolean $returnID = true) : integer|string|boolean
Inserts data into the current table. If an object is provided, it will attempt to convert it to an array.
array|object | $data | |
boolean | $returnID | Whether insert ID should be returned or not. |
insertBatch(array $set = null, boolean $escape = null, integer $batchSize = 100, boolean $testing = false) : integer|boolean
Compiles batch insert strings and runs the queries, validating each row prior.
array | $set | An associative array of insert values |
boolean | $escape | Whether to escape values and identifiers |
integer | $batchSize | |
boolean | $testing |
Number of rows inserted or FALSE on failure
updateBatch(array $set = null, string $index = null, integer $batchSize = 100, boolean $returnSQL = false) : mixed
Update_Batch
Compiles an update string and runs the query
array | $set | An associative array of update values |
string | $index | The where key |
integer | $batchSize | The size of the batch to run |
boolean | $returnSQL | True means SQL is returned, false will execute the query |
Number of rows affected or FALSE on failure
delete(integer|array|null $id = null, boolean $purge = false) : mixed
Deletes a single record from $this->table where $id matches the table's primaryKey
integer|array|null | $id | The rows primary key(s) |
boolean | $purge | Allows overriding the soft deletes setting. |
withDeleted(boolean $val = true) : \CodeIgniter\Model
Sets $useSoftDeletes value so that we can temporarily override the softdeletes settings. Can be used for all find* methods.
boolean | $val |
onlyDeleted() : \CodeIgniter\Model
Works with the find* methods to return only the rows that have been deleted.
asArray() : \CodeIgniter\Model
Sets the return type of the results to be as an associative array.
asObject(string $class = 'object') : \CodeIgniter\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.
string | $class |
chunk(integer $size, \Closure $userFunc)
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.
integer | $size | |
\Closure | $userFunc |
paginate(integer $perPage = 20, string $group = 'default', integer $page) : array|null
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.
integer | $perPage | |
string | $group | Will be used by the pagination library to identify a unique pagination set. |
integer | $page | Optional page number (useful when the page number is provided in different way) |
protect(boolean $protect = true) : \CodeIgniter\Model
Sets whether or not we should whitelist data set during updates or inserts against $this->availableFields.
boolean | $protect |
setTable(string $table) : \CodeIgniter\Model
Specify the table associated with a model
string | $table |
errors(boolean $forceDB = false) : array|null
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.
boolean | $forceDB | Always grab the db error, not validation |
skipValidation(boolean $skip = true) : \CodeIgniter\Model
Set the value of the skipValidation flag.
boolean | $skip |
__call(string $name, array $params) : \CodeIgniter\Model|null
Provides direct access to method in the builder (if available) and the database connection.
string | $name | |
array | $params |
doProtectFields(array $data) : array
Ensures that only the fields that are allowed to be updated are in the data array.
Used by insert() and update() to protect against mass assignment vulnerabilities.
array | $data |
setDate(integer $userData = null) : mixed
A utility function to allow child models to use the type of date/time format that they prefer. This is primarily used for setting created_at, updated_at and deleted_at values, but can be used by inheriting classes.
The available time formats are:
integer | $userData | An optional PHP timestamp to be converted. |
fillPlaceholders(array $rules, array $data) : array
Replace any placeholders within the rules with the values that match the 'key' of any properties being set. For example, if we had the following $data array:
[ 'id' => 13 ]
and the following rule:
'required|is_unique[users,email,id,{id}]'
The value of {id} would be replaced with the actual id in the form data:
'required|is_unique[users,email,id,13]'
array | $rules | |
array | $data |
trigger(string $event, array $data) : mixed
A simple event trigger for Model Events that allows additional data manipulation within the model. Specifically intended for usage by child models this can be used to format data, save/load related classes, etc.
It is the responsibility of the callback methods to return the data itself.
Each $data array MUST have a 'data' key with the relevant data for callback methods (like an array of key/value pairs to insert or update, an array of results, etc)
string | $event | |
array | $data |