art-fatal/doctrine-enum-bundle
A Symfony bundle that provides automatic registration of PHP 8.1+ BackedEnum as Doctrine DBAL custom types with MySQL ENUM columns.
make:enum command to generate enums and typesEnumType and you're doneDayOfWeekcomposer require art-fatal/doctrine-enum-bundle
If you're not using Symfony Flex, add the bundle to config/bundles.php:
return [
// ...
ArtFatal\DoctrineEnumBundle\DoctrineEnumBundle::class => ['all' => true],
];
The bundle includes production-ready enums that you can use directly without creating your own.
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')
You can create your own custom enums in two ways:
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:
UserStatus, OrderState)NAME=value (e.g., ACTIVE=active, PENDING=pending)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 classsrc/Type/UserStatusEnumType.php - The Doctrine typeIf you prefer manual creation:
// 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,
];
}
// 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:
user_status)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;
}
}
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;
DoctrineEnumExtension auto-tags all classes extending EnumTypeRegisterEnumTypesPass registers all tagged enum types with Doctrine DBALEnumType handles conversion between PHP BackedEnum and database string valuesComplete, working examples are available in the examples/ directory:
See examples/README.md for detailed usage instructions.
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!";
}
services.yaml or Kernel.phpMIT License. See LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues, please open an issue on the GitHub repository.
How can I help you explore Laravel packages today?