How to Register custom DQL Functions

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

    1. # config/packages/doctrine.yaml
    2. doctrine:
    3. orm:
    4. # ...
    5. dql:
    6. string_functions:
    7. test_string: App\DQL\StringFunction
    8. second_string: App\DQL\SecondStringFunction
    9. numeric_functions:
    10. test_numeric: App\DQL\NumericFunction
    11. datetime_functions:
    12. test_datetime: App\DQL\DatetimeFunction
  • XML

    1. <!-- config/packages/doctrine.xml -->
    2. <container xmlns="http://symfony.com/schema/dic/services"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd
    7. http://symfony.com/schema/dic/doctrine
    8. https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
    9. <doctrine:config>
    10. <doctrine:orm>
    11. <!-- ... -->
    12. <doctrine:dql>
    13. <doctrine:string-function name="test_string">App\DQL\StringFunction</doctrine:string-function>
    14. <doctrine:string-function name="second_string">App\DQL\SecondStringFunction</doctrine:string-function>
    15. <doctrine:numeric-function name="test_numeric">App\DQL\NumericFunction</doctrine:numeric-function>
    16. <doctrine:datetime-function name="test_datetime">App\DQL\DatetimeFunction</doctrine:datetime-function>
    17. </doctrine:dql>
    18. </doctrine:orm>
    19. </doctrine:config>
    20. </container>
  • PHP

    1. // config/packages/doctrine.php
    2. use App\DQL\DatetimeFunction;
    3. use App\DQL\NumericFunction;
    4. use App\DQL\SecondStringFunction;
    5. use App\DQL\StringFunction;
    6. $container->loadFromExtension('doctrine', [
    7. 'orm' => [
    8. // ...
    9. 'dql' => [
    10. 'string_functions' => [
    11. 'test_string' => StringFunction::class,
    12. 'second_string' => SecondStringFunction::class,
    13. ],
    14. 'numeric_functions' => [
    15. 'test_numeric' => NumericFunction::class,
    16. ],
    17. 'datetime_functions' => [
    18. 'test_datetime' => DatetimeFunction::class,
    19. ],
    20. ],
    21. ],
    22. ]);

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

    1. # config/packages/doctrine.yaml
    2. doctrine:
    3. orm:
    4. # ...
    5. entity_managers:
    6. example_manager:
    7. # Place your functions here
    8. dql:
    9. datetime_functions:
    10. test_datetime: App\DQL\DatetimeFunction
  • XML

    1. <!-- config/packages/doctrine.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/doctrine
    9. https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
    10. <doctrine:config>
    11. <doctrine:orm>
    12. <!-- ... -->
    13. <doctrine:entity-manager name="example_manager">
    14. <!-- place your functions here -->
    15. <doctrine:dql>
    16. <doctrine:datetime-function name="test_datetime">
    17. App\DQL\DatetimeFunction
    18. </doctrine:datetime-function>
    19. </doctrine:dql>
    20. </doctrine:entity-manager>
    21. </doctrine:orm>
    22. </doctrine:config>
    23. </container>
  • PHP

    1. // config/packages/doctrine.php
    2. use App\DQL\DatetimeFunction;
    3. $container->loadFromExtension('doctrine', [
    4. 'orm' => [
    5. // ...
    6. 'entity_managers' => [
    7. 'example_manager' => [
    8. // place your functions here
    9. 'dql' => [
    10. 'datetime_functions' => [
    11. 'test_datetime' => DatetimeFunction::class,
    12. ],
    13. ],
    14. ],
    15. ],
    16. ],
    17. ]);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.