tigrov/yii2-pgsql-enum
Composer 安装命令:
composer require tigrov/yii2-pgsql-enum
包简介
Enum type behavior and helper for Yii2, for PostgreSQL only
README 文档
README
Enum type behavior and helper for Yii2, for PostgreSQL only.
Using enum types provides good readability of data in DataBase without spending extra resources on the storage of duplicate values (in cases with any string types).
Such types are more difficult to maintain in a project. This extension helps to simplify the use of enum types.
Limitation
Since 1.1.0 requires PHP >= 5.5
For method renameValue() requires PostgreSQL >= 10.0
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist tigrov/yii2-pgsql-enum
or add
"tigrov/yii2-pgsql-enum": "~1.0"
to the require section of your composer.json file.
Usage
Once the extension is installed, you can create new enum types as follow:
class NewEnum extends \tigrov\pgsql\enum\EnumBehavior { /** @var array list of attributes that are to be automatically humanized value */ public $attributes = ['type' => 'type_key']; }
Create a table with the enum type
// Create the enum in DB or in PHP code NewEnum::create(['value1', 'value2', 'value3']); \Yii::$app->getDb()->createCommand() ->createTable('model', [ 'id' => 'pk', 'type_key' => NewEnum::typeName(), // 'new_enum' ])->execute();
Create a model for the table
class Model extends \yii\db\ActiveRecord { /** * @inheritdoc */ public function behaviors() { return [ NewEnum::class, // 'type' => [ // 'class' => NewEnum::class, // 'attributes' => ['type' => 'type_key'], //], ]; } /** * @inheritdoc */ public function save($runValidation = true, $attributeNames = null) { // You can add new values if they are not exists in the enum type try { return parent::save($runValidation, $attributeNames); } catch (\yii\db\Exception $e) { NewEnum::add($this->type_key); return parent::save($runValidation, $attributeNames); } } }
and then use them in your code
/** * @var ActiveRecord $model */ $model = new Model; $model->type_key = 'value1'; $model->save(); $newModel = Model::findOne($model->id); $newModel->type_key === 'value1'; $newModel->type === 'Value1'; // see yii\helpers\Inflector::humanize($word, true) // The extension will try to add a new value if it does not exist $model->type_key = 'non-existent value'; $model->save(); // To get all enum values in [value => humanized value] array notation NewEnum::values(); // To get all enum values without humanized values in [value1, value2, ...] array notation NewEnum::codes(); // To add a new value to the enum type NewEnum::add('new value'); // To get a humanized value NewEnum::value('new value'); // returns "New Value" or translated value // To check if the enum type has a value NewEnum::has('new value'); // To rename a value of the enum type NewEnum::renameValue('new value', 'renamed value'); // To remove a value from the enum type NewEnum::remove('renamed value'); // To check if the enum type exists NewEnum::exists(); // To drop the enum type NewEnum::drop();
Examples
Gender codes:
class GenderCode extends \tigrov\pgsql\enum\EnumBehavior { /** * @var array list of attributes that are to be automatically humanized value. * humanized => original attribute */ public $attributes = ['gender' => 'gender_code']; /** * Values of genders * @param bool $withEmpty with empty value at first * @return array */ public static function values($withEmpty = false) { return ($withEmpty ? ['' => static::emptyValue()] : []) + [ 'M' => \Yii::t('enum', 'Male'), 'F' => \Yii::t('enum', 'Female'), ]; } } class Model extends \yii\db\ActiveRecord { public function behaviors() { return [ GenderCode::class, ]; } } $model->gender_code = 'M'; $model->gender === 'Male'; // or translated value
Messenger names:
class MessengerType extends \tigrov\pgsql\enum\EnumBehavior { /** @var array list of attributes that are to be automatically humanized value */ public $attributes = ['type' => 'type_key']; /** * Values of Messengers * @param bool $withEmpty with empty value at first * @return array */ public static function values($withEmpty = false) { return ($withEmpty ? ['' => static::emptyValue()] : []) + [ 'skype' => 'Skype', 'whatsapp' => 'WhatsApp', 'viber' => 'Viber', 'facebook' => 'Facebook', 'imessage' => 'iMessage', 'telegram' => 'Telegram', 'line' => 'Line', 'jabber' => 'Jabber', 'qq' => 'QQ', 'blackberry' => 'BlackBerry', 'aim' => 'AIM', 'ebuddy' => 'eBuddy', 'yahoo' => 'Yahoo', 'other' => \Yii::t('enum', 'Other'), ]; } } $model->type_key = 'whatsapp'; $model->type === 'WhatsApp';
Translate values:
class TranslatableType extends \tigrov\pgsql\enum\EnumBehavior { public static $messageCategory = 'app'; } // $model->type is translated value // TranslatableType::values() are all translated values
Type values as constants:
class Status extends \tigrov\pgsql\enum\EnumBehavior { const ACTIVE = 'active'; const PENDING = 'pending'; const REJECTED = 'rejected'; const DELETED = 'deleted'; public $attributes = ['status' => 'status_key']; public static $messageCategory = 'status'; } // Do not forget to initialize the enum type in the database // E.g. Status::create(); $model->status_key = Status::PENDING; $model->status === 'Pending'; // or translated value
License
tigrov/yii2-pgsql-enum 适用场景与选型建议
tigrov/yii2-pgsql-enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.01k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「postgresql」 「extension」 「pgsql」 「postgres」 「enum」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tigrov/yii2-pgsql-enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tigrov/yii2-pgsql-enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tigrov/yii2-pgsql-enum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SilverStripe now has tentative support for PostgreSQL ('Postgres')
Dibi is Database Abstraction Library for PHP
Async Reactive Postgres Driver for PHP (Non-blocking)
A custom URL rule class for Yii 2 which allows to create translated URL rules
Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
统计信息
- 总下载量: 5.01k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-13