Model_Crud Class
Introduction
A lot of database operations come to basic CRUD (Create Retrieve Update Delete) operations. The Model_Crud class supplies there functionalities in a standardized way. The class helps you with:
- Creating database entries
- Retrieving database entries
- Updating database entries
- Deleting database entries
- Entree input validation
Your first model
To use the Model_Crud class, create a class that extends \Model_Crud. Example:
<?php
class Model_Users extends \Model_Crud
{
// Set the table to use
protected static $_table_name = 'users';
}
Now you have a basic model to work with.
Configuration
Configuring a model is done by setting a few parameters:
Param | Type | Default | Description | Example |
---|---|---|---|---|
$_table_name | string | required | The table to use. |
|
$_primary_key | string |
|
The table's id field. |
|
$_rules | array | none | Input validation rules |
|
$_labels | array | none | The validation labels. |
|
$_properties | array | none | Columns to use when updating/saving. |
|
$_mass_whitelist | array | none |
Array of columns which can be set with: __construct, ::forge ->set() |
|
$_mass_blacklist | array | none | Array of columns which could not be set with: __construct, ::forge and ->set() method. |
|
$_mass_whitelist is like a allows for extra security when mass-assigning propperties. Do note that this only works with __construct, ::forge and ->set. |
||||
$_connection | string | none | The database connection to use, or the connection used for reads in a master/slave setup. If not configured, the DB config default will be used. |
|
$_write_connection | string | none | The database connection to use for writes in a master/slave setup. |
|
$_defaults | array | none | An array of default values |
|
$_created_at | string | none | Field name for a 'created at' field. Set $_mysql_timestamp to true to use a MySQL timestamp instead of a UNIX timestamp |
|
$_updated_at | string | none | Field name for a 'updated at' field. Set $_mysql_timestamp to true to use a MySQL timestamp instead of a UNIX timestamp |
|
$_mysql_timestamp | boolean | none | Set to true to use a MySQL timestamp instead of a UNIX timestamp for $_created_at and $_updated_at fields. |
|