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

Enum type fatal error when passing the enum class in Symfony 7.2.3 #1663

Open
d9beuD opened this issue Feb 14, 2025 · 4 comments · May be fixed by #1664
Open

Enum type fatal error when passing the enum class in Symfony 7.2.3 #1663

d9beuD opened this issue Feb 14, 2025 · 4 comments · May be fixed by #1664

Comments

@d9beuD
Copy link

d9beuD commented Feb 14, 2025

I created a new Symfony 7.2.3 project in a dockerized environment and created a Task entity using make:entity command. I just added a title property.

Then I created a TaskStatus enum like so:

# src/Entity/Enum/TaskStatus.php
<?php

namespace App\Entity\Enum;

enum TaskStatus: int
{
    case Pending = 0;
    case Done = 1;
}

Then, using make:entity again, I edit my Task entity and fill the form:

 Class name of the entity to create or update (e.g. FierceKangaroo):
 > task

 Your entity already exists! So let's add some new fields!

 New property name (press <return> to stop adding fields):
 > status

 Field type (enter ? to see all types) [string]:
 > enum

 Enum class:
 > App\\Entity\\Enum\\TaskStatus

When I press enter, a fatal error is thrown:

Fatal error: Cannot declare enum App\Entity\Enum\TaskStatus, because the name is already in use in /var/www/src/Entity/Enum/TaskStatus.php on line 5
21:26:22 CRITICAL  [php] Fatal Compile Error: Cannot declare enum App\Entity\Enum\TaskStatus, because the name is already in use ["exception" => Symfony\Component\ErrorHandler\Error\FatalError^ { …}]

At this point, what I understand is that the enum cannot exists before I try to add it to my entity. But if I delete it and try again, it now says:

[ERROR] Class "App\\Entity\\Enum\\TaskStatus" doesn't exist; please enter an existing full class name.   

By the way, I use a php:8.3.17-apache docker image. How am I supposed to use the enum type in my entities? I found nothing in Symfony's docs.

@Fan2Shrek
Copy link
Contributor

This is really weird, but it seems to be related to the double \ when entering the enum class. I'll take a look.

@Fan2Shrek
Copy link
Contributor

This seems to be related to a composer bug when loading classes with multiple backslashes. I propose a fix to Composer, but I'm not sure if we need to fix this here as well.

@Fan2Shrek Fan2Shrek linked a pull request Feb 19, 2025 that will close this issue
@Fan2Shrek
Copy link
Contributor

composer/composer#12320 was closed, so I opened #1664 to fix this :)

@d9beuD
Copy link
Author

d9beuD commented Feb 20, 2025

Thank you for your amazing work @Fan2Shrek.

I instinctively used \\ because it is often needed when working with strings, I didn't know that a single \ wouldn't be interpreted as a way to escape the next character in the make:entity command.

A sub-section in Creating an Entity Class documentation dedicated to enum usage may be a good idea I think. Because when you google for site:symfony.com enum there are results only from blog posts announcing backed enums support and EnumType Field. What do you think ? I can write it myself and make a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants