doctrineum/string

String-only enumeration type and value dependent sub-type for Doctrine

2.2.0 2019-03-01 11:04 UTC

This package is auto-updated.

Last update: 2024-11-29 05:15:00 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