3

I installed the Beberlei DoctrineExtensions but can't get the mysql DAY and YEAR to work.

I have this in my autoloader:

$doctrineExtensions = new Doctrine\Common\ClassLoader('DoctrineExtensions', ROOT.DS.'library');
$doctrineExtensions->register();

And when i try to do following dql statement

   SELECT YEAR(e.eventdate) FROM \Entities\Event e

I get following error: Error: Expected known function, got 'YEAR'' in /usr/local/lib/php/Doctrine/ORM/Query/QueryException.php:42

In top of my file i have

use \DoctrineExtensions\Query\Mysql; 

And i can see that it can find the class if i type:

    $test = \DoctrineExtensions\Query\Mysql\Year::getSql();

But not if i type

    $test = Year::getSql();

Seems like some implementation is missing but can't find out what the problem is..

Saggo
  • 129
  • 1
  • 4
  • 13

3 Answers3

2

Please note that Doctrine\Common\ClassLoader as used in the other answers is now deprecated.

The proper way is something like:

use DoctrineExtensions\Query\Mysql\Year;
...
$config = new \Doctrine\ORM\Configuration();
// Can also be:
// $config = \Doctrine\ORM\Tools\Setup::createConfiguration();
// Or:
// $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration([...]);
$config->addCustomDateTimeFunction('YEAR', Year::class);

You can also load from a configuration file using fully-qualified class names using Doctrine\ORM\Configuration setCustomDateTimeFunctions([...]), like so:

doctrine:
  orm:
    dql:
      datetime_functions:
        year: DoctrineExtensions\Query\Mysql\Year
        month: DoctrineExtensions\Query\Mysql\Month
LiquidPony
  • 2,188
  • 1
  • 17
  • 19
-1

You need to register the function:

$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions');
$classLoader->register();
...

$config = $this->getEntityManager()->getConfiguration();
$config->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
Arnold Roa
  • 7,335
  • 5
  • 50
  • 69
-1

It's a bit late :) but it will help others like me that get from a client an old project.

You have to register the namespace of the DoctrineExtension directory:

$classLoader = new \Doctrine\Common\ClassLoader('DoctrineExtensions', APPPATH . 'libraries/vendor/beberlei');
$classLoader->register();

Now when you get the DoctrineExtensions from composer, it comes under the src directory, so just move up the content of the src directory (under the DoctrineExtensions) and add extension you need to the config :

$config->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');