doctrineum/integer

Enumeration type for Doctrine - integers only

4.0.0 2019-02-13 09:10 UTC

This package is auto-updated.

Last update: 2024-10-15 01:05:48 UTC


README

Build Status Test Coverage License

About

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

##Usage

<?php

use Doctrine\ORM\Mapping as ORM;
use Doctrineum\Integer\IntegerEnum;

/**
 * @ORM\Entity()
 */
class Journey
{
    /**
     * @var int
     * @ORM\Id() @ORM\GeneratedValue(strategy="AUTO") @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var IntegerEnum
     * @ORM\Column(type="integer_enum")
     */
    private $distanceInKm;
    
    public function __construct(IntegerEnum $distanceInKm)
    {
        $this->distanceInKm = $distanceInKm;
    }

    /**
     * @return IntegerEnum
     */
    public function getDistanceInKm()
    {
        return $this->distanceInKm;
    }
}

$toSun = new Journey(IntegerEnum::getEnum(152100000));

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

/** @var Journey[] $StarTracks */
$StarTracks = $entityManager->createQuery(
    "SELECT j FROM Journey j WHERE j.distanceInKm >= 1000000"
)->getResult();

var_dump($StarTracks[0]->getDistanceInKm()->getValue()); // 152100000;

##Installation

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

composer require jaroslavtyc/doctrineum-integer

Doctrine integration

Register new DBAL type:

<?php

use Doctrineum\Integer\IntegerEnumType;

IntegerEnumType::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:
            integer_enum: integer_enum
        types:
            integer_enum: Doctrineum\Integer\IntegerEnumType