dynamikaweb/yii2-uuid
最新稳定版本:1.1
Composer 安装命令:
composer require dynamikaweb/yii2-uuid
包简介
Guide and package for using UUID in yii2 projects
README 文档
README
Installation
The preferred way to install this extension is through composer.
Either run
$ composer require dynamikaweb/yii2-uuid "*"
or add
"dynamikaweb/yii2-uuid": "*"
to the require section of your composer.json file.
Database usage
Create migration
public function safeUp() { $this->addColumn('sometable', 'uuid', 'uuid' => $this->binary(16)->unique()->notNull()); $this->createIndex('sometable_uuid_idx', 'sometable', 'uuid'); ... }
Model usage
Validate
use dynamikaweb\uuid\UuidValidator; public function rules() { return [ [['uuid'], UuidValidator::classname(), 'on' => self::SCENARIO_SEARCH] ... ]; }
Generate and save
use dynamikaweb\uuid\Uuid; public function beforeSave($insert) { if (!parent::beforeSave($insert)) { return false; } if ($this->isNewRecord) { $this->setAttribute('uuid', Uuid::uuid4()->getBytes()); } ... }
Formatting to string
public function getUuidToString() { if (is_resource($this->uuid)) { $this->uuid = stream_get_contents($this->uuid); } return Uuid::fromBytes($this->uuid)->toString(); }
Controller usage
View
public function actionView($uuid) { return $this->render('view', [ 'model' => $this->findModel($uuid) ]); }
Finding
protected function findModel($uuid) { try { $uuid = '\x'.bin2hex(Uuid::fromString($uuid)->getBytes()); } catch (InvalidUuidStringException $e) { throw new HttpException(400, 'UUID invalid!'); } if (($model = SomeModel::findOne(['uuid' => $uuid])) === null) { throw new HttpException(404, 'UUID not found!'); } return $model; }
View usage
Form
use dynamikaweb\uuid\UuidMask; echo UuidMask::widget([ 'name' => 'uuid' ]);
Active form
echo $form->field($model, 'from_date')->widget(Uuid::className());
统计信息
- 总下载量: 1.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2021-04-20