Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Add custom enum mapping types for your MongoDB documents in Symfony

License

Notifications You must be signed in to change notification settings

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

doctrine-odm-enum-type-bundle

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

About

Add custom enum mapping types for your MongoDB documents in Symfony

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages