Fork me on Github

Doctrine 2 ORM integration

Doctrine 2 ORM User ?
Implements your Event Providers in Seconds (really, seconds).

First way for PHP 5.4+ users

CalendR provides a EventRepository trait, that can be used in your event repository to implements the ProviderInterface::getEvents() mandatory method.

<?php
use Doctrine\ORM\EntityRepository;
use CalendR\Extension\Doctrine2\EventRepository as EventRepositoryTrait;

class EventRepository extends EntityRepository
{
    use EventRepositoryTrait;
}
?>

And… well… That’s all.

Tweaking field names and options management

The above example is valid and fully working (you can check, it’s used as-is in the CalendR test suite), but it assumes some little things :

  • Your begin field is called begin
  • Your end field is called end
  • You don’t need options management (single calendar)

Thanks to PHP 5.4 trait methods overriding, here is a more complete example :

<?php
use Doctrine\ORM\EntityRepository;
use CalendR\Extension\Doctrine2\EventRepository as EventRepositoryTrait;

class EventRepository extends EntityRepository
{
    use EventRepositoryTrait;

    /**
     * Returns the query builder on wich the event retrieving query will
     * be append. The default table alias is `evt`
     */
    public function createQueryBuilderForGetEvent(array $options)
    {
        // do what you want with the $option array
        return $this->createQueryBuilder('evt')
            ->setMaxResults(10)
        ;
    }

    /**
     * Returns the name of the begin field.
     */
    public function getBeginFieldName()
    {
        return 'evt.beginDate';
    }

    /**
     * Returns the name of the end field.
     */
    public function getEndFieldName()
    {
        return 'evt.endDate';
    }
}
Note : The EventRepository trait provide you two more methods :
  • getEventsQueryBuilder()
  • getEventsQuery()

Second way for PHP 5.3+ users

There is a CalendR\Extensions\Doctrine2\QueryHelper class that provides a addEventQuery method to add the event retrieving criteria to the QueryBuilder.

<?php
use Doctrine\ORM\EntityRepository;
use CalendR\Extension\Doctrine2\QueryHelper;

class EventRepository extends EntityRepository
{
    public function getEventsQueryBuilder(\DateTime $begin, \DateTime $end, array $options = array())
    {
        $qb = $this->createQueryBuilder('e');

        return QueryHelper::addEventQuery($qb, 'e.begin', 'e.end', $begin, $end)
            ->getQuery()
            ->getResults()
        ;
    }
}

Warning : The PHP 5.3 way is deprecated but will be keep as long as PHP 5.3 is supported.