PHPackages                             art-fatal/doctrine-enum-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. art-fatal/doctrine-enum-bundle

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

art-fatal/doctrine-enum-bundle
==============================

Symfony bundle for automatic registration of PHP BackedEnum as Doctrine DBAL custom types with MySQL ENUM columns

v1.1.1(6mo ago)03MITPHPPHP &gt;=8.1

Since Oct 31Pushed 6mo agoCompare

[ Source](https://github.com/art-fatal/doctrine-enum-bundle)[ Packagist](https://packagist.org/packages/art-fatal/doctrine-enum-bundle)[ RSS](/packages/art-fatal-doctrine-enum-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (4)Used By (0)

Doctrine Enum Bundle
====================

[](#doctrine-enum-bundle)

A Symfony bundle that provides automatic registration of PHP 8.1+ BackedEnum as Doctrine DBAL custom types with MySQL ENUM columns.

Features
--------

[](#features)

- **Zero Configuration**: Auto-registers all enum types with Doctrine
- **Code Generator**: Interactive `make:enum` command to generate enums and types
- **Type-Safe**: Uses native PHP BackedEnum
- **MySQL ENUM Support**: Generates proper MySQL ENUM columns
- **Easy to Use**: Just extend `EnumType` and you're done
- **Built-in Enums**: Includes ready-to-use enums like `DayOfWeek`
- **Symfony 6/7 Compatible**: Works with modern Symfony versions

Installation
------------

[](#installation)

```
composer require art-fatal/doctrine-enum-bundle
```

### Manual Bundle Registration (Symfony without Flex)

[](#manual-bundle-registration-symfony-without-flex)

If you're not using Symfony Flex, add the bundle to `config/bundles.php`:

```
return [
    // ...
    ArtFatal\DoctrineEnumBundle\DoctrineEnumBundle::class => ['all' => true],
];
```

Built-in Enums
--------------

[](#built-in-enums)

The bundle includes production-ready enums that you can use directly without creating your own.

### DayOfWeek

[](#dayofweek)

A complete implementation of days of the week with helpful utility methods.

**Usage in Entity:**

```
use ArtFatal\DoctrineEnumBundle\Enum\DayOfWeek;
use ArtFatal\DoctrineEnumBundle\Type\DayOfWeekEnumType;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Schedule
{
    // You can use either the NAME constant or string literal
    #[ORM\Column(type: DayOfWeekEnumType::NAME)]
    // OR
    // #[ORM\Column(type: 'day_of_week')]
    private ?DayOfWeek $dayOfWeek = null;

    public function getDayOfWeek(): ?DayOfWeek
    {
        return $this->dayOfWeek;
    }

    public function setDayOfWeek(?DayOfWeek $dayOfWeek): self
    {
        $this->dayOfWeek = $dayOfWeek;
        return $this;
    }
}
```

**Available Methods:**

```
// Set a day
$schedule->setDayOfWeek(DayOfWeek::MONDAY);

// Check if it's a weekend
if ($schedule->getDayOfWeek()->isWeekend()) {
    echo "It's the weekend!"; // true for Saturday/Sunday
}

// Check if it's a weekday
if ($schedule->getDayOfWeek()->isWeekday()) {
    echo "It's a weekday!"; // true for Monday-Friday
}

// Get human-readable label
echo $schedule->getDayOfWeek()->label(); // "Monday"

// Get ISO-8601 day number
$number = DayOfWeek::MONDAY->toIsoNumber(); // 1

// Create from ISO-8601 day number
$day = DayOfWeek::fromIsoNumber(7); // DayOfWeek::SUNDAY

// Get all weekdays or weekend days
$weekdays = DayOfWeek::weekDays(); // [MONDAY, TUESDAY, ..., FRIDAY]
$weekend = DayOfWeek::weekendDays(); // [SATURDAY, SUNDAY]
```

**Database Column:**

```
ENUM('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday')
```

Custom Enums
------------

[](#custom-enums)

You can create your own custom enums in two ways:

### Quick Way: Using the Generator Command

[](#quick-way-using-the-generator-command)

The bundle includes a `make:enum` command that generates both the enum and type files interactively:

```
php bin/console make:enum
```

The command will ask you:

1. **Enum name** (e.g., `UserStatus`, `OrderState`)
2. **Enum cases** in format `NAME=value` (e.g., `ACTIVE=active`, `PENDING=pending`)
3. **Namespaces** for enum and type (defaults: `App\Enum` and `App\Type`)

**Example session:**

```
Enum name: UserStatus
Case #1: ACTIVE=active
Case #2: INACTIVE=inactive
Case #3: BANNED=banned
Case #4: [press enter to finish]

```

This will create:

- `src/Enum/UserStatus.php` - The enum class
- `src/Type/UserStatusEnumType.php` - The Doctrine type

### Manual Way: Create Files Yourself

[](#manual-way-create-files-yourself)

If you prefer manual creation:

### 1. Create a PHP Enum

[](#1-create-a-php-enum)

```
// src/Constant/Enum/User/Status.php
namespace App\Constant\Enum\User;

enum Status: string
{
    case ACTIVE = 'active';
    case INACTIVE = 'inactive';
    case BANNED = 'banned';

    const ALL = [
        self::ACTIVE,
        self::INACTIVE,
        self::BANNED,
    ];
}
```

### 2. Create a Doctrine Enum Type

[](#2-create-a-doctrine-enum-type)

```
// src/Type/Enum/UserStatusEnumType.php
namespace App\Type\Enum;

use App\Constant\Enum\User\Status;
use ArtFatal\DoctrineEnumBundle\Type\EnumType;

class UserStatusEnumType extends EnumType
{
    public const NAME = 'user_status';

    public static function getEnumsClass(): string
    {
        return Status::class;
    }
}
```

**About the NAME constant:**

- Define it to have a type-safe constant for attributes
- The value should be in snake\_case (e.g., `user_status`)
- If not defined, the name is auto-generated from the class name

### 3. Use in Your Entity

[](#3-use-in-your-entity)

You can reference the type name in two ways:

**Option 1: Using the NAME constant (recommended for refactoring safety)**

```
#[ORM\Column(type: UserStatusEnumType::NAME)]
private ?Status $status = null;
```

**Option 2: Using the string literal**

```
#[ORM\Column(type: 'user_status')]
private ?Status $status = null;
```

Both are equivalent, but the NAME constant provides IDE autocomplete and refactoring support.

**Full example:**

```
// src/Entity/User.php
namespace App\Entity;

use App\Constant\Enum\User\Status;
use App\Type\Enum\UserStatusEnumType;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity]
class User
{
    #[ORM\Column(type: 'user_status', options: ['default' => Status::ACTIVE->value])]
    #[Assert\NotNull]
    #[Assert\Choice(choices: Status::ALL)]
    private ?Status $status = Status::ACTIVE;

    public function getStatus(): ?Status
    {
        return $this->status;
    }

    public function setStatus(?Status $status): self
    {
        $this->status = $status;
        return $this;
    }
}
```

### 4. Generate Migration

[](#4-generate-migration)

```
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
```

The migration will create a proper MySQL ENUM column:

```
ALTER TABLE user ADD status ENUM('active', 'inactive', 'banned') DEFAULT 'active' NOT NULL;
```

How It Works
------------

[](#how-it-works)

1. **Extension**: The `DoctrineEnumExtension` auto-tags all classes extending `EnumType`
2. **Compiler Pass**: The `RegisterEnumTypesPass` registers all tagged enum types with Doctrine DBAL
3. **Type Conversion**: `EnumType` handles conversion between PHP BackedEnum and database string values
4. **SQL Generation**: Creates proper MySQL ENUM columns with all enum cases

Examples
--------

[](#examples)

Complete, working examples are available in the [`examples/`](examples/) directory:

- **DayOfWeek enum**: A real-world example with helper methods
- **Complete entity**: Shows integration with Doctrine entities
- **Step-by-step guide**: Detailed instructions for using the examples

See [`examples/README.md`](examples/README.md) for detailed usage instructions.

### Quick Example Preview

[](#quick-example-preview)

The `DayOfWeek` example includes useful helper methods:

```
enum DayOfWeek: string
{
    case MONDAY = 'monday';
    // ... other days

    public function isWeekend(): bool
    {
        return in_array($this, [self::SATURDAY, self::SUNDAY], true);
    }

    public function label(): string
    {
        return ucfirst($this->value); // "Monday"
    }
}
```

Used in an entity:

```
$schedule->setDayOfWeek(DayOfWeek::SATURDAY);
if ($schedule->getDayOfWeek()->isWeekend()) {
    echo "Weekend activity!";
}
```

Advantages
----------

[](#advantages)

- **No Manual Configuration**: No need to modify `services.yaml` or `Kernel.php`
- **Reusable**: Just install the bundle in any Symfony project
- **Type Safety**: Full IDE autocomplete and type checking with PHP enums
- **Database Constraints**: MySQL ENUM provides database-level validation
- **Clean Code**: Separation between enum definition, type definition, and entity usage

Requirements
------------

[](#requirements)

- PHP &gt;= 8.1
- Symfony 6.x or 7.x
- Doctrine DBAL 3.x or 4.x

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE) file for details.

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

Support
-------

[](#support)

If you encounter any issues, please open an issue on the GitHub repository.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance68

Regular maintenance activity

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

190d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/38f31d5e6ffb67365bbfaf6d9461261bf2f7e59901d57aa603d885d4cf6958b5?d=identicon)[art-fatal](/maintainers/art-fatal)

---

Top Contributors

[![art-fatal](https://avatars.githubusercontent.com/u/69078254?v=4)](https://github.com/art-fatal "art-fatal (6 commits)")

---

Tags

symfonyenummysqldoctrinedbalphp-enumbacked-enum

### Embed Badge

![Health badge](/badges/art-fatal-doctrine-enum-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/art-fatal-doctrine-enum-bundle/health.svg)](https://phpackages.com/packages/art-fatal-doctrine-enum-bundle)
```

###  Alternatives

[fresh/doctrine-enum-bundle

Provides support of ENUM type for Doctrine2 in Symfony applications.

4636.8M12](/packages/fresh-doctrine-enum-bundle)[sonata-project/doctrine-orm-admin-bundle

Integrate Doctrine ORM into the SonataAdminBundle

46117.7M155](/packages/sonata-project-doctrine-orm-admin-bundle)[doctrineencryptbundle/doctrine-encrypt-bundle

Encrypted symfony entity's by verified and standardized libraries

29415.1k](/packages/doctrineencryptbundle-doctrine-encrypt-bundle)[vasek-purchart/doctrine-date-time-immutable-types-bundle

Bundle integration of Doctrine DateTimeImmutable types for Symfony

1085.6k](/packages/vasek-purchart-doctrine-date-time-immutable-types-bundle)[okapon/doctrine-set-type-bundle

Provides support of MySQL SET type for Doctrine2 in Symfony2 applications.

11159.0k](/packages/okapon-doctrine-set-type-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
