Support for PHP 8.1 enums in Doctrine.

Overview

CI Workflow Coverage

Doctrine Native Enums

This library provides first-class support to PHP Enums, introduced in PHP 8.1, within your Doctrine entities.

Installation

composer require bentools/doctrine-native-enums

Usage

This library only works with Backed enums.

In a Symfony project

1. Declare the bundle.

// config/bundles.php

return [
    // ...
    BenTools\Doctrine\NativeEnums\Bundle\DoctrineNativeEnumsBundle::class => ['all' => true],
];

2. Register enums in your configuration.

# config/packages/doctrine_native_enums.yaml

doctrine_native_enums:
  enum_types:
    App\Entity\StatusEnum: ~
    #App\Entity\StatusEnum: status # Alternatively, if you want your type to be named "status"

3. Use them in your entities.

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
final class Book
{
    #[
        ORM\Id,
        ORM\Column(unique: true),
        ORM\GeneratedValue(strategy: 'AUTO'),
    ]
    public int $id;

    #[ORM\Column]
    public string $name;

    #[ORM\Column(type: StatusEnum::class)]
    public StatusEnum $status;
}

In other projects using Doctrine

use App\Entity\StatusEnum;
use BenTools\Doctrine\NativeEnums\Type\NativeEnum;
use Doctrine\DBAL\Types\Type;

NativeEnum::registerEnumType(StatusEnum::class);
// NativeEnum::registerEnumType('status', StatusEnum::class); // Alternatively, if you want your type to be named "status"

Tests

php vendor/bin/pest

License

MIT.

You might also like...
Doctrine ORM Module for Laminas

Doctrine ORM Module for Laminas The DoctrineORMModule leverages DoctrineModule and integrates Doctrine ORM with Laminas quickly and easily. The follow

Symfony Bundle to create HTML tables with bootstrap-table for Doctrine Entities.

HelloBootstrapTableBundle This Bundle provides simple bootstrap-table configuration for your Doctrine Entities. Used bootstrap-table version 1.18.3. I

Dockerise Symfony Application (Symfony 6 + Clean Architecture+ DDD+ CQRS + Docker + Xdebug + PHPUnit + Doctrine ORM + JWT Auth + Static analysis)

Symfony Dockerise Symfony Application Install Docker Install Docker Compose Docker PHP & Nginx Create Symfony Application Debugging Install Xdebug Con

Doctrine adapter for SlmQueue module

SlmQueueDoctrine Created by Stefan Kleff Requirements SlmQueue Doctrine 2 ORM Module or roave/psr-container-doctrine Note: it's necessary require the

The Assure Alliance support website. This website is based on Questions2Answers and is a forum for support using Biblical Tools

The Assure Alliance support website. This website is based on Questions2Answers and is a forum for support using Biblical Tools

Pug-php adds inline PHP scripting support to the Pug template compiler

Pug-php adds inline PHP scripting support to the Pug template compiler. Since version 3, it uses Phug, a very customizable Pug template engine made by the tale-pug and pug-php developers as the new PHP Pug engine reference.

PHP library providing retry functionality with multiple backoff strategies and jitter support

PHP Backoff Easily wrap your code with retry functionality. This library provides: 4 backoff strategies (plus the ability to use your own) Optional ji

A GETTR.com client library written in PHP with Laravel support.
A GETTR.com client library written in PHP with Laravel support.

Gettr API Clinet PHP A GETTR.com client library written in PHP with Laravel support. This library uses unofficial publicly accessible API endpoints of

Comments
  • Error when creating database

    Error when creating database

    Thanks for creating and sharing this repo!

    I'm trying this on Symfony 5.4 with PHP 8.1. While the application seems to work, I've got problems in my test suite. This is what I get:

    
    In MySQLPlatform.php line 1177:
                                                                                                               
      str_replace(): Argument #3 ($subject) must be of type array|string, App\Model\Enum\DocumentStatus given  
                                                                                                              
    

    It's the \Doctrine\DBAL\Platforms\MySQLPlatform::quoteStringLiteral method.

    Any idea how we can fix this?

    opened by stephanvierkant 3
  • Deprecated: Method

    Deprecated: Method "Doctrine\DBAL\Types\Type::convertToDatabaseValue()" might add "mixed" as a native return type declaration in the future

    The following deprecation warnings are reported by PHP when using the library:

    User Deprecated: Method "Doctrine\DBAL\Types\Type::convertToDatabaseValue()"
    might add "mixed" as a native return type declaration in the future. Do the 
    same in child class "BenTools\Doctrine\NativeEnums\Type\NativeEnum" now to 
    avoid errors or add an explicit @return annotation to suppress this message.
    
    User Deprecated: Method "Doctrine\DBAL\Types\Type::requiresSQLCommentHint()"
    might add "bool" as a native return type declaration in the future. Do the
    same in child class "BenTools\Doctrine\NativeEnums\Type\NativeEnum" now to
    avoid errors or add an explicit @return annotation to suppress this message.
    

    Adding the return types shouldn't cause any trouble, I suppose.

    opened by Radiergummi 1
  • Generate diff migration always dump Enum columns

    Generate diff migration always dump Enum columns

    Example :

    Creating the following initial table third_party :

            'CREATE TABLE third_party (
                  id SERIAL NOT NULL, 
                  name VARCHAR(255) NOT NULL, 
                  type VARCHAR(255) NOT NULL, 
                  address VARCHAR(1024) DEFAULT NULL, 
                  post_code VARCHAR(10) DEFAULT NULL, 
                  city VARCHAR(128) DEFAULT NULL, 
                  country VARCHAR(255) NOT NULL, 
                  PRIMARY KEY(id))
              ');
    

    Where type and country are both of them BackedEnum. If I execute this migration, and regenarate a diff migration, it will always reconfigure this 2 columns definition :

         ALTER TABLE third_party ALTER type TYPE VARCHAR(255);
         ALTER TABLE third_party ALTER type DROP DEFAULT;
         ALTER TABLE third_party ALTER country TYPE VARCHAR(255);
         ALTER TABLE third_party ALTER country DROP DEFAULT;
    

    By searching on the migrations packages, the changes seems to be detected like this (for country) :

    Before change :

    ^ Doctrine\DBAL\Schema\Column^ {#347
      #_name: "country"
      #_namespace: null
      #_quoted: false
      #_type: Doctrine\DBAL\Types\StringType^ {#123}
      #_length: 255
      #_precision: 10
      #_scale: 0
      #_unsigned: false
      #_fixed: false
      #_notnull: true
      #_default: null
      #_autoincrement: false
      #_platformOptions: []
      #_columnDefinition: null
      #_comment: null
      #_customSchemaOptions: []
    }
    

    After change :

    Doctrine\DBAL\Schema\Column^ {#302
      #_name: "country"
      #_namespace: null
      #_quoted: false
      #_type: BenTools\Doctrine\NativeEnums\Type\NativeEnum^ {#115
        -name: "App\Enum\CountryEnum"
        -class: "App\Enum\CountryEnum"
        -type: BenTools\Doctrine\NativeEnums\Type\BackedEnumType^ {#116
          +name: "STRING"
        }
      }
      #_length: null
      #_precision: 10
      #_scale: 0
      #_unsigned: false
      #_fixed: false
      #_notnull: true
      #_default: null
      #_autoincrement: false
      #_platformOptions: array:1 [
        "version" => false
      ]
      #_columnDefinition: null
      #_comment: null
      #_customSchemaOptions: []
    }
    

    I'm trying to find a solution right now.

    opened by jonbk 0
