https://github.com/heymoon-cc/doctrine-psql-enum
Doctrine migrations for PostgreSQL enum
https://github.com/heymoon-cc/doctrine-psql-enum
doctrine-orm postgresql
Last synced: about 1 month ago
JSON representation
Doctrine migrations for PostgreSQL enum
- Host: GitHub
- URL: https://github.com/heymoon-cc/doctrine-psql-enum
- Owner: heymoon-cc
- License: mit
- Created: 2023-09-24T12:35:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2026-02-15T11:03:27.000Z (about 2 months ago)
- Last Synced: 2026-02-15T16:29:59.774Z (about 2 months ago)
- Topics: doctrine-orm, postgresql
- Language: PHP
- Homepage: https://packagist.org/packages/heymoon/doctrine-psql-enum
- Size: 55.7 KB
- Stars: 23
- Watchers: 1
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Doctrine enums for PostgreSQL
[](https://packagist.org/packages/heymoon/doctrine-psql-enum)
[](https://packagist.org/packages/heymoon/doctrine-psql-enum)
[](https://github.com/heymoon-cc/doctrine-psql-enum/actions/workflows/test.yaml)
[](https://qlty.sh/gh/heymoon-cc/projects/doctrine-psql-enum)
[](https://qlty.sh/gh/heymoon-cc/projects/doctrine-psql-enum)
[](https://matrix.to/#/#doctrine-psql-enum:heymoon.cc)
## Prerequisites: *Symfony 7 + Doctrine 3*
### Installation
`composer require heymoon/doctrine-psql-enum`
### Usage
Create library configuration:
`config/packages/doctrine_postgres_enum.yaml`
```yaml
doctrine_postgres_enum:
type_name: enum
migrations:
enabled: true
comment_tag: DC2Enum
```
For defining new enum type, [use native PHP enums](https://www.php.net/manual/language.types.enumerations.php):
```php
use HeyMoon\DoctrinePostgresEnum\Attribute\EnumType;
#[EnumType('auth_status')]
enum AuthStatus: string
{
case New = 'new';
case Active = 'active';
case Inactive = 'inactive';
case Deleted = 'deleted';
}
#[EnumType('auth_service')]
enum Service: string
{
case Google = 'google';
}
```
For creation of enum-field in model, use `enum` as `type` value, `enumType` in `Column` attribute must be defined:
```php
#[ORM\Entity(repositoryClass: AuthRepository::class)]
class Auth
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(class: "doctrine.uuid_generator")]
#[ORM\Column(type: 'uuid')]
private Uuid $id;
#[ORM\Column(type: 'enum', enumType: AuthStatus::class)]
private AuthStatus $status;
#[ORM\Column(type: 'enum', enumType: Service::class)]
private Service $service;
}
```
Create migrations via `make:migration`. If enum was created or modified, the `CREATE TYPE`/`ALTER TYPE` calls would be added to migration. Example:
```php
$this->addSql('DROP TYPE IF EXISTS auth_status');
$this->addSql('CREATE TYPE auth_status AS ENUM (\'new\',\'active\',\'inactive\',\'deleted\')');
$this->addSql('DROP TYPE IF EXISTS auth_service');
$this->addSql('CREATE TYPE auth_service AS ENUM (\'google\')');
$this->addSql('CREATE TABLE auth (id UUID NOT NULL, status auth_status NOT NULL, service auth_service NOT NULL, PRIMARY KEY(id))');
```