tigron/skeleton-object

Tigron 骨架对象特性

v1.1 2024-05-14 21:31 UTC

README

简介

这个库包含多个特性,可以添加到你的类中。每个特性都会为类添加更多功能。要使用特性,请在类定义中添加以下行

use \Skeleton\Object\TRAIT;

特性

模型特性

此特性为数据库中的对象提供基本功能

$object = new MyClass();
$object->database_field_1 = $value1;
$object->database_field_2 = $value2;

$dirty_fields = $object->get_dirty_fields();

此变量现在包含 [ 'database_field_1', 'database_field_2' ]

$object->load_array( $_POST['form_for_object'] );

删除特性

$object->delete()

永久删除对象

$object->archive()

在字段 'archived' 中存储存档日期

$object->restore()

重置字段 'archived'

获取特性

$object->get_info()

返回包含对象所有数据库字段的数组

$object->get_classname()

返回对象的类名

Object::get_by_id($id)

返回具有 id '$id' 的对象

Object::get_all($sort, $direction)

返回所有对象。如果存在存档列,则会考虑该列

可选参数

$sort: the field to sort on
$direction: ASC/DESC

别名特性

别名特性创建了一个易于阅读的字符串,可以用于 URL 中标识特定页面。别名创建分为 3 步

检查是否需要生成或重新生成别名

  • 如果请求在新的对象上保存(),则别名创建将继续
  • trait_slug_needs_regeneration(): bool 被调用以检查现有别名是否需要重新生成。默认情况下,trait_sltrait_slug_needs_regeneration() 返回 false,别名永远不会重新生成。

查找基本字符串

为了找到基本字符串,调用方法 trait_slug_get_base(): string。默认情况下,它搜索属性 'name' 或属性 'text_en_name' 中的值,其中 'en' 被基本语言替换。字段可以在 $class_configuration['sluggable'] 中指定。

从基本字符串生成别名

基本字符串被转换为小写 ASCII 字符。空格被 '-' 替换。

使别名唯一

为了使别名唯一,调用方法 trait_slug_unique($slug): $unique。如果别名已存在,则此方法将十六进制值追加到别名中,直到别名变得唯一。

UUID 特性

当使用 UUID 特性时,名为 "uuid" 的字段将填充随机且唯一的 UUIDv4。

数字特性

数字特性将为每个对象生成一个唯一的数字。与主键(id)不同,数字字段将在给定的 'number_dividers' 集合内保持唯一,'number_dividers' 是一个字段名称数组。

例如:为发票中的每个项目创建一个唯一的数字

Invoice X
   Invoice_Item 1
   Invoice_Item 2
   Invoice_Item 3
Invoice Y
   Invoice_Item 1
   Invoice_Item 2
   Invoice_Item 3

对于此示例,需要将类配置定义为

private static $class_configuration = array (
  'number_dividers' => [ 'invoice_id' ], // A unique number is created per object with the same invoice_id
  'number_field' => 'number', // Store the number in field 'number'
);

子特性

如果你想从一个骨架对象扩展并可能在一个单独的表中存储特定于子类的信息,请使用此特性。

为此功能正常工作,需要遵循一些简单的规则

  • 子类只能有 Child 特性,并且必须从合适的骨架对象扩展

  • 子类可以有自己的数据库表,其中包含对父级的引用。默认情况下,这是父类名的小写后跟 _id。你可以通过类配置 parent_field_id 来覆盖此设置。

  • 如果子类不应使用数据库表,请将子类的 class_configuration 中的 database_table 设置为 null

  • 父类需要有一个字段来存储子对象的类名。这个值应通过类配置 child_classname_field 来设置。

  • 可以通过以下方式将子对象强制转换为另一个子类:

    $new_class = $object->cast('another_child_class');

缓存特质

如果您想减少数据库查询次数,可以使用此特质。缓存激活可以针对每个骨骼对象进行。

use \Skeleton\Object\Cache;

有三种缓存类型:

内存

这是一种按进程的缓存技术。对象存储在进程缓存中。如果多次请求同一对象,它只会从数据库查询一次。请注意,此缓存不考虑其他PHP进程。如果对象在另一个进程中被更新,您的缓存不会被失效。

\Skeleton\Object\Config::$cache_handler = 'Memory';

Memcache

使用php-memcache进行缓存技术(https://php.ac.cn/manual/en/book.memcache.php)。主机名、端口和过期时间应通过缓存配置设置。

\Skeleton\Object\Config::$cache_handler = 'Memcache';
\Skeleton\Object\Config::$cache_handler_config = [
	'hostname' => '127.0.0.1',
	'port' => '11211',
	'expire' => 600,
];

Memcached

使用php-memcached进行缓存技术(https://php.ac.cn/manual/en/book.memcached.php)。主机名、端口和过期时间应通过缓存配置设置。

\Skeleton\Object\Config::$cache_handler = 'Memcached';
\Skeleton\Object\Config::$cache_handler_config = [
	'hostname' => '127.0.0.1',
	'port' => '11211',
	'expire' => 600,
];

类配置

此处指定的特质考虑了在类内可以设置的一些特殊配置参数。

disallow_set (数组)

private static $class_configuration = array (
  'disallow_set' => array (
    'directory',
    'full_path',
    'password',
  ),
);

防止直接设置一些类变量

database_table (字符串)

private static $class_configuration = [
  'database_table' => 'my_super_special_class',
];

覆盖默认表名
默认: strtolower(get_class())

database_config_name (字符串)

private static $class_configuration = [
  'database_config_name' => 'database_dsn',
];

覆盖默认数据库连接
默认: Database::get();

table_field_id (字符串)

private static $class_configuration = [
  'table_field_id' => 'my_strange_id',
];

覆盖默认主键字段
默认: 'id'

table_field_created (字符串)

private static $class_configuration = [
  'table_field_created' => 'my_strange_created_field',
];

覆盖用于存储创建时间戳的默认字段
默认: 'created'

table_field_updated (字符串)

private static $class_configuration = [
  'table_field_updated' => 'my_strange_updated_field',
];

覆盖用于存储最后更新时间戳的默认字段
默认: 'updated'

table_field_archived (字符串)

private static $class_configuration = [
  'table_field_archived' => 'my_strange_updated_archived',
];

覆盖用于存储归档时间戳的默认字段
默认: 'archived'

sluggable (字符串)

private static $class_configuration = [
  'sluggable' => 'my_field_to_slug',
];

覆盖用于创建slug的字段
默认: 'name'

number_dividers (数组)

private static $class_configuration = [
  'number_dividers' => [ 'group1', 'group2' ],
];

定义将具有唯一数字的对象分组在一起的字段
默认: []

number_field (字符串)

private static $class_configuration = [
  'number_field' => [ 'number' ],
];

定义存储唯一数字的字段
默认: null