doctrineum/float

Enumeration type for Doctrine - floats only

3.2.0 2019-03-01 11:00 UTC

This package is auto-updated.

Last update: 2024-10-29 04:52:55 UTC


README

Build Status Test Coverage License

About

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

##Usage

<?php

use Doctrine\ORM\Mapping as ORM;
use Doctrineum\Float\FloatEnum;

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

    /**
     * @var FloatEnum
     * @ORM\Column(type="float_enum")
     */
    private $height;
    
    public function __construct(FloatEnum $height)
    {
        $this->height = $height;
    }

    /**
     * @return FloatEnum
     */
    public function getHeight()
    {
        return $this->height;
    }
}

$tallMan = new Person(FloatEnum::getEnum(197.45));

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

/** @var Person[] $persons */
$canSitOnFrontSeat = $entityManager->createQuery(
    "SELECT p FROM Person p WHERE p.height >= 140"
)->getResult();

var_dump($canSitOnFrontSeat[0]->getHeight()->getValue()); // 197.45;

##Installation

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

composer require jaroslavtyc/doctrineum-float

Doctrine integration

Register new DBAL type:

<?php

use Doctrineum\Float\FloatEnumType;

FloatEnumType::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:
            float_enum: float_enum
        types:
            float_enum: Doctrineum\Float\FloatEnumType