granam/string-enum

Enumeration with strings only

1.1.0 2021-03-24 17:00 UTC

This package is auto-updated.

Last update: 2024-11-25 01:20:36 UTC


README

Build Status Test Coverage License

About

Adds Enum to Doctrine ORM (can be used as a @Column(type="string_enum")).

##Usage

<?php

use Doctrine\ORM\Mapping as ORM;
use Doctrineum\String\StringEnum;

/**
 * @ORM\Enity()
 */
class Person
{
    /**
     * @var int
     * @ORM\Id() @ORM\GeneratedValue(strategy="AUTO") @ORM\Column(type="integer")
     */
    private $id;
   
    /**
     * @var StringEnum
     * @ORM\Column(type="string_enum")
     */
    private $name;
    
    public function __construct(StringEnum $name)
    {
        $this->name = $name;
    }

    /**
     * @return StringEnum
     */
    public function getName()
    {
        return $this->name;
    }
}

// ... entity Money using Currency
$trueHero = new Person(StringEnum::getEnum('Don Quixote de La Mancha'));

/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager->persist($trueHero);
$entityManager->flush();
$entityManager->clear();

/** @var Currency[] $StarTracks */
$sirs = $entityManager->createQuery(
    "SELECT p FROM Person p WHERE p.name LIKE 'Don %'"
)->getResult();

var_dump($sirs[0]->getName()->getValue()); // 'Don Quixote de La Mancha';

##Installation

Add it to your list of Composer dependencies (or by manual edit your composer.json, the require section)

composer require jaroslavtyc/doctrineum-string

Doctrine integration

Register new DBAL type:

<?php

use Doctrineum\String\StringEnumType;

StringEnumType::registerSelf();

When using Symfony with Doctrine you can do the same as above by configuration:

# app/config/config.yml

# Doctrine Configuration
doctrine:
    dbal:
        # ...
        mapping_types:
            string_enum: string_enum
        types:
            string_enum: Doctrineum\String\StringEnumType