Model_Crud 类別

简介

許多資料庫的操作回歸基本的 CRUD(建立、取回、更新、刪除)操作。 Model_Crud 类別提供标准化的功能,类別能幫助你:

你的第一个模型

要使用 Model_Crud 类別,建立一个类別擴充 \Model_Crud,範例:

<?php

class Model_Users extends \Model_Crud
{
	// 設定要使用的資料表
	protected static $_table_name = 'users';
}

现在你有基本的模型可以使用。

配置

藉由設定一些參数配置模型:

參数 类型 預設 描述 範例
$_table_name 字串 必要 要使用的資料表。
protected static $_table_name = 'table';
$_primary_key 字串
'id'
資料表的 id 欄位。
protected static $_primary_key = 'custom_id';
$_rules 陣列 輸入驗證規則
protected static $_rules = array(
	'name' => 'required',
	'email' => 'required|valid_email',
);
$_labels 陣列 驗證标籤。
protected static $_labels = array(
	'name' => 'Your Name',
	'email' => 'Email Address',
);
$_properties 陣列 當更新/儲存時要使用的行。
protected static $_properties = array(
	'id',
	'name',
	'age',
	'birth_date',
	'gender',
);
$_mass_whitelist 陣列 可以被:
__construct、
::forge
->set()
設定的行陣列。
protected static $_mass_whitelist = array(
	'first_name',
	'last_name',
	'age',
);
$_mass_blacklist 陣列 不能被:__construct、::forge 和 ->set() 方法設定的行陣列。
protected static $_mass_blacklist = array(
	'password',
	'is_admin',
);

$_mass_whitelist 就像是在大量指派屬性時允許額外的安全性。 千萬注意,这僅適用於 __construct::forge->set

$_connection 字串 要使用的資料庫連線,或在一个 master/slave 設定中用於讀取的連線。如果沒有配置,将使用預設的 DB 配置。
protected static $_connection = null;
$_write_connection 字串 在一个 master/slave 設定中要用來寫入的資料庫連線。
protected static $_write_connection = 'master';
$_defaults 陣列 預設值的陣列
protected static $_defaults = array(
	'field' => 'value',
	'other_field' => 'other value',
);
$_created_at 字串 給 'created at' 欄位的名稱。設 $_mysql_timestamp 为 true 以使用 MySQL 時間戳記取代 UNIX 時間戳記
protected static $_created_at = 'created_at';
$_updated_at 字串 給 'updated at' 欄位的名稱。設 $_mysql_timestamp 为 true 以使用 MySQL 時間戳記取代 UNIX 時間戳記
protected static $_updated_at = 'modified_at';
$_mysql_timestamp 布林 $_created_at$_updated_at 欄位, 設为 true 以使用 MySQL 時間戳記取代 UNIX 時間戳記。
protected static $_mysql_timestamp = true;