How can I use SQL's YEAR(), MONTH() and DAY() in Doctrine2? How can I use SQL's YEAR(), MONTH() and DAY() in Doctrine2? sql sql

How can I use SQL's YEAR(), MONTH() and DAY() in Doctrine2?


You can add Doctrine extension so you can use the MySql YEAR and MONTH statement by adding this configuration if you're on Symfony:

doctrine:    orm:        dql:            string_functions:                MONTH: DoctrineExtensions\Query\Mysql\Month                YEAR: DoctrineExtensions\Query\Mysql\Year

now you can use the MONTH and YEAR statements in your DQL or querybuilder.

Note: The extension supports MySQL, Oracle, PostgreSQL and SQLite.


In Symfony 4 you must install DoctrineExtensions:

composer require beberlei/DoctrineExtensions

And then edit the doctrine config file (config/packages/doctrine.yaml) as follow:

doctrine:    orm:        dql:            string_functions:                MONTH: DoctrineExtensions\Query\Mysql\Month                YEAR: DoctrineExtensions\Query\Mysql\Year


orocrm/doctrine-extensions seems to be a good project too

It supports both MySQL and PostgreSql .. the goal is to be cross DB