Open SirMishaa opened 8 months ago
@SirMishaa Hi. Maybe this article could help you https://www.doctrine-project.org/2022/01/11/orm-2.11.html The approach is different, now Doctrine supports native PHP enums out of box
The reason we wanted to use PHP enums was better reason about the value in our application logic and the reason to keep using DoctrineEnumBundle was to enforce the value in the database.
We've had success with enums and DoctrineEnumBundle.
Here is our implementation. We have an enum for Review types.
enum ReviewEnum: string
{
case WRITTEN = 'written';
case PHONE_INTERVIEW = 'phone_interview';
}
And we reference the enum in the Type.
final class ReviewType extends AbstractEnumType
{
protected static array $choices = [
ReviewEnum::WRITTEN->value => 'Written',
ReviewEnum::PHONE_INTERVIEW->value => 'Phone interview',
];
}
#[ORM\Column(type: ReviewType::class, nullable: false, enumType: ReviewEnum::class)]
#[PhpEnumType(entity: ReviewType::class)]
private ReviewEnum $type;
public function getType(): ReviewEnum
{
return $this->type;
}
public function setType(ReviewEnum|string $type): self
{
if (is_string($type)) {
$type = ReviewEnum::from($type);
}
$this->type = $type;
return $this;
}
setType()
accepts strings to make it easier to load fixtures when testing.
However, the EnumTypeValidator
will not work with php enums, so we need to wrap it to allow it to convert the enum into a string.
namespace App\Validator\Constraints;
use Fresh\DoctrineEnumBundle\Exception\RuntimeException;
use Fresh\DoctrineEnumBundle\Validator\Constraints\EnumType;
use Fresh\DoctrineEnumBundle\Validator\Constraints\EnumTypeValidator;
use Symfony\Component\Validator\Constraint;
class PhpEnumTypeValidator extends EnumTypeValidator
{
/**
* @throws RuntimeException
*/
public function validate(mixed $value, Constraint|EnumType $constraint): void
{
if (!$constraint instanceof EnumType) {
throw new RuntimeException(\sprintf('Object of class %s is not instance of %s', \get_class($constraint), EnumType::class));
}
$constraint->choices = $constraint->entity::getValues();
if ($value instanceof \BackedEnum) {
$value = $value->value;
}
parent::validate($value, $constraint);
}
}
and
namespace App\Validator\Constraints;
use Fresh\DoctrineEnumBundle\Validator\Constraints\EnumType;
/**
* Required to activate PhpEnumTypeValidator.
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class PhpEnumType extends EnumType
{
}
@fre5h If you could incorporate the unwrapping of the enum in the validator, then this bundle would support php enums.
@hackzilla thanks for advice, I'll consider this 👍
Hello,
According this example :
I wonder why we don't use PHP's native enumerations? I have this enum, and I'd want to use as a type :
Regards ❤️