riccardonar/doctrine-intrange

Supports postgres intrange in Doctrine DBAL.

1.0.0 2017-01-26 13:42 UTC

This package is auto-updated.

Last update: 2024-10-23 20:16:20 UTC


README

Thanks to @Salamek, this is an adapt to https://github.com/Salamek/doctrine-daterange

Supports PostgreSQL DateRange in Doctrine DBAL.

Summary

The IntRange library

  • adds a intrange type to DBAL

Installation

Add it to your list of Composer dependencies:

composer require riccardonar/doctrine-intrange

Register it with Doctrine DBAL:

<?php

use Doctrine\DBAL\Types\Type;
use riccardonar\Doctrine\DBAL\Types\IntRangeType;

Type::addType(
    IntRangeType::INTRANGE,
    'riccardonar\\Doctrine\\DBAL\\Types\\IntRangeType'
);

When using Symfony2 with Doctrine you can do the same as above by only changing your configuration:

# app/config/config.yml

# Doctrine Configuration
doctrine:
    dbal:
        # ...
        mapping_types:
            intrange: intrange
        types:
            intrange:  riccardonar\Doctrine\DBAL\Types\IntRangeType

Usage

<?php

/**
 * @Entity()
 * @Table(name="jobs")
 */
class Job
{
    /**
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     * @Id()
     */
    private $id;

    /**
     * @Column(type="intrange")
     */
    private $range;

    /**
     * @return \riccardonar\IntRange
     */
    public function getRange()
    {
        return $this->range;
    }

    /**
     * @param \riccardonar\IntRange $range
     */
    public function setRange(array $range)
    {
        $this->range = $range;
    }
}

$annualJob = new Job();
$annualJob->setRange(new \riccardonar\IntRange(1, 6));

$entityManager->persist($annualJob);
$entityManager->flush();
$entityManager->clear();

$jobs = $entityManager->createQuery(
    "SELECT j FROM Jobs j"
)->getResult();

echo $jobs[0]->getRange()->getStartInt(); // 1
echo $jobs[0]->getRange()->getEndInt(); //  6