php-arsenal / doctrine-odm-enum-type-bundle
Add custom enum mapping types for your documents.
Installs: 17
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.0
- doctrine/mongodb-odm-bundle: ^4.3
- myclabs/php-enum: ^1.8
- symfony/dependency-injection: ^5.3
- symfony/http-kernel: ^5.3
README
Add custom enum mapping types for your MongoDB documents in Symfony. Inspired by doctrine-enum-type and based on php-enum.
composer require php-arsenal/doctrine-odm-enum-type-bundle
How it works?
Enum
classes that extend MyCLabs\Enum\Enum
are automatically added as valid Doctrine types.
<?php namespace YourNamespace\Enum; use MyCLabs\Enum\Enum; final class ActionEnum extends Enum { private const CREATE = 'create'; private const READ = 'read'; private const UPDATE = 'update'; private const DELETE = 'delete'; }
You can set the type as Enum
class. Upon saving or retrieving from the database it will be converted to and back the Enum
class.
<?php namespace YourNamespace\Entity; use YourNamespace\Enum\ActionEnum; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; /** * @ORM\Document() */ class User { /** * @ODM\Field(type=ActionEnum::class, name="action") */ protected $action; public function __construct() { $this->action = ActionEnum::CREATE(); } public function getAction(): ActionEnum { return $this->action; } public function setAction(ActionEnum $action): void { $this->action = $action; } }