php-arsenal/doctrine-odm-enum-type-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Add custom enum mapping types for your documents.

0.1.1 2021-06-15 12:01 UTC

This package is auto-updated.

Last update: 2021-09-07 20:17:55 UTC


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

Release CI Packagist

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;
    }
}