wartron / yii2-uuid
Yii2 UUID 辅助工具
dev-master
2015-09-20 21:38 UTC
Requires
- ramsey/uuid: ^2.8
- yiisoft/yii2: ~2.0
This package is not auto-updated.
Last update: 2024-09-28 18:10:37 UTC
README
用于将 uuid 作为主键的辅助工具。
安装
将 uuid 行为添加到模型中。
我们可以使用 SqlExpression 在 sql 中生成 uuid,但是在最后插入的 id 中我们无法获取 ID。(如果您想要一个纯 SQL 方法,可以通过触发器解决这个问题)。但在 PHP 中生成 ID UUID 时,我们不需要担心获取 ID 并刷新模型。
使用我们的 UUID'd ActiveRecord,它会负责生成 ID,并且可以处理 toArray 以在 API 调用中表示 uuid 和其他以十六进制表示的 uuid 字段。
use wartron\yii2uuid\db\ActiveRecord;
使用 id 的行为。
public function behaviors()
{
return [
\wartron\yii2uuid\behaviors\UUIDBehavior::className(),
];
}
要使用 asHex 格式化器
'components' => [
'formatter' => [
'class' => '\wartron\yii2uuid\components\Formatter'
],
]
Helper 提供对 ramsey/uuid 的访问。这将自动将我们使用的二进制格式转换为其他格式!
use wartron\yii2uuid\helpers\Uuid;
//generate uuids
Uuid::uuid1();
Uuid::uuid3();
Uuid::uuid4();
Uuid::uuid5();
//converting
Uuid::str2uuid($hexString);
Uuid::uuid2str($binary);
要使用 RESTful 控制器,请使用提供的 ActiveController 而不是 yii\rest\ActiveController
use wartron\yii2uuid\rest\ActiveController;