tigrov / yii2-pgsql-enum
为 Yii2 提供枚举类型行为和辅助工具,仅适用于 PostgreSQL
1.4.0
2018-11-07 14:11 UTC
Requires
- php: >=5.5.0
- tigrov/yii2-enum: ~1.2
- yiisoft/yii2: ~2.0.0
README
为 Yii2 提供枚举类型行为和辅助工具,仅适用于 PostgreSQL。
使用枚举类型可以提供良好的数据可读性,而无需在存储重复值上额外花费资源(在任意字符串类型的情况下)。
这种类型在项目中维护较为困难。本扩展有助于简化枚举类型的使用。
限制
从 1.1.0 版本开始,需要 PHP >= 5.5
方法 renameValue()
需要 PostgreSQL >= 10.0
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist tigrov/yii2-pgsql-enum
或者
"tigrov/yii2-pgsql-enum": "~1.0"
将以下内容添加到你的 composer.json
文件的 require 部分。
使用方法
扩展安装完成后,你可以按照以下步骤创建新的枚举类型
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 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();
为该表创建一个模型
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); } } }
然后在你的代码中使用它们
/** * @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();
示例
性别代码
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
消息传递者名称
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';
翻译值
class TranslatableType extends \tigrov\pgsql\enum\EnumBehavior { public static $messageCategory = 'app'; } // $model->type is translated value // TranslatableType::values() are all translated values
将类型值作为常量
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