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

Detect enums and use type string (PHP 8.1 only) #9149

Closed
wants to merge 1 commit into from

Conversation

Nek-
Copy link
Contributor

@Nek- Nek- commented Oct 25, 2021

This is a patch for #9021 .

Edit: test added.

See doctrine/dbal#4930

@Nek- Nek- changed the title Detect enums and use type string Detect enums and use type string (PHP 8.1 only) Oct 25, 2021
@derrabus derrabus marked this pull request as draft October 25, 2021 09:41
@derrabus
Copy link
Member

Converting to draft because tests are missing.

About the tests I am sorry I didn't find out how to test something only for a specific version of PHP.

https://phpunit.readthedocs.io/en/9.5/annotations.html#requires

This is a patch for #9021 .

I doubt that your change really solves the problem completely, but let's write some functional tests first an iterate from there.

@derrabus derrabus added this to the 2.11.0 milestone Oct 25, 2021
This commit does not contains a test because AFAIK for the moment there
is no test scoped to a specific version of PHP.

It fixes doctrine#9021
@Nek- Nek- force-pushed the feature/enum-type-detection branch from 81abd91 to 24e29f1 Compare October 25, 2021 21:15
@Nek-
Copy link
Contributor Author

Nek- commented Oct 25, 2021

@derrabus I think it's better like this indeed :) . And the rest should be inside the dbal! On it!

{
case DRAFT;
case PUBLISHED;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for that, the current version of phpcs have issues with enums 😅 .


namespace Doctrine\Tests\Models\TypedProperties;

enum ArticleStateEnum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should support backed enums only.

Copy link
Contributor Author

@Nek- Nek- Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Everything will work like this. I do not understand this limitation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit enums do not specify a way to convert them from or to a scalar value. This is the reason why backed enums exist in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about creating a Type for each enum type. A special serialization could be done for Unit enums making it easier to query. Maybe a json field.

I think all of this leads to more code but really seems fine to me. WDYT? (if you valid I go on this)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. If someone wants to hydrate/dehydrate enums from/to the database, they can back it with scalar values for that purpose. That's why backed enums exist.

For unbacked enums, we need a custom conversion logic and in that case, it would be easier if the developer wrote their own DBAL type for that.

Nek- added a commit to Nek-/dbal that referenced this pull request Oct 25, 2021
Nek- added a commit to Nek-/dbal that referenced this pull request Oct 25, 2021
@derrabus
Copy link
Member

Closing as explained in doctrine/dbal#4930.

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

Successfully merging this pull request may close these issues.

2 participants