Model
extends BaseModel
in package
Class Model
The Model class extends BaseModel and provides additional convenient features that makes working with a SQL database table less painful.
It will: - automatically connect to database - allow intermingling calls to the builder - removes the need to use Result object directly in most cases
Tags
Table of Contents
- $db : BaseConnection
- Database Connection
- $pager : Pager
- Pager instance.
- $afterDelete : array<string|int, mixed>
- Callbacks for afterDelete
- $afterFind : array<string|int, mixed>
- Callbacks for afterFind
- $afterInsert : array<string|int, mixed>
- Callbacks for afterInsert
- $afterUpdate : array<string|int, mixed>
- Callbacks for afterUpdate
- $allowCallbacks : bool
- Whether to trigger the defined callbacks
- $allowedFields : array<string|int, mixed>
- An array of field names that are allowed to be set by the user in inserts/updates.
- $beforeDelete : array<string|int, mixed>
- Callbacks for beforeDelete
- $beforeFind : array<string|int, mixed>
- Callbacks for beforeFind
- $beforeInsert : array<string|int, mixed>
- Callbacks for beforeInsert
- $beforeUpdate : array<string|int, mixed>
- Callbacks for beforeUpdate
- $builder : BaseBuilder|null
- Query Builder object
- $cleanValidationRules : bool
- Whether rules should be removed that do not exist in the passed in data. Used between inserts/updates.
- $createdField : string
- The column used for insert timestamps
- $dateFormat : string
- The type of column that created_at and updated_at are expected to.
- $db : BaseConnection
- Database Connection
- $DBGroup : string
- The Database connection group that should be instantiated.
- $deletedField : string
- The column used to save soft delete state
- $escape : array<string|int, mixed>
- Escape array that maps usage of escape flag for every parameter.
- $insertID : int|string
- Last insert ID
- $primaryKey : string
- The table's primary key.
- $protectFields : bool
- Whether we should limit fields in inserts and updates to those available in $allowedFields or not.
- $returnType : string
- The format that the results should be returned as.
- $skipValidation : bool
- Skip the model's validation. Used in conjunction with skipValidation() to skip data validation for any future calls.
- $table : string
- Name of database table
- $tempAllowCallbacks : bool
- Used by allowCallbacks() to override the model's allowCallbacks setting.
- $tempData : array<string|int, mixed>
- Holds information passed in via 'set' so that we can capture it (not the builder) and ensure it gets validated first.
- $tempReturnType : string
- Used by asArray and asObject to provide temporary overrides of model default.
- $tempUseSoftDeletes : bool
- Used by withDeleted to override the model's softDelete setting.
- $updatedField : string
- The column used for update timestamps
- $useAutoIncrement : bool
- Whether primary key uses auto increment.
- $useSoftDeletes : bool
- If this model should use "softDeletes" and simply set a date when rows are deleted, or do hard deletes.
- $useTimestamps : bool
- If true, will set created_at, and updated_at values during insert and update routines.
- $validation : Validation
- Our validator instance.
- $validationMessages : array<string|int, mixed>
- Contains any custom error messages to be used during data validation.
- $validationRules : array<string|int, mixed>|string
- Rules used to validate data in insert, update, and save methods.
- __call() : $this|null
- Provides direct access to method in the builder (if available) and the database connection.
- __construct() : mixed
- Model constructor.
- __get() : mixed
- Provides/instantiates the builder/db connection and model's table/primary key names and return type.
- __isset() : bool
- Checks for the existence of properties across this model, builder, and db connection.
- allowCallbacks() : $this
- Sets $tempAllowCallbacks value so that we can temporarily override the setting. Resets after the next method that uses triggers.
- asArray() : $this
- Sets the return type of the results to be as an associative array.
- asObject() : $this
- Sets the return type to be of the specified type of object.
- builder() : BaseBuilder
- Provides a shared instance of the Query Builder.
- chunk() : void
- Loops over records in batches, allowing you to operate on them.
- classToArray() : array<string|int, mixed>
- Takes a class an returns an array of it's public and protected properties as an array suitable for use in creates and updates.
- cleanRules() : $this
- Should validation rules be removed before saving? Most handy when doing updates.
- countAllResults() : mixed
- Override countAllResults to account for soft deleted accounts.
- delete() : BaseResult|bool
- Deletes a single record from the database where $id matches
- errors() : array<string, string>
- 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.
- find() : array<string|int, mixed>|object|null
- Fetches the row of database
- findAll() : array<string|int, mixed>
- Fetches all results, while optionally limiting them.
- findColumn() : array<string|int, mixed>|null
- Fetches the column of database
- first() : array<string|int, mixed>|object|null
- Returns the first row of the result set.
- getIdValue() : int|array<string|int, mixed>|string|null
- Returns the id value for the data array or object
- getInsertID() : int|string
- Returns last insert ID or 0.
- getValidationMessages() : array<string|int, mixed>
- Returns the model's define validation messages so they can be used elsewhere, if needed.
- getValidationRules() : array<string|int, mixed>
- Returns the model's defined validation rules so that they can be used elsewhere, if needed.
- insert() : BaseResult|object|int|string|false
- Inserts data into the database. If an object is provided, it will attempt to convert it to an array.
- insertBatch() : int|bool
- Compiles batch insert runs the queries, validating each row prior.
- onlyDeleted() : $this
- Works with the find* methods to return only the rows that have been deleted.
- paginate() : array<string|int, mixed>|null
- Works with Pager to get the size and offset parameters.
- protect() : $this
- Sets whether or not we should whitelist data set during updates or inserts against $this->availableFields.
- purgeDeleted() : bool|mixed
- Permanently deletes all rows that have been marked as deleted through soft deletes (deleted = 1)
- replace() : mixed
- Compiles a replace and runs the query
- save() : bool
- 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.
- set() : $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.
- setAllowedFields() : $this
- It could be used when you have to change default or override current allowed fields.
- setTable() : $this
- Specify the table associated with a model
- setValidationMessage() : $this
- Allows to set field wise validation message.
- setValidationMessages() : $this
- Allows to set validation messages.
- setValidationRule() : $this
- Allows to set field wise validation rules.
- setValidationRules() : $this
- Allows to set validation rules.
- skipValidation() : $this
- Set the value of the skipValidation flag.
- update() : bool
- Updates a single record in the database. If an object is provided, it will attempt to convert it into an array.
- updateBatch() : mixed
- Compiles an update and runs the query
- validate() : bool
- Validate the data against the validation rules (or the validation group) specified in the class property, $validationRules.
- withDeleted() : $this
- Sets $useSoftDeletes value so that we can temporarily override the soft deletes settings. Can be used for all find* methods.
- cleanValidationRules() : array<string|int, mixed>
- Removes any rules that apply to fields that have not been set currently so that rules don't block updating when only updating a partial row.
- doDelete() : string|bool
- Deletes a single record from $this->table where $id matches the table's primaryKey This methods works only with dbCalls
- doErrors() : array<string, string>
- Grabs the last error(s) that occurred from the Database connection.
- doFind() : array<string|int, mixed>|object|null
- Fetches the row of database from $this->table with a primary key matching $id. This methods works only with dbCalls This methods works only with dbCalls
- doFindAll() : array<string|int, mixed>
- Works with the current Query Builder instance to return all results, while optionally limiting them.
- doFindColumn() : array<string|int, mixed>|null
- Fetches the column of database from $this->table This methods works only with dbCalls
- doFirst() : array<string|int, mixed>|object|null
- Returns the first row of the result set. Will take any previous Query Builder calls into account when determining the result set.
- doInsert() : Query|bool
- Inserts data into the current table.
- doInsertBatch() : int|bool
- Compiles batch insert strings and runs the queries, validating each row prior.
- doOnlyDeleted() : void
- Works with the find* methods to return only the rows that have been deleted.
- doProtectFields() : array<string|int, mixed>
- Ensures that only the fields that are allowed to be updated are in the data array.
- doPurgeDeleted() : bool|mixed
- Permanently deletes all rows that have been marked as deleted through soft deletes (deleted = 1) This methods works only with dbCalls
- doReplace() : mixed
- Compiles a replace into string and runs the query This methods works only with dbCalls
- doUpdate() : bool
- Updates a single record in $this->table.
- doUpdateBatch() : mixed
- Compiles an update string and runs the query This methods works only with dbCalls
- fillPlaceholders() : array<string|int, mixed>
- 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:
- idValue() : int|array<string|int, mixed>|string|null
- Returns the id value for the data array or object
- initialize() : mixed
- Initializes the instance with any additional steps.
- intToDate() : int|string
- 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.
- objectToArray() : array<string|int, mixed>
- Takes a class an returns an array of it's public and protected properties as an array suitable for use in creates and updates.
- objectToRawArray() : array<string|int, mixed>|null
- Takes a class an returns an array of it's public and protected properties as an array with raw values.
- setDate() : mixed
- Sets the date or current date if null value is passed
- shouldUpdate() : bool
- This method is called on save to determine if entry have to be updated If this method return false insert operation will be executed
- timeToDate() : string|int
- Converts Time value to string using $this->dateFormat
- transformDataToArray() : array<string|int, mixed>
- Transform data to array
- trigger() : 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.
Properties
$db
Database Connection
public
BaseConnection
$db
$pager
Pager instance.
public
Pager
$pager
Populated after calling $this->paginate()
$afterDelete
Callbacks for afterDelete
protected
array<string|int, mixed>
$afterDelete
= []
$afterFind
Callbacks for afterFind
protected
array<string|int, mixed>
$afterFind
= []
$afterInsert
Callbacks for afterInsert
protected
array<string|int, mixed>
$afterInsert
= []
$afterUpdate
Callbacks for afterUpdate
protected
array<string|int, mixed>
$afterUpdate
= []
$allowCallbacks
Whether to trigger the defined callbacks
protected
bool
$allowCallbacks
= true
$allowedFields
An array of field names that are allowed to be set by the user in inserts/updates.
protected
array<string|int, mixed>
$allowedFields
= []
$beforeDelete
Callbacks for beforeDelete
protected
array<string|int, mixed>
$beforeDelete
= []
$beforeFind
Callbacks for beforeFind
protected
array<string|int, mixed>
$beforeFind
= []
$beforeInsert
Callbacks for beforeInsert
protected
array<string|int, mixed>
$beforeInsert
= []
$beforeUpdate
Callbacks for beforeUpdate
protected
array<string|int, mixed>
$beforeUpdate
= []
$builder
Query Builder object
protected
BaseBuilder|null
$builder
$cleanValidationRules
Whether rules should be removed that do not exist in the passed in data. Used between inserts/updates.
protected
bool
$cleanValidationRules
= true
$createdField
The column used for insert timestamps
protected
string
$createdField
= 'created_at'
$dateFormat
The type of column that created_at and updated_at are expected to.
protected
string
$dateFormat
= 'datetime'
Allowed: 'datetime', 'date', 'int'
$db
Database Connection
protected
BaseConnection
$db
$DBGroup
The Database connection group that should be instantiated.
protected
string
$DBGroup
$deletedField
The column used to save soft delete state
protected
string
$deletedField
= 'deleted_at'
$escape
Escape array that maps usage of escape flag for every parameter.
protected
array<string|int, mixed>
$escape
= []
$insertID
Last insert ID
protected
int|string
$insertID
= 0
$primaryKey
The table's primary key.
protected
string
$primaryKey
= 'id'
$protectFields
Whether we should limit fields in inserts and updates to those available in $allowedFields or not.
protected
bool
$protectFields
= true
$returnType
The format that the results should be returned as.
protected
string
$returnType
= 'array'
Will be overridden if the as* methods are used.
$skipValidation
Skip the model's validation. Used in conjunction with skipValidation() to skip data validation for any future calls.
protected
bool
$skipValidation
= false
$table
Name of database table
protected
string
$table
$tempAllowCallbacks
Used by allowCallbacks() to override the model's allowCallbacks setting.
protected
bool
$tempAllowCallbacks
$tempData
Holds information passed in via 'set' so that we can capture it (not the builder) and ensure it gets validated first.
protected
array<string|int, mixed>
$tempData
= []
$tempReturnType
Used by asArray and asObject to provide temporary overrides of model default.
protected
string
$tempReturnType
$tempUseSoftDeletes
Used by withDeleted to override the model's softDelete setting.
protected
bool
$tempUseSoftDeletes
$updatedField
The column used for update timestamps
protected
string
$updatedField
= 'updated_at'
$useAutoIncrement
Whether primary key uses auto increment.
protected
bool
$useAutoIncrement
= true
$useSoftDeletes
If this model should use "softDeletes" and simply set a date when rows are deleted, or do hard deletes.
protected
bool
$useSoftDeletes
= false
$useTimestamps
If true, will set created_at, and updated_at values during insert and update routines.
protected
bool
$useTimestamps
= false
$validation
Our validator instance.
protected
Validation
$validation
$validationMessages
Contains any custom error messages to be used during data validation.
protected
array<string|int, mixed>
$validationMessages
= []
$validationRules
Rules used to validate data in insert, update, and save methods.
protected
array<string|int, mixed>|string
$validationRules
= []
The array must match the format of data passed to the Validation library.
Methods
__call()
Provides direct access to method in the builder (if available) and the database connection.
public
__call(string $name, array<string|int, mixed> $params) : $this|null
Parameters
- $name : string
-
Name
- $params : array<string|int, mixed>
-
Params
Return values
$this|null —__construct()
Model constructor.
public
__construct([ConnectionInterface|null &$db = null ][, ValidationInterface|null $validation = null ]) : mixed
Parameters
- $db : ConnectionInterface|null = null
-
DB Connection
- $validation : ValidationInterface|null = null
-
Validation
Return values
mixed —__get()
Provides/instantiates the builder/db connection and model's table/primary key names and return type.
public
__get(string $name) : mixed
Parameters
- $name : string
-
Name
Return values
mixed —__isset()
Checks for the existence of properties across this model, builder, and db connection.
public
__isset(string $name) : bool
Parameters
- $name : string
-
Name
Return values
bool —allowCallbacks()
Sets $tempAllowCallbacks value so that we can temporarily override the setting. Resets after the next method that uses triggers.
public
allowCallbacks([bool $val = true ]) : $this
Parameters
- $val : bool = true
-
value
Return values
$this —asArray()
Sets the return type of the results to be as an associative array.
public
asArray() : $this
Return values
$this —asObject()
Sets the return type to be of the specified type of object.
public
asObject([string $class = 'object' ]) : $this
Defaults to a simple object, but can be any class that has class vars with the same name as the collection columns, or at least allows them to be created.
Parameters
- $class : string = 'object'
-
Class Name
Return values
$this —builder()
Provides a shared instance of the Query Builder.
public
builder([string|null $table = null ]) : BaseBuilder
Parameters
- $table : string|null = null
-
Table name
Tags
Return values
BaseBuilder —chunk()
Loops over records in batches, allowing you to operate on them.
public
chunk(int $size, Closure $userFunc) : void
Works with $this->builder to get the Compiled select to determine the rows to operate on. This methods works only with dbCalls
Parameters
- $size : int
-
Size
- $userFunc : Closure
-
Callback Function
Tags
Return values
void —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.
public
static classToArray(string|object $data[, string|null $primaryKey = null ][, string $dateFormat = 'datetime' ][, bool $onlyChanged = true ]) : array<string|int, mixed>
Parameters
- $data : string|object
-
Data
- $primaryKey : string|null = null
-
Primary Key
- $dateFormat : string = 'datetime'
-
Date Format
- $onlyChanged : bool = true
-
Only Changed
Tags
Return values
array<string|int, mixed> —cleanRules()
Should validation rules be removed before saving? Most handy when doing updates.
public
cleanRules([bool $choice = false ]) : $this
Parameters
- $choice : bool = false
-
Value
Return values
$this —countAllResults()
Override countAllResults to account for soft deleted accounts.
public
countAllResults([bool $reset = true ][, bool $test = false ]) : mixed
Parameters
- $reset : bool = true
-
Reset
- $test : bool = false
-
Test
Return values
mixed —delete()
Deletes a single record from the database where $id matches
public
delete([int|string|array<string|int, mixed>|null $id = null ][, bool $purge = false ]) : BaseResult|bool
Parameters
- $id : int|string|array<string|int, mixed>|null = null
-
The rows primary key(s)
- $purge : bool = false
-
Allows overriding the soft deletes setting.
Tags
Return values
BaseResult|bool —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.
public
errors([bool $forceDB = false ]) : array<string, string>
The return array should be in the following format: ['source' => 'message']
Parameters
- $forceDB : bool = false
-
Always grab the db error, not validation
Return values
array<string, string> —find()
Fetches the row of database
public
find([array<string|int, mixed>|int|string|null $id = null ]) : array<string|int, mixed>|object|null
Parameters
- $id : array<string|int, mixed>|int|string|null = null
-
One primary key or an array of primary keys
Return values
array<string|int, mixed>|object|null —The resulting row of data, or null.
findAll()
Fetches all results, while optionally limiting them.
public
findAll(int $limit, int $offset) : array<string|int, mixed>
Parameters
- $limit : int
-
Limit
- $offset : int
-
Offset
Return values
array<string|int, mixed> —findColumn()
Fetches the column of database
public
findColumn(string $columnName) : array<string|int, mixed>|null
Parameters
- $columnName : string
-
Column Name
Tags
Return values
array<string|int, mixed>|null —The resulting row of data, or null if no data found.
first()
Returns the first row of the result set.
public
first() : array<string|int, mixed>|object|null
Return values
array<string|int, mixed>|object|null —getIdValue()
Returns the id value for the data array or object
public
getIdValue(array<string|int, mixed>|object $data) : int|array<string|int, mixed>|string|null
Parameters
- $data : array<string|int, mixed>|object
-
Data
Return values
int|array<string|int, mixed>|string|null —getInsertID()
Returns last insert ID or 0.
public
getInsertID() : int|string
Return values
int|string —getValidationMessages()
Returns the model's define validation messages so they can be used elsewhere, if needed.
public
getValidationMessages() : array<string|int, mixed>
Return values
array<string|int, mixed> —getValidationRules()
Returns the model's defined validation rules so that they can be used elsewhere, if needed.
public
getValidationRules([array<string|int, mixed> $options = [] ]) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed> = []
-
Options
Return values
array<string|int, mixed> —insert()
Inserts data into the database. If an object is provided, it will attempt to convert it to an array.
public
insert([array<string|int, mixed>|object|null $data = null ][, bool $returnID = true ]) : BaseResult|object|int|string|false
Parameters
- $data : array<string|int, mixed>|object|null = null
-
Data
- $returnID : bool = true
-
Whether insert ID should be returned or not.
Tags
Return values
BaseResult|object|int|string|false —insertBatch()
Compiles batch insert runs the queries, validating each row prior.
public
insertBatch([array<string|int, mixed>|null $set = null ][, bool|null $escape = null ][, int $batchSize = 100 ][, bool $testing = false ]) : int|bool
Parameters
- $set : array<string|int, mixed>|null = null
-
an associative array of insert values
- $escape : bool|null = null
-
Whether to escape values and identifiers
- $batchSize : int = 100
-
The size of the batch to run
- $testing : bool = false
-
True means only number of records is returned, false will execute the query
Tags
Return values
int|bool —Number of rows inserted or FALSE on failure
onlyDeleted()
Works with the find* methods to return only the rows that have been deleted.
public
onlyDeleted() : $this
Return values
$this —paginate()
Works with Pager to get the size and offset parameters.
public
paginate([int|null $perPage = null ][, string $group = 'default' ][, int|null $page = null ], int $segment) : array<string|int, mixed>|null
Expects a GET variable (?page=2) that specifies the page of results to display.
Parameters
- $perPage : int|null = null
-
Items per page
- $group : string = 'default'
-
Will be used by the pagination library to identify a unique pagination set.
- $page : int|null = null
-
Optional page number (useful when the page number is provided in different way)
- $segment : int
-
Optional URI segment number (if page number is provided by URI segment)
Return values
array<string|int, mixed>|null —protect()
Sets whether or not we should whitelist data set during updates or inserts against $this->availableFields.
public
protect([bool $protect = true ]) : $this
Parameters
- $protect : bool = true
-
Value
Return values
$this —purgeDeleted()
Permanently deletes all rows that have been marked as deleted through soft deletes (deleted = 1)
public
purgeDeleted() : bool|mixed
Return values
bool|mixed —replace()
Compiles a replace and runs the query
public
replace([array<string|int, mixed>|null $data = null ][, bool $returnSQL = false ]) : mixed
Parameters
- $data : array<string|int, mixed>|null = null
-
Data
- $returnSQL : bool = false
-
Set to true to return Query String
Return values
mixed —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.
public
save(array<string|int, mixed>|object $data) : bool
Parameters
- $data : array<string|int, mixed>|object
-
Data
Tags
Return values
bool —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.
public
set(mixed $key[, string|null $value = '' ][, bool|null $escape = null ]) : $this
Parameters
- $key : mixed
-
Field name, or an array of field/value pairs
- $value : string|null = ''
-
Field value, if $key is a single field
- $escape : bool|null = null
-
Whether to escape values and identifiers
Return values
$this —setAllowedFields()
It could be used when you have to change default or override current allowed fields.
public
setAllowedFields(array<string|int, mixed> $allowedFields) : $this
Parameters
- $allowedFields : array<string|int, mixed>
-
Array with names of fields
Return values
$this —setTable()
Specify the table associated with a model
public
setTable(string $table) : $this
Parameters
- $table : string
-
Table
Return values
$this —setValidationMessage()
Allows to set field wise validation message.
public
setValidationMessage(string $field, array<string|int, mixed> $fieldMessages) : $this
It could be used when you have to change default or override current validate messages.
Parameters
- $field : string
-
Field Name
- $fieldMessages : array<string|int, mixed>
-
Validation messages
Return values
$this —setValidationMessages()
Allows to set validation messages.
public
setValidationMessages(array<string|int, mixed> $validationMessages) : $this
It could be used when you have to change default or override current validate messages.
Parameters
- $validationMessages : array<string|int, mixed>
-
Value
Return values
$this —setValidationRule()
Allows to set field wise validation rules.
public
setValidationRule(string $field, string|array<string|int, mixed> $fieldRules) : $this
It could be used when you have to change default or override current validate rules.
Parameters
- $field : string
-
Field Name
- $fieldRules : string|array<string|int, mixed>
-
Validation rules
Return values
$this —setValidationRules()
Allows to set validation rules.
public
setValidationRules(array<string|int, mixed> $validationRules) : $this
It could be used when you have to change default or override current validate rules.
Parameters
- $validationRules : array<string|int, mixed>
-
Value
Return values
$this —skipValidation()
Set the value of the skipValidation flag.
public
skipValidation([bool $skip = true ]) : $this
Parameters
- $skip : bool = true
-
Value
Return values
$this —update()
Updates a single record in the database. If an object is provided, it will attempt to convert it into an array.
public
update([int|array<string|int, mixed>|string|null $id = null ][, array<string|int, mixed>|object|null $data = null ]) : bool
Parameters
- $id : int|array<string|int, mixed>|string|null = null
-
ID
- $data : array<string|int, mixed>|object|null = null
-
Data
Tags
Return values
bool —updateBatch()
Compiles an update and runs the query
public
updateBatch([array<string|int, mixed>|null $set = null ][, string|null $index = null ][, int $batchSize = 100 ][, bool $returnSQL = false ]) : mixed
Parameters
- $set : array<string|int, mixed>|null = null
-
An associative array of update values
- $index : string|null = null
-
The where key
- $batchSize : int = 100
-
The size of the batch to run
- $returnSQL : bool = false
-
True means SQL is returned, false will execute the query
Tags
Return values
mixed —Number of rows affected or FALSE on failure
validate()
Validate the data against the validation rules (or the validation group) specified in the class property, $validationRules.
public
validate(array<string|int, mixed>|object $data) : bool
Parameters
- $data : array<string|int, mixed>|object
-
Data
Return values
bool —withDeleted()
Sets $useSoftDeletes value so that we can temporarily override the soft deletes settings. Can be used for all find* methods.
public
withDeleted([bool $val = true ]) : $this
Parameters
- $val : bool = true
-
Value
Return values
$this —cleanValidationRules()
Removes any rules that apply to fields that have not been set currently so that rules don't block updating when only updating a partial row.
protected
cleanValidationRules(array<string|int, mixed> $rules[, array<string|int, mixed>|null $data = null ]) : array<string|int, mixed>
Parameters
- $rules : array<string|int, mixed>
-
Array containing field name and rule
- $data : array<string|int, mixed>|null = null
-
Data
Return values
array<string|int, mixed> —doDelete()
Deletes a single record from $this->table where $id matches the table's primaryKey This methods works only with dbCalls
protected
doDelete([int|string|array<string|int, mixed>|null $id = null ][, bool $purge = false ]) : string|bool
Parameters
- $id : int|string|array<string|int, mixed>|null = null
-
The rows primary key(s)
- $purge : bool = false
-
Allows overriding the soft deletes setting.
Tags
Return values
string|bool —doErrors()
Grabs the last error(s) that occurred from the Database connection.
protected
doErrors() : array<string, string>
The return array should be in the following format: ['source' => 'message'] This methods works only with dbCalls
Return values
array<string, string> —doFind()
Fetches the row of database from $this->table with a primary key matching $id. This methods works only with dbCalls This methods works only with dbCalls
protected
doFind(bool $singleton[, array<string|int, mixed>|int|string|null $id = null ]) : array<string|int, mixed>|object|null
Parameters
- $singleton : bool
-
Single or multiple results
- $id : array<string|int, mixed>|int|string|null = null
-
One primary key or an array of primary keys
Return values
array<string|int, mixed>|object|null —The resulting row of data, or null.
doFindAll()
Works with the current Query Builder instance to return all results, while optionally limiting them.
protected
doFindAll(int $limit, int $offset) : array<string|int, mixed>
This methods works only with dbCalls
Parameters
- $limit : int
-
Limit
- $offset : int
-
Offset
Return values
array<string|int, mixed> —doFindColumn()
Fetches the column of database from $this->table This methods works only with dbCalls
protected
doFindColumn(string $columnName) : array<string|int, mixed>|null
Parameters
- $columnName : string
-
Column Name
Return values
array<string|int, mixed>|null —The resulting row of data, or null if no data found.
doFirst()
Returns the first row of the result set. Will take any previous Query Builder calls into account when determining the result set.
protected
doFirst() : array<string|int, mixed>|object|null
This methods works only with dbCalls
Return values
array<string|int, mixed>|object|null —doInsert()
Inserts data into the current table.
protected
doInsert(array<string|int, mixed> $data) : Query|bool
This methods works only with dbCalls
Parameters
- $data : array<string|int, mixed>
-
Data
Return values
Query|bool —doInsertBatch()
Compiles batch insert strings and runs the queries, validating each row prior.
protected
doInsertBatch([array<string|int, mixed>|null $set = null ][, bool|null $escape = null ][, int $batchSize = 100 ][, bool $testing = false ]) : int|bool
This methods works only with dbCalls
Parameters
- $set : array<string|int, mixed>|null = null
-
An associative array of insert values
- $escape : bool|null = null
-
Whether to escape values and identifiers
- $batchSize : int = 100
-
The size of the batch to run
- $testing : bool = false
-
True means only number of records is returned, false will execute the query
Return values
int|bool —Number of rows inserted or FALSE on failure
doOnlyDeleted()
Works with the find* methods to return only the rows that have been deleted.
protected
doOnlyDeleted() : void
This methods works only with dbCalls
Return values
void —doProtectFields()
Ensures that only the fields that are allowed to be updated are in the data array.
protected
doProtectFields(array<string|int, mixed> $data) : array<string|int, mixed>
Used by insert() and update() to protect against mass assignment vulnerabilities.
Parameters
- $data : array<string|int, mixed>
-
Data
Tags
Return values
array<string|int, mixed> —doPurgeDeleted()
Permanently deletes all rows that have been marked as deleted through soft deletes (deleted = 1) This methods works only with dbCalls
protected
doPurgeDeleted() : bool|mixed
Return values
bool|mixed —doReplace()
Compiles a replace into string and runs the query This methods works only with dbCalls
protected
doReplace([array<string|int, mixed>|null $data = null ][, bool $returnSQL = false ]) : mixed
Parameters
- $data : array<string|int, mixed>|null = null
-
Data
- $returnSQL : bool = false
-
Set to true to return Query String
Return values
mixed —doUpdate()
Updates a single record in $this->table.
protected
doUpdate([int|array<string|int, mixed>|string|null $id = null ][, array<string|int, mixed>|null $data = null ]) : bool
This methods works only with dbCalls
Parameters
- $id : int|array<string|int, mixed>|string|null = null
-
ID
- $data : array<string|int, mixed>|null = null
-
Data
Return values
bool —doUpdateBatch()
Compiles an update string and runs the query This methods works only with dbCalls
protected
doUpdateBatch([array<string|int, mixed>|null $set = null ][, string|null $index = null ][, int $batchSize = 100 ][, bool $returnSQL = false ]) : mixed
Parameters
- $set : array<string|int, mixed>|null = null
-
An associative array of update values
- $index : string|null = null
-
The where key
- $batchSize : int = 100
-
The size of the batch to run
- $returnSQL : bool = false
-
True means SQL is returned, false will execute the query
Tags
Return values
mixed —Number of rows affected or FALSE on failure
fillPlaceholders()
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:
protected
fillPlaceholders(array<string|int, mixed> $rules, array<string|int, mixed> $data) : array<string|int, mixed>
[ '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]'
Parameters
- $rules : array<string|int, mixed>
-
Validation rules
- $data : array<string|int, mixed>
-
Data
Tags
Return values
array<string|int, mixed> —idValue()
Returns the id value for the data array or object
protected
idValue(array<string|int, mixed>|object $data) : int|array<string|int, mixed>|string|null
Parameters
- $data : array<string|int, mixed>|object
-
Data
Tags
Return values
int|array<string|int, mixed>|string|null —initialize()
Initializes the instance with any additional steps.
protected
initialize() : mixed
Optionally implemented by child classes.
Return values
mixed —intToDate()
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.
protected
intToDate(int $value) : int|string
The available time formats are:
- 'int' - Stores the date as an integer timestamp
- 'datetime' - Stores the data in the SQL datetime format
- 'date' - Stores the date (only) in the SQL date format.
Parameters
- $value : int
-
value
Tags
Return values
int|string —objectToArray()
Takes a class an returns an array of it's public and protected properties as an array suitable for use in creates and updates.
protected
objectToArray(string|object $data[, bool $onlyChanged = true ][, bool $recursive = false ]) : array<string|int, mixed>
This method use objectToRawArray internally and does conversion to string on all Time instances
Parameters
- $data : string|object
-
Data
- $onlyChanged : bool = true
-
Only Changed Property
- $recursive : bool = false
-
If true, inner entities will be casted as array as well
Tags
Return values
array<string|int, mixed> —Array
objectToRawArray()
Takes a class an returns an array of it's public and protected properties as an array with raw values.
protected
objectToRawArray(string|object $data[, bool $onlyChanged = true ][, bool $recursive = false ]) : array<string|int, mixed>|null
Parameters
- $data : string|object
-
Data
- $onlyChanged : bool = true
-
Only Changed Property
- $recursive : bool = false
-
If true, inner entities will be casted as array as well
Tags
Return values
array<string|int, mixed>|null —Array
setDate()
Sets the date or current date if null value is passed
protected
setDate([int|null $userData = null ]) : mixed
Parameters
- $userData : int|null = null
-
An optional PHP timestamp to be converted.
Tags
Return values
mixed —shouldUpdate()
This method is called on save to determine if entry have to be updated If this method return false insert operation will be executed
protected
shouldUpdate(array<string|int, mixed>|object $data) : bool
Parameters
- $data : array<string|int, mixed>|object
-
Data
Return values
bool —timeToDate()
Converts Time value to string using $this->dateFormat
protected
timeToDate(Time $value) : string|int
The available time formats are:
- 'int' - Stores the date as an integer timestamp
- 'datetime' - Stores the data in the SQL datetime format
- 'date' - Stores the date (only) in the SQL date format.
Parameters
- $value : Time
-
value
Return values
string|int —transformDataToArray()
Transform data to array
protected
transformDataToArray(array<string|int, mixed>|object|null $data, string $type) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>|object|null
-
Data
- $type : string
-
Type of data (insert|update)
Tags
Return values
array<string|int, mixed> —trigger()
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.
protected
trigger(string $event, array<string|int, mixed> $eventData) : mixed
It is the responsibility of the callback methods to return the data itself.
Each $eventData 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)
If callbacks are not allowed then returns $eventData immediately.
Parameters
- $event : string
-
Event
- $eventData : array<string|int, mixed>
-
Event Data