Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install Doctrine Extensions in a Symfony2 project

I guess that this is really trivial and stupid question, but I don't know how to install Doctrine Extensions - https://github.com/beberlei/DoctrineExtensions in my Symfony2 project. I need them because of the MONTH, YEAR functions. Where should I put their folder? And should I put the whole DoctrineExtensions folder? And where to write this:

<?php

$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', "/path/to/extensions");
$classLoader->register(); 

In a separate file? Where to put it and how to call it?

And then is this all I need to use them:

public function findOneByYearMonthDay($year, $month, $day)
{
    $emConfig = $this->getEntityManager()->getConfiguration();
    $emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
    $emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
    $emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');

Thank you very much in advance and sorry once again for the question, but I couldn't find a tutorial (which makes me feel even more guilty, because I guess it's too trivial when there isn't even a tutorial)

like image 744
Faery Avatar asked Jun 03 '13 10:06

Faery


3 Answers

You can install it via composer. Just add it to your composer.json and then php composer.phar update beberlei/DoctrineExtensions

"beberlei/DoctrineExtensions": "*",

Then you can register functions to your ORM

doctrine:
    orm:
      auto_generate_proxy_classes: %kernel.debug%
      entity_managers:
        default:
          auto_mapping: true
          dql:
            datetime_functions:
              MONTH: DoctrineExtensions\Query\Mysql\Month
              YEAR: DoctrineExtensions\Query\Mysql\Year
like image 116
Alexey B. Avatar answered Oct 21 '22 15:10

Alexey B.


There also is a nice fork by wiredmedia of @beberlei which includes even more datetime_functions like DATE() itself:

This is an unsanctioned fork of https://github.com/beberlei/DoctrineExtensions since he seems to have gone off grid and is not merging pull requests.

Unfortunately Version 0.1 just includes the fork and not all the functions. We are waiting for a stable release:

Please create taged stable version for set in my composer #2

But you can add them manually unitl a stable version is out.

like image 1
webDEVILopers Avatar answered Oct 21 '22 16:10

webDEVILopers


Here is how to use DoctrineExtensions in the context of Symfony using the DoctrineBundle.

First install the package DoctrineExtensions:

composer require beberlei/doctrineextensions

Then add to your doctrine configuration (doctrine.yaml file) the DQL functions you want to include in your application:

doctrine:
    # Register types this way in the dbal config part
    dbal:
        types:
            carbondatetime: DoctrineExtensions\Types\CarbonDateTimeType
    # Register DQL functions in the ORM part
    orm:
        dql:
            string_functions:
                FIND_IN_SET: DoctrineExtensions\Query\Mysql\FindInSet
            numeric_functions:
                SIN: DoctrineExtensions\Query\Mysql\Sin
            datetime_functions:
                DATE: DoctrineExtensions\Query\Mysql\Date

This is an example, feel free to adjust to your needs (you can remove sections).

like image 1
Nek Avatar answered Oct 21 '22 17:10

Nek