primal/record

该软件包已被废弃,不再维护。没有建议的替代软件包。

适用于 PDO 的简单且可移植的 Active Record 实现。

v2.0a4 2013-02-03 18:51 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:22:53 UTC


README

#Primal Record

由 Jarvis Badgley 创建并版权所有 2013,chiper at chipersoft dot com。

Primal Record 是一个 Active Record ORM 库,用于将单个表的行作为数组进行操作,设计得非常轻量级且可扩展,以提供最大的灵活性。建议将 Primal Record 用于数据映射功能,但也可以根据需要直接用作数据模型。

Primal 是一组用于简化常见 PHP 任务的独立微库。

请注意,RECORD 2.0 处于 alpha 状态,可能会进行实现更改

##用法

Primal Record 是一个抽象类,必须 扩展才能使用。为了快速开发,唯一的实现要求是定义一个表名。Primal Record 将自动从您的数据库请求表架构,并缓存该结构以供以后使用。

假设您有一个以下表需要交互

CREATE TABLE `example_table` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `column_1` varchar(200) DEFAULT NULL,
  `column_2` int(11) DEFAULT NULL,
  `date_column` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
)

首先为该表创建您的记录实现。

class ExampleRecord extends \Primal\Database\MySQL\Record {
	protected $tablename = 'example_table';
}

一旦创建了此实现,您就可以与表行进行接口。

以下示例假设以下代码已经执行

$pdo = new PDO("mysql:host=localhost;dbname=test", 'username', 'password');

获取和设置行数据

Primal Record 扩展了内置的 PHP ArrayObject 类。所有行数据都在对象上访问,就像 Record 对象是一个数组一样

$row = new ExampleRecord($pdo);
$row['column_1'] = "Foo";

Record 还实现了 __get__set 方法,允许将内容作为对象的属性获取和设置。

$row = new ExampleRecord($pdo);
$row->column_1 = "Foo";

然而,这不是一个推荐的方法,因为它消除了实现数据(成员属性)和外部数据(数据库行)之间的分离。

保存行

save() 函数将执行行的智能保存。如果主键与数据库中现有行匹配,save 将执行更新。如果不匹配,或自动递增的主键未被定义,save 将执行插入,并将记录对象更新为新自动递增的列值。

$row = new ExampleRecord($pdo);
$row['column_1'] = "Foo";
$row['column_2'] = 16;
$row['date_column'] = new DateTime('yesterday');
$row->save();

//$row['id'] now contains the auto-incremented primary key value.

如果已知记录是否存在,您也可以显式调用 insert() 和 update() 函数。调用 insert(true) 将执行行替换(REPLACE 与 INSERT)。还提供了一个用于保存为全新行的快捷方式;调用 save(true) 将删除自动递增的主键并触发插入一个全新的行。

保存部分行内容

只有定义在 Record 对象上的值将包括在 INSERT 和 UPDATE 调用中。

$row = new ExampleRecord($pdo);
$row['id'] = 2;
$row['column_2'] = 16;
$row['date_column'] = 'now'; //Record will automatically parse strings into DateTime values for date column types (`DATE`, `DATETIME`, `TIMESTAMP`).
$row->save();

// only `column_2` and `date_column` on the row keyed with an id of 2 will be updated, all other columns will remain unchanged.

调用 save 或 update 将更新数据库中 Record 定义的值。如果您只想更新单个值,则可以使用 set() 函数。

$row = new ExampleRecord($pdo);
if ($row->load(5)) {
    $row->set('column_2', $new_value);
}

set() 的第二个参数可以省略,以触发使用记录中定义的值(如果没有定义则为 NULL)的更新。

加载行

$row = new ExampleRecord($pdo);
if ($row->load(2)) {
    //$row found a table row where the `id` column (the primary key) contained 2.
} else {
    //No row was found that matched that primary key value.
}

load() 函数支持三种交互方法

  1. 单主键加载:$row->load($value);
  2. 单非主键加载:$row->load($value, 'column_name');
  3. 多键加载:$row->load(array('column_1'=>'foo', 'column_2'=>8));

在所有三种情况下,load() 将返回一个布尔条件,指示是否找到匹配的行。此结果也可以从公共的 found 属性中检索。

为了方便,可以直接在类构造函数中传递加载参数。

$user = new User($pdo, array('email'=>'john.doe@example.com'));
if ($user->found) {
    // user code
}

##预缓存表结构

如果您的表模式已经锁定且不会更改,您可以通过在类定义中预先创建模式结构数组来提高性能。

class Member extends \Primal\Database\MySQL\Record {
	protected $tablename = 'members';
	protected $schema = array(
    	'primaries' => array('member_id'),
    	'auto_increment' => 'member_id',
    	'columns' => array(
    		'member_id' => array(
    			'null' => false,
    			'unsigned' => 1,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'int',
    		),
    		'email' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'varchar',
    			'length' => '255',
    		),
    		'username' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'varchar',
    			'length' => '200',
    		),
    		'firstname' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'tinytext',
    		),
    		'middlename' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'tinytext',
    		),
    		'lastname' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'tinytext',
    		),
    		'website' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'string',
    			'precision' => 0,
    			'type' => 'text',
    		),
    		'industry' => array(
    			'null' => false,
    			'unsigned' => 1,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'int',
    		),
    		'last_updated' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'datetime',
    			'precision' => 0,
    			'type' => 'timestamp',
    		),
    		'last_login' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'datetime',
    			'precision' => 0,
    			'type' => 'datetime',
    		),
    		'validated' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'tinyint',
    		),
    		'review_rating' => array(
    			'null' => true,
    			'unsigned' => 1,
    			'format' => 'number',
    			'precision' => 0,
    			'type' => 'int',
    		),
    		'membership_type' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'enum',
    			'precision' => 0,
    			'type' => 'enum',
    			'options' => array (
    				0 => 'Free',
    				1 => 'None',
    				2 => 'Credited',
    				3 => 'Monthly',
    				4 => 'Yearly',
    			),
    		),
    		'membership_expires' => array(
    			'null' => true,
    			'unsigned' => 0,
    			'format' => 'date',
    			'precision' => 0,
    			'type' => 'date',
    		),
    		'balance' => array(
    			'null' => false,
    			'unsigned' => 0,
    			'format' => 'number',
    			'precision' => '2',
    			'type' => 'decimal',
    		),
    	)
    );
}

这个结构可以手动创建,但通过结合使用 var_export 函数和 ->buildTableSchema() 更容易通过代码获得。

$record = new Member($this->mypdo);
var_export($record->buildTableSchema());

//output is the above array structure