How to install Doctrine Extensions in a Symfony2 p

2019-01-17 22:16发布

问题:

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)

回答1:

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


回答2:

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.