Model_Crud クラス

はじめに

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:

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.

設定

Configuring a model is done by setting a few parameters:

パラメータ 規定値 説明
$_table_name string 必須 The table to use.
protected static $_table_name = 'table';
$_primary_key string
'id'
The table's id field.
protected static $_primary_key = 'custom_id';
$_rules array none Input validation rules
protected static $_rules = array(
	'name' => 'required',
	'email' => 'required|valid_email',
);
$_labels array none The validation labels.
protected static $_labels = array(
	'name' => 'Your Name',
	'email' => 'Email Address',
);
$_properties array none Columns to use when updating/saving.
protected static $_properties = array(
	'id',
	'name'
	'age',
	'birth_date',
	'gender',
);
$_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.
protected static $_connection = null;
$_write_connection string none The database connection to use for writes in a master/slave setup.
protected static $_write_connection = 'master';
$_defaults array none An array of default values
protected static $_defaults = array(
	'field' => 'value',
	'other_field' => 'other value',
);
$_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
protected static $_created_at = 'created_at';
$_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
protected static $_updated_at = 'modified_at';
$_mysql_timestamp boolean none Set to true to use a MySQL timestamp instead of a UNIX timestamp for $_created_at and $_updated_at fields.
protected static $_mysql_timestamp = true;