Model
class Model
Parent class for our models. Takes functions from DB wrapper and extract functionality further to make operations easier to use and improve extendability.
Constants
| BLACKLIST |
Constant for blacklist. |
| WHITELIST |
Constant for whitelist. |
Properties
| static protected DB | $_db | The instance of the DB class. |
|
| number | $id | The current id. |
|
| protected string | $_modelName | The name of the model. |
|
| static protected bool | $_softDelete | $_softDelete value. |
|
| static protected string | $_table | The name of the table. |
|
| protected bool | $_validates | Flag to indicate if validation is successful. |
|
| protected array | $_validationErrors | The array of validation errors. |
Methods
Default constructor.
Generates error messages that occur during form validation.
Called before delete.
Called before save.
Update the object with an associative array.
This runs before delete, needs to return a boolean
Called after save.
Retrieves number of rows affected or returned by the last query. This is a wrapper function for the DB::count function.
Wrapper for database delete function. If not softDelete we set it.
Gets columns from table.
Gets an associative array of field values for insert or updating.
Displays error messages when form validation fails.
Used to set default fetchStyle param.
Wrapper for the find function that is found in the DB class.
Get result from database by primary key ID.
Performs a database query against a specified table using the same query-building logic as the default find method.
Retrieves list of all records within a table related to a user.
Wrapper for the findFirst function that is found in the DB class.
Returns number of records in a table. A wrapper function for findTotal function in DB class.
Wrapper for database insert function.
Checks if an object is a new insertion or an existing record.
Runs when the object is constructed.
Populates object with data.
Runs a validator object and sets validates boolean and adds error message if validator fails.
Wrapper for update and insert functions. A failed form validation will cause this function to return false.
Adds to the conditions to avoid getting soft deleted rows returned
Wrapper for the update function found in the DB class.
Updates one or more rows in this model's underlying table using your framework's params-style conditions.
Getter function for the $_validates boolean instance variable.
Function that is called on save. If validation fails the save function will not proceed. This function is just a signature and must be implemented by models that run form validation because since it is called from within this class.
Details
at line 66
__construct()
Default constructor.
at line 80
void
addErrorMessage(string $field, string $message)
Generates error messages that occur during form validation.
at line 93
void
afterDelete()
Called before delete.
at line 100
void
afterSave()
Called before save.
at line 112
Model
assign(array $params, array $list = [], string $assignmentFilter = self::BLACKLIST)
Update the object with an associative array.
at line 135
void
beforeDelete()
This runs before delete, needs to return a boolean
at line 142
void
beforeSave()
Called after save.
at line 150
static int
count()
Retrieves number of rows affected or returned by the last query. This is a wrapper function for the DB::count function.
at line 159
stdClass
data()
Grab object and if we just need data for smaller result set.
at line 180
bool
delete()
Wrapper for database delete function. If not softDelete we set it.
If row is set to softDelete we call the database delete function.
at line 202
static array
getColumns()
Gets columns from table.
at line 212
array
getColumnsForSave()
Gets an associative array of field values for insert or updating.
at line 241
static DB
getDb()
Returns an instance of the DB class.
at line 254
array
getErrorMessages()
Displays error messages when form validation fails.
at line 264
static protected array
_fetchStyleParams(array $params)
Used to set default fetchStyle param.
at line 278
static array|bool
find(array $params = [])
Wrapper for the find function that is found in the DB class.
at line 292
static object|bool
findById(int $id)
Get result from database by primary key ID.
at line 308
static array|bool
findByTable(string $table, array $params = [])
Performs a database query against a specified table using the same query-building logic as the default find method.
This method is useful when querying a different table than the one defined by the model's static::$_table property (e.g., joining or selecting from related tables).
at line 324
static array
findAllByUserId(int $user_id, array $params = [])
Retrieves list of all records within a table related to a user.
at line 342
static object|bool
findFirst(array $params = [])
Wrapper for the findFirst function that is found in the DB class.
at line 357
static int
findTotal(array $params = [])
Returns number of records in a table. A wrapper function for findTotal function in DB class.
at line 372
bool
insert(array $fields)
Wrapper for database insert function.
at line 384
bool
isNew()
Checks if an object is a new insertion or an existing record.
at line 393
void
onConstruct()
Runs when the object is constructed.
at line 401
protected void
populateObjData(array|object $result)
Populates object with data.
at line 416
DB
query(string $sql, array $bind = [])
Wrapper for database query function.
at line 427
void
runValidation(object $validator)
Runs a validator object and sets validates boolean and adds error message if validator fails.
at line 442
bool
save()
Wrapper for update and insert functions. A failed form validation will cause this function to return false.
at line 483
static protected array
_softDeleteParams(array $params)
Adds to the conditions to avoid getting soft deleted rows returned
at line 512
bool
update(array $fields)
Wrapper for the update function found in the DB class.
at line 542
static int
updateWhere(array $fields, array $params)
Updates one or more rows in this model's underlying table using your framework's params-style conditions.
This method delegates to the DB::updateWhere() method and allows
you to pass both the fields to update and a parameterized WHERE
clause (using conditions and bind arrays just like find/findFirst).
Example:
Queue::updateWhere(
['reserved_at' => date('Y-m-d H:i:s')],
['conditions' => 'id = ?', 'bind' => [$jobId]]
);
at line 552
bool
validationPassed()
Getter function for the $_validates boolean instance variable.
at line 563
void
validator()
Function that is called on save. If validation fails the save function will not proceed. This function is just a signature and must be implemented by models that run form validation because since it is called from within this class.