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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,10 @@ private function validateAndCompleteTypedFieldMapping(array $mapping): array
case 'string':
$mapping['type'] = Types::STRING;
break;
default:
if (PHP_VERSION_ID >= 80100 && enum_exists($type->getName())) {
$mapping['type'] = Types::ENUM;
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Doctrine/Tests/Models/TypedProperties/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\TypedProperties;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity, ORM\Table(name: 'cms_articles_enumed')]
class Article
{
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
public int $id;

#[ORM\Column]
public ArticleStateEnum $enum;
}
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/Models/TypedProperties/ArticleStateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

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.

{
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 😅 .

15 changes: 14 additions & 1 deletion tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function testFieldIsNullableByType(): void
public function testFieldTypeFromReflection(): void
{
if (PHP_VERSION_ID < 70400) {
self::markTestSkipped('requies PHP 7.4');
self::markTestSkipped('requires PHP 7.4');
}

$cm = new ClassMetadata(TypedProperties\UserTyped::class);
Expand Down Expand Up @@ -174,6 +174,19 @@ public function testFieldTypeFromReflection(): void
self::assertEquals('float', $cm->getTypeOfField('float'));
}

public function testFieldTypeEnumFromReflection(): void
{
if (PHP_VERSION_ID < 80100) {
self::markTestSkipped('requires PHP 8.1');
}

$cm = new ClassMetadata(TypedProperties\Article::class);
$cm->initializeReflection(new RuntimeReflectionService());

$cm->mapField(['fieldName' => 'enum']);
self::assertEquals('enum', $cm->getTypeOfField('enum'));
}

/**
* @group DDC-115
*/
Expand Down