{"id":17909623,"url":"https://github.com/tigrov/yii2-pgsql-enum","last_synced_at":"2026-02-16T13:04:13.166Z","repository":{"id":57069686,"uuid":"92879727","full_name":"Tigrov/yii2-pgsql-enum","owner":"Tigrov","description":"Enum type behavior and helper for Yii2, for PostgreSQL only","archived":false,"fork":false,"pushed_at":"2018-11-07T16:55:29.000Z","size":30,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-08T07:39:24.883Z","etag":null,"topics":["enum","pgsql","postgres","postgresql","yii2-extension"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tigrov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-30T21:57:09.000Z","updated_at":"2021-03-18T15:52:10.000Z","dependencies_parsed_at":"2022-08-24T14:54:18.347Z","dependency_job_id":null,"html_url":"https://github.com/Tigrov/yii2-pgsql-enum","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Tigrov/yii2-pgsql-enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tigrov%2Fyii2-pgsql-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tigrov%2Fyii2-pgsql-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tigrov%2Fyii2-pgsql-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tigrov%2Fyii2-pgsql-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tigrov","download_url":"https://codeload.github.com/Tigrov/yii2-pgsql-enum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tigrov%2Fyii2-pgsql-enum/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278909715,"owners_count":26066887,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["enum","pgsql","postgres","postgresql","yii2-extension"],"created_at":"2024-10-28T19:26:30.889Z","updated_at":"2025-10-08T07:39:25.520Z","avatar_url":"https://github.com/Tigrov.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"yii2-pgsql-enum\n==============\n\nEnum type behavior and helper for Yii2, for PostgreSQL only.\n\nUsing 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).\n\nSuch types are more difficult to maintain in a project. This extension helps to simplify the use of enum types. \n\n[![Latest Stable Version](https://poser.pugx.org/Tigrov/yii2-pgsql-enum/v/stable)](https://packagist.org/packages/Tigrov/yii2-pgsql-enum)\n[![Build Status](https://travis-ci.org/Tigrov/yii2-pgsql-enum.svg?branch=master)](https://travis-ci.org/Tigrov/yii2-pgsql-enum)\n\nLimitation\n----------\n\nSince 1.1.0 requires PHP \u003e= 5.5\n\nFor method `renameValue()` requires PostgreSQL \u003e= 10.0  \n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist tigrov/yii2-pgsql-enum\n```\n\nor add\n\n```\n\"tigrov/yii2-pgsql-enum\": \"~1.0\"\n```\n\nto the require section of your `composer.json` file.\n\n\t\nUsage\n-----\n\nOnce the extension is installed, you can create new enum types as follow:\n\n```php\nclass NewEnum extends \\tigrov\\pgsql\\enum\\EnumBehavior\n{\n    /** @var array list of attributes that are to be automatically humanized value */\n    public $attributes = ['type' =\u003e 'type_key'];\n}\n```\n\nCreate a table with the enum type\n```php\n// Create the enum in DB or in PHP code \nNewEnum::create(['value1', 'value2', 'value3']);\n\n\\Yii::$app-\u003egetDb()-\u003ecreateCommand()\n    -\u003ecreateTable('model', [\n       'id' =\u003e 'pk',\n       'type_key' =\u003e NewEnum::typeName(), // 'new_enum'\n   ])-\u003eexecute();\n```\n\nCreate a model for the table\n```php\nclass Model extends \\yii\\db\\ActiveRecord\n{\n    /**\n     * @inheritdoc\n     */\n    public function behaviors()\n    {\n        return [\n            NewEnum::class,\n            // 'type' =\u003e [\n            //    'class' =\u003e NewEnum::class,\n            //    'attributes' =\u003e ['type' =\u003e 'type_key'],\n            //],\n        ];\n    }\n    \n    /**\n     * @inheritdoc\n     */\n    public function save($runValidation = true, $attributeNames = null)\n    {\n        // You can add new values if they are not exists in the enum type\n        try {\n            return parent::save($runValidation, $attributeNames);\n        } catch (\\yii\\db\\Exception $e) {\n            NewEnum::add($this-\u003etype_key);\n            return parent::save($runValidation, $attributeNames);\n        }\n    }\n}\n```\n\nand then use them in your code\n```php\n/**\n * @var ActiveRecord $model\n */\n$model = new Model;\n$model-\u003etype_key = 'value1';\n$model-\u003esave();\n\n$newModel = Model::findOne($model-\u003eid);\n$newModel-\u003etype_key === 'value1';\n$newModel-\u003etype === 'Value1'; // see yii\\helpers\\Inflector::humanize($word, true)\n\n// The extension will try to add a new value if it does not exist\n$model-\u003etype_key = 'non-existent value';\n$model-\u003esave();\n\n// To get all enum values in [value =\u003e humanized value] array notation\nNewEnum::values();\n\n// To get all enum values without humanized values in [value1, value2, ...] array notation\nNewEnum::codes();\n\n// To add a new value to the enum type\nNewEnum::add('new value');\n\n// To get a humanized value\nNewEnum::value('new value'); // returns \"New Value\" or translated value\n\n// To check if the enum type has a value\nNewEnum::has('new value');\n\n// To rename a value of the enum type\nNewEnum::renameValue('new value', 'renamed value');\n\n// To remove a value from the enum type\nNewEnum::remove('renamed value');\n\n// To check if the enum type exists\nNewEnum::exists();\n\n// To drop the enum type\nNewEnum::drop();\n```\n\nExamples\n--------\n\nGender codes:\n```php\nclass GenderCode extends \\tigrov\\pgsql\\enum\\EnumBehavior\n{\n    /**\n     * @var array list of attributes that are to be automatically humanized value.\n     * humanized =\u003e original attribute\n     */\n    public $attributes = ['gender' =\u003e 'gender_code'];\n\n    /**\n     * Values of genders\n     * @param bool $withEmpty with empty value at first\n     * @return array\n     */\n    public static function values($withEmpty = false)\n    {\n        return ($withEmpty ? ['' =\u003e static::emptyValue()] : []) + [\n            'M' =\u003e \\Yii::t('enum', 'Male'),\n            'F' =\u003e \\Yii::t('enum', 'Female'),\n        ];\n    }\n}\n\nclass Model extends \\yii\\db\\ActiveRecord\n{\n    public function behaviors()\n    {\n        return [\n            GenderCode::class,\n        ];\n    }\n}\n\n$model-\u003egender_code = 'M';\n$model-\u003egender === 'Male'; // or translated value\n```\n\nMessenger names:\n```php\nclass MessengerType extends \\tigrov\\pgsql\\enum\\EnumBehavior\n{\n    /** @var array list of attributes that are to be automatically humanized value */\n    public $attributes = ['type' =\u003e 'type_key'];\n    \n    /**\n     * Values of Messengers\n     * @param bool $withEmpty with empty value at first\n     * @return array\n     */\n    public static function values($withEmpty = false)\n    {\n        return ($withEmpty ? ['' =\u003e static::emptyValue()] : []) + [\n            'skype' =\u003e 'Skype',\n            'whatsapp' =\u003e 'WhatsApp',\n            'viber' =\u003e 'Viber',\n            'facebook' =\u003e 'Facebook',\n            'imessage' =\u003e 'iMessage',\n            'telegram' =\u003e 'Telegram',\n            'line' =\u003e 'Line',\n            'jabber' =\u003e 'Jabber',\n            'qq' =\u003e 'QQ',\n            'blackberry' =\u003e 'BlackBerry',\n            'aim' =\u003e 'AIM',\n            'ebuddy' =\u003e 'eBuddy',\n            'yahoo' =\u003e 'Yahoo',\n            'other' =\u003e \\Yii::t('enum', 'Other'),\n        ];\n    }\n}\n\n$model-\u003etype_key = 'whatsapp';\n$model-\u003etype === 'WhatsApp';\n```\n\nTranslate values:\n```php\nclass TranslatableType extends \\tigrov\\pgsql\\enum\\EnumBehavior\n{\n    public static $messageCategory = 'app';\n}\n\n// $model-\u003etype is translated value\n// TranslatableType::values() are all translated values\n```\n\nType values as constants:\n```php\nclass Status extends \\tigrov\\pgsql\\enum\\EnumBehavior\n{\n    const ACTIVE = 'active';\n    const PENDING = 'pending';\n    const REJECTED = 'rejected';\n    const DELETED = 'deleted';\n\n    public $attributes = ['status' =\u003e 'status_key'];\n    public static $messageCategory = 'status';\n}\n\n// Do not forget to initialize the enum type in the database\n// E.g. Status::create();\n\n$model-\u003estatus_key = Status::PENDING;\n$model-\u003estatus === 'Pending'; // or translated value\n```\n\nLicense\n-------\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftigrov%2Fyii2-pgsql-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftigrov%2Fyii2-pgsql-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftigrov%2Fyii2-pgsql-enum/lists"}