2017 © Pedro Peláez
 

library doctrine-dateinterval

Supports DateInterval in Doctrine DBAL and ORM.

image

herrera-io/doctrine-dateinterval

Supports DateInterval in Doctrine DBAL and ORM.

  • Thursday, March 19, 2015
  • by kherge
  • Repository
  • 1 Watchers
  • 6 Stars
  • 4,579 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 5 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Doctrine DateInterval Type

Build Status, (*1)

Supports DateInterval in Doctrine DBAL and ORM., (*2)

Summary

The DateInterval library, (*3)

  • adds a dateinterval type to DBAL
  • adds a DATE_INTERVAL DQL function to ORM

This is made possible by the DateInterval library., (*4)

Installation

Add it to your list of Composer dependencies:, (*5)

$ composer require herrera-io/doctrine-dateinterval=1.*

Register it with Doctrine DBAL:, (*6)

<?php

use Doctrine\DBAL\Types\Type;
use Herrera\Doctrine\DBAL\Types\DateIntervalType;

Type::addType(
    DateIntervalType::DATEINTERVAL,
    'Herrera\\Doctrine\\DBAL\\Types\\DateIntervalType'
);

Register it with Doctrine ORM:, (*7)

<?php

$entityManager->getConfiguration()->addCustomDatetimeFunction(
    'DATE_INTERVAL',
    'Herrera\\Doctrine\\ORM\\Query\\AST\\Functions\\DateIntervalFunction'
);

$entityManager->getConnection()
              ->getDatabasePlatform()
              ->registerDoctrineTypeMapping(
    DateIntervalType::DATEINTERVAL,
    DateIntervalType::DATEINTERVAL
);

When using Symfony2 with Doctrine you can do the same as above by only changing your configuration:, (*8)

# app/config/config.yml

# Doctrine Configuration
doctrine:
    dbal:
        # ...
        mapping_types:
            dateinterval: dateinterval
        types:
            dateinterval:  Herrera\Doctrine\DBAL\Types\DateIntervalType

    orm:
        # ...
        dql:
            datetime_functions:
                DATE_INTERVAL: Herrera\Doctrine\ORM\Query\AST\Functions\DateIntervalFunction

Usage

<?php

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

    /**
     * @Column(type="dateinterval")
     */
    private $interval;

    /**
     * @return DateInterval
     */
    public function getInterval()
    {
        return $this->interval;
    }

    /**
     * @param DateInterval $interval
     */
    public function setInterval(DateInterval $interval)
    {
        $this->interval = $interval;
    }
}

$annualJob = new Job();
$annualJob->setInterval(new DateInterval('P1Y'));

$monthlyJob = new Job();
$monthlyJob->setInterval(new DateInterval('P1M'));

$dailyJob = new Job();
$dailyJob->setInterval(new DateInterval('P1D'));

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

$jobs = $entityManager->createQuery(
    "SELECT j FROM Jobs j WHERE j.interval < DATE_INTERVAL('P1Y') ORDER BY j.interval ASC"
)->getResult();

echo $jobs[0]->getInterval()->toSpec(); // "P1D"
echo $jobs[1]->getInterval()->toSpec(); // "P1M"

NOTICE The date interval instances returned are of Herrera\DateInterval\DateInterval., (*9)

The Versions

19/03 2015

dev-master

9999999-dev http://herrera-io.github.com/php-doctrine-dateinterval

Supports DateInterval in Doctrine DBAL and ORM.

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine date interval

20/02 2013

1.0.0

1.0.0.0 http://herrera-io.github.com/php-doctrine-dateinterval

Supports DateInterval in Doctrine DBAL and ORM.

  Sources   Download

MIT

The Requires

 

The Development Requires

doctrine date interval