Delphine
Delphine

Reputation: 888

Symfony2 - Custom DQL function not found

I followed this tuto : http://iksela.tumblr.com/post/4985265226/custom-dql-functions-nvl-convert-to-number.

But I still get the same error:

SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION my_project.TO_NUMBER does not exist  

This is my code:

in My_project\MyBundle\DoctrineFunctions\ToNumberFunction.php :

namespace My_project\MyBundle\DoctrineFunctions;

use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;

class ToNumberFunction extends FunctionNode {


public $field;


/**
 * Parse DQL Function
 *
 * @param \Doctrine\ORM\Query\Parser $parser
 */

public function parse (\Doctrine\ORM\Query\Parser $parser)
{
    $parser->match(Lexer::T_IDENTIFIER);
    $parser->match(Lexer::T_OPEN_PARENTHESIS);
    $this->field = $parser->StringPrimary();
    $parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

/**
 * Get SQL
 *
 * @param \Doctrine\ORM\Query\SqlWalker $sqlWalker
 *
 * @return int
 */
public function getSql (\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
    return 'TO_NUMBER('.$this->field->dispatch($sqlWalker).')';
}


} 

In my config.yml, I have the following :

    orm:
    auto_generate_proxy_classes: %kernel.debug%
    entity_managers:
        default:
            mappings:
                ..........
                tree:
            loggable:
                    .......
        dql:
            numeric_functions:
                TO_NUMBER: My_project\MyBundle\DoctrineFunctions\ToNumberFunction

Upvotes: 0

Views: 544

Answers (2)

Jeff Horton
Jeff Horton

Reputation: 954

It looks like the Doctrine code is working fine, but TO_NUMBER isn't a function recognised by the database you are using.

TO_NUMBER appears to be an Oracle SQL specific function, so if you are using a different SQL database that is probably your issue.

Upvotes: 0

Jesus
Jesus

Reputation: 11

Try like this:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    entity_managers:
        default:
            mappings:
                ..........
                tree:
            loggable:
                .......
            dql:
                numeric_functions:
                    TO_NUMBER: My_project\MyBundle\DoctrineFunctions\ToNumberFunction

Or like this:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    default_entity_manager:  default
    dql:
        numeric_functions:
            TO_NUMBER: My_project\MyBundle\DoctrineFunctions\ToNumberFunction

if you dont have multimple entity managers is not necesary to declare:

default_entity_manager:  default.

http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html

Upvotes: 1

Related Questions