How to Register custom DQL Functions

Version: 2.8
Edit this page

Warning: You are browsing the documentation for Symfony 2.8, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

How to Register custom DQL Functions

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:

  • YAML
  • XML
  • PHP
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.

  • YAML
  • XML
  • PHP
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
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.