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

Array of enums not supported #320

Closed
ThomasLandauer opened this issue Apr 20, 2022 · 14 comments
Closed

Array of enums not supported #320

ThomasLandauer opened this issue Apr 20, 2022 · 14 comments

Comments

@ThomasLandauer
Copy link
Contributor

ThomasLandauer commented Apr 20, 2022

doctrine/orm 2.12.0 now supports arrays of enums, see doctrine/orm#9497

This is the syntax:

#[ORM\Column(type: Types::SIMPLE_ARRAY, length: 255, nullable: true, enumType: Foo::class)]
private array $foo = [];

phpstan is reporting:

Property App\Entity\User::$foo type mapping mismatch: backing type string of enum App\Enum\Foo does not match database type array.
Property App\Entity\User::$foo type mapping mismatch: database can contain App\Enum\Foo|null but property expects array.
Property App\Entity\User::$foo type mapping mismatch: property can contain array but database expects App\Enum\Foo|null.

@seb-jean
Copy link

I have the same problem

@jeffreymoelands
Copy link

Same problem here on doctrine/orm 2.14.1

@janklan
Copy link

janklan commented Sep 24, 2023

It also applies to other types:

/**
 * @var ViewSettingsEnum[]
 */
#[ORM\Column(type: 'json', nullable: true, enumType: ViewSettingsEnum::class)]
private ?array $viewSettings = null;

results in

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/Entity/Employee.php                                                                                                                                                                                            
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  96     Property App\Entity\Employee::$viewSettings type mapping mismatch: backing type string of enum App\Enum\ViewSettingsEnum does not match database type array|bool|float|int|JsonSerializable|stdClass|string|null.  
  96     Property App\Entity\Employee::$viewSettings type mapping mismatch: database can contain App\Enum\ViewSettingsEnum|null but property expects array<App\Enum\ViewSettingsEnum>|null.                                 
  96     Property App\Entity\Employee::$viewSettings type mapping mismatch: property can contain array<App\Enum\ViewSettingsEnum>|null but database expects App\Enum\ViewSettingsEnum|null.                                 
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

@amalricBzh
Copy link

Still present with doctrine/orm 3.1.3

@ServerExe
Copy link

Still present on doctrine/orm 3.2.0

@psprokofiev
Copy link

Still present...

"doctrine/orm": "3.2.1",
"phpstan/phpstan": "1.11.17",
"phpstan/phpstan-doctrine": "1.4.5",

@stof
Copy link
Contributor

stof commented Aug 23, 2024

should be fixed by #605 (except that the case described in the initial issue is affected by #436 as well, which is unrelated to enums)

@jeffreymoelands
Copy link

Is it correct that only simple_array is supported and json isn't?

@stof
Copy link
Contributor

stof commented Sep 9, 2024

Any DBAL type mapping to a PHP array is supported.
The json type will still report a type mismatch error because the DBAL type does not match to a PHP array (strings, numbers and booleans are also valid JSON values)

@Legendary4226
Copy link

Legendary4226 commented Sep 11, 2024

Still present with doctrine/orm 3.2.2 while "handling" a form

@ondrejmirtes
Copy link
Member

@Legendary4226 This is a closed and fixed issue. If you have any problems, open a new issue and describe your problem in full detail.

@ServerExe
Copy link

ServerExe commented Sep 20, 2024

Issue was fixed but a new one appeared which I can't fully understand. My previously ignored error:

Property App\Entity\EmailTemplate::$emailBccAddresses type mapping mismatch: backing type string of enum App\Enum\EmailBccAddress does not match database type array|bool|float|int|JsonSerializable|stdClass|string|null.

was replaced by

Property App\Entity\EmailTemplate::$emailBccAddresses type mapping mismatch: backing type string of enum App\Enum\EmailBccAddress does not match value type mixed of the database type array|bool|float|int|JsonSerializable|stdClass|string|null.

This is my entity property:

/**
 * @var EmailBccAddress[]|null
 */
#[ORM\Column(type: 'json', nullable: true, enumType: EmailBccAddress::class)]
private ?array $emailBccAddresses = null;

This is my Enum:

<?php
declare(strict_types=1);

namespace App\Enum;

enum EmailBccAddress: string
{
    case MY_VALUE = 'my_value';
}

@stof
Copy link
Contributor

stof commented Sep 20, 2024

@ServerExe the type json does not guarantee that the data will be an array of strings (nor a string). That's why this error is reported. The database value could be a string, a boolean, a number, or an array of those.

@ServerExe
Copy link

ServerExe commented Sep 20, 2024

I see. Sorry, I missed your comment at the top where you already described it! My bad. So, best solution would be to pick simple_array for that.

The only thing I am afraid of is: If enum types contain a comma in their string, this could break Doctrines simple_array logic because values are separated via comma in the column. That's the reason I chose JSON

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

No branches or pull requests

10 participants