0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

You're maybe right ! Unfortunately, I don't code this project anymore. The client decided to use Wordpress.. Shame on him. Anyway, I think you're right and for next people I suggest to use CAST instead of TO_NUMBER in "get_sql" method. In my case : return 'CAST('.$this->field->dispatch($sqlWalker).' as UNSIGNED)';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.