From b16423b26b21cf87c05bed58fc7fcdf71eef0127 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Fri, 16 May 2014 09:32:52 +0200 Subject: [PATCH 1/2] Documentation for #991 --- docs/en/cookbook/dql-user-defined-functions.rst | 11 +++++++++++ lib/Doctrine/ORM/Configuration.php | 12 ++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/en/cookbook/dql-user-defined-functions.rst b/docs/en/cookbook/dql-user-defined-functions.rst index 77d66ad82..a3a7606ad 100644 --- a/docs/en/cookbook/dql-user-defined-functions.rst +++ b/docs/en/cookbook/dql-user-defined-functions.rst @@ -53,6 +53,17 @@ DQL query. ``$class`` is a string of a class-name which has to extend ``Doctrine\ORM\Query\Node\FunctionNode``. This is a class that offers all the necessary API and methods to implement a UDF. +Instead of providing the function class name, you can also provide +a callable that returns the function object: + +.. code-block:: php + + addCustomStringFunction($name, function () { + return new MyCustomFunction(); + }); + In this post we will implement some MySql specific Date calculation methods, which are quite handy in my opinion: diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index d9ff96b47..02433b93c 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -401,8 +401,8 @@ class Configuration extends \Doctrine\DBAL\Configuration * * DQL function names are case-insensitive. * - * @param string $name - * @param string $className + * @param string $name Function name. + * @param string|callable $className Class name or a callable that returns the function. * * @return void * @@ -459,8 +459,8 @@ class Configuration extends \Doctrine\DBAL\Configuration * * DQL function names are case-insensitive. * - * @param string $name - * @param string $className + * @param string $name Function name. + * @param string|callable $className Class name or a callable that returns the function. * * @return void * @@ -517,8 +517,8 @@ class Configuration extends \Doctrine\DBAL\Configuration * * DQL function names are case-insensitive. * - * @param string $name - * @param string $className + * @param string $name Function name. + * @param string|callable $className Class name or a callable that returns the function. * * @return void * From 5a4c55886571246733551916620cfcc00e131e9a Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Fri, 16 May 2014 10:49:59 +0200 Subject: [PATCH 2/2] Fixed #991 to support non-closure callables on PHP 5.3 --- lib/Doctrine/ORM/Query/Parser.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 762cf67ce..c64e731c6 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -3411,7 +3411,7 @@ class Parser $function = is_string($functionClass) ? new $functionClass($functionName) - : $functionClass($functionName); + : call_user_func($functionClass, $functionName); $function->parse($this); @@ -3450,7 +3450,7 @@ class Parser $function = is_string($functionClass) ? new $functionClass($functionName) - : $functionClass($functionName); + : call_user_func($functionClass, $functionName); $function->parse($this); @@ -3490,7 +3490,7 @@ class Parser $function = is_string($functionClass) ? new $functionClass($functionName) - : $functionClass($functionName); + : call_user_func($functionClass, $functionName); $function->parse($this);