Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PHP 8.1 Backed Enum's (casting to string or int) #5054

Closed
ThomasLandauer opened this issue Nov 30, 2021 · 3 comments
Closed

Support PHP 8.1 Backed Enum's (casting to string or int) #5054

ThomasLandauer opened this issue Nov 30, 2021 · 3 comments

Comments

@ThomasLandauer
Copy link
Contributor

Feature Request

Q A
New Feature yes
RFC yes https://www.php.net/manual/language.enumerations.php
BC Break no

Summary

Symfony 5.4 supports an EnumType Field in its forms: https://symfony.com/doc/5.4/reference/forms/types/enum.html It would be nice to be able to map this to an entity as-is. Right now, you need to manually transform the Enum object to its backed string (or int). So far, I see three ways to achieve this:

  1. Creating a Symfony Data Transformer: https://symfony.com/doc/5.4/form/data_transformers.html#creating-the-transformer
  2. Convert it in the entity's setter/getter:
    public function setMimetype(?MimetypeEnum $mimetype): self
    {
        $this->mimetype = $mimetype->value;
        return $this;
    }
    public function getMimetype(): ?MimetypeEnum
    {
        return Mimetype::from($this->mimetype);
    }
  3. Setting up a Custom Mapping Type in Doctrine: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types

So instead of having each user figure this out themselves, I'm suggesting to add two new types: enum_string and enum_int.

@derrabus
Copy link
Member

Duplicate of doctrine/orm#9021

@derrabus derrabus marked this as a duplicate of doctrine/orm#9021 Nov 30, 2021
@greg0ire
Copy link
Member

Relates to #5036

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants