How to Register custom DQL Functions
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
Doctrine allows you to specify custom DQL functions. For more information on this topic, read Doctrine's cookbook article "`DQL User Defined Functions`_".
In Symfony, you can register your custom DQL functions as follows:
1 2 3 4 5 6 7 8 9 10 11 12
# app/config/config.yml
doctrine:
orm:
# ...
dql:
string_functions:
test_string: AppBundle\DQL\StringFunction
second_string: AppBundle\DQL\SecondStringFunction
numeric_functions:
test_numeric: AppBundle\DQL\NumericFunction
datetime_functions:
test_datetime: AppBundle\DQL\DatetimeFunction
Note
In case the entity_managers
were named explicitly, configuring the functions with the
orm directly will trigger the exception `Unrecognized option "dql" under "doctrine.orm"`.
The dql
configuration block must be defined under the named entity manager.
1 2 3 4 5 6 7 8 9 10
# app/config/config.yml
doctrine:
orm:
# ...
entity_managers:
example_manager:
# Place your functions here
dql:
datetime_functions:
test_datetime: AppBundle\DQL\DatetimeFunction