当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Model_Crud 简介 - 类別

优质
小牛编辑
118浏览
2023-12-01

简介

许多资料库的操作回归基本的 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;