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.

Doctrine では、カスタム DQL 関数を指定できます。このトピックの詳細については、Doctrine のクックブック記事 DQL User Defined Functions を参照してください。

In Symfony, you can register your custom DQL functions as follows:

Symfony では、カスタム DQL 関数を次のように登録できます。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
11
12
# config/packages/doctrine.yaml
doctrine:
    orm:
        # ...
        dql:
            string_functions:
                test_string: App\DQL\StringFunction
                second_string: App\DQL\SecondStringFunction
            numeric_functions:
                test_numeric: App\DQL\NumericFunction
            datetime_functions:
                test_datetime: App\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.

entity_managers が明示的に命名された場合、ORM を使用して関数を直接構成すると、「doctrine.orm」の下の認識されないオプション「dql」という例外がトリガーされます。dql 構成ブロックは、指定されたエンティティーマネージャーの下で定義する必要があります。
  • YAML
    YAML
  • XML
    XML
  • PHP
    PHP
1
2
3
4
5
6
7
8
9
10
# config/packages/doctrine.yaml
doctrine:
    orm:
        # ...
        entity_managers:
            example_manager:
                # Place your functions here
                dql:
                    datetime_functions:
                        test_datetime: App\DQL\DatetimeFunction

Caution

注意

DQL functions are instantiated by Doctrine outside of the Symfony service container so you can't inject services or parameters into a custom DQL function.

DQL 関数は Symfonyservice コンテナの外で Doctrine によってインスタンス化されるため、サービスやパラメーターをカスタム DQL 関数に注入することはできません。