Releases(0.1.2)
  • 0.1.2(Jan 31, 2022)

    What's Changed

    • Fix: type hint by @bpolaszek in https://github.com/bpolaszek/doctrine-native-enums/pull/7
    • Docs: Add deprecation notice by @bpolaszek in https://github.com/bpolaszek/doctrine-native-enums/pull/8

    Full Changelog: https://github.com/bpolaszek/doctrine-native-enums/compare/0.1.1...0.1.2

    Source code(tar.gz)
    Source code(zip)
  • 0.1.1(Jan 4, 2022)

    What's Changed

    • CI: try with latest doctrine/dbal version by @bpolaszek in https://github.com/bpolaszek/doctrine-native-enums/pull/3
    • Put comment hint on SQL columns for Enum type by @jonbk in https://github.com/bpolaszek/doctrine-native-enums/pull/5

    New Contributors

    • @jonbk made their first contribution in https://github.com/bpolaszek/doctrine-native-enums/pull/5

    Full Changelog: https://github.com/bpolaszek/doctrine-native-enums/compare/0.1...0.1.1

    Source code(tar.gz)
    Source code(zip)
Owner
Beno!t POLASZEK
Web Developer: Symfony Api-Platform VueJS Mercure TailwindCSS
Beno!t POLASZEK
Helpers for making PHP enums more lovable.

Enums A collection of enum helpers for PHP. InvokableCases Names Values Options From Metadata You can read more about the idea on Twitter. I originall

ARCHTECH 212 Dec 22, 2022
A collection of standards as PHP Enums: ISO3166, ISO4217, ISO639...

Standards A collection of standards as PHP Enums Setup Make sure you are running PHP 8.1 or higher to use this package To start right away, run the fo

null 295 Dec 20, 2022
This library can be used, among other things, to retrieve the classes, interfaces, traits, enums, functions and constants declared in a file

marijnvanwezel/reflection-file Library that allows reflection of files. This library can be used, among other things, to retrieve the classes, interfa

Marijn van Wezel 5 Apr 17, 2022
Immutable value object for IPv4 and IPv6 addresses, including helper methods and Doctrine support.

IP is an immutable value object for (both version 4 and 6) IP addresses. Several helper methods are provided for ranges, broadcast and network address

Darsyn 224 Dec 28, 2022
Rector upgrades rules for Doctrine

Rector Rules for Doctrine See available Doctrine rules Install This package is already part of rector/rector package, so it works out of the box.

Rector 37 Nov 7, 2022
Pageon Doctrine Data Grid Bundle

Pageon Doctrine Data Grid Bundle A bundle that wraps around the knp paginator bundle and doctrine to generate a data grid from your entity Documentati

null 1 Dec 14, 2021
Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer

Spot DataMapper ORM v2.0 Spot v2.x is built on the Doctrine DBAL, and targets PHP 5.4+. The aim of Spot is to be a lightweight DataMapper alternative

Spot ORM 602 Dec 27, 2022
A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrine lifecycle events.

SpecShaper Encrypt Bundle A bundle to handle encoding and decoding of parameters using OpenSSL and Doctrine lifecycle events. Features include: Master

Mark Ogilvie 48 Nov 4, 2022
Doctrine extensions for PHPStan

Doctrine extensions for PHPStan PHPStan Doctrine This extension provides following features: DQL validation for parse errors, unknown entity classes a

PHPStan 478 Jan 3, 2023
This package provides a set of factories to be used with containers using the PSR-11 standard for an easy Doctrine integration in a project

psr-container-doctrine: Doctrine Factories for PSR-11 Containers Doctrine factories for PSR-11 containers. This package provides a set of factories to

Roave, LLC 84 Dec 14, 2022