Doctrine 2 CustomStringFunction and Undefined Index

Doctrine 2 allows for user defined functions to be created, this allows us to add vendor specific functions such as MySQL’s CONCAT_WS. I had the need to implement my own functions in a project I’ve been working on as DQL does not support a way to CONCAT expressions that contain null values without returning null (i.e. there is no IFNULL or ISNULL function).

After reading the Adding your own functions to the DQL language documentation and attempting to implement my own function I was repeatedly getting the following error: Undefined index and Class name must be a valid object or a string. I had a feeling that this was to do with my custom function not being auto-loaded although I was certain that I had followed the documentation correctly.

Having looked through Doctrine’s ORM classes I discovered a function named CustomFunctionsReturningStrings inside Parser.php. I set a breakpoint inside the function and was surprised to see that it never gets called. This is a bug in versions of Doctrine prior to 2.0.5.

If you are unable or do not wish to upgrade between versions but still need this fix you can apply the following patch to \Doctrine\ORM\Query\Parser.php:

<                 return $this->FunctionsReturningStrings();
>                 // do NOT directly go to FunctionsReturningString() because it doesnt check for custom functions.
>                 return $this->FunctionDeclaration();

Leave a Reply

Your email will not be published. Name and Email fields are required.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>