LoginSignup
1
3

More than 5 years have passed since last update.

DoctrineのDQL関数拡張を行う方法

Last updated at Posted at 2018-10-23

DoctrineのDQLに存在しない関数をユーザー独自関数として拡張を行う方法のメモ

MySQL独自関数やPostgreSQL独自関数をDQLで呼ぼうとすると、

Doctrine\ORM\Query\ QueryException
[Syntax Error] line 0, col 42: Error: Expected known function, got 'DATEDIFF'

のようなエラーが出る。
これはDQLに該当の関数が実装されていないからで、ユーザー独自関数としてDQLに登録すれば使えるようになる。その方法の覚書。

Doctrineに拡張できるSQL関数リスト

DoctrineExtensions


DQLへの関数拡張を扱った記事リスト

Add MySQL function DATE_FORMAT to Symfony Doctrine DQL

DQL User Defined Functions

Symfony2【Doctrine DQL関数拡張】MySQL(PostGreSQL)のNOW,ROUND,FLOOR,GROUP_CONCAT等の関数を使用(EC-CUBE3)

DoctrineでSQLの日付関数を使う方法

How to Register custom DQL Functions


Symfony2の場合、config.yml に記載して登録する方法もある
In Symfony2 you can register functions in config.yml


EC-CUBE4系でハマったところ

EC-CUBE4系では、下記のリンク先のように

\$emConfig = \$this->getEntityManager()->getConfiguration();
\$emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
\$emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
\$emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');

の方法で\$configを設定しないとユーザー独自関数の登録が上手くいかなかった。
How To Select YEAR() MONTH() DAY() In Doctrine2

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3