简体   繁体   中英

Custom Doctrine DQL Function

I'm new to Symfony, so maybe my problem is very stupid but here it is :

I'm trying to define a custom doctrine DQL function but I can't get it work.

PHP

namespace MsfBundle\AST\Functions;


use Doctrine\ORM\Query\Lexer;

class Geo extends \Doctrine\ORM\Query\AST\Functions\FunctionNode
{
    /**
     * @var \Doctrine\ORM\Query\AST\ComparisonExpression
     */
    private $latitude;
    /**
     * @var \Doctrine\ORM\Query\AST\ComparisonExpression
     */    
    private $longitude;

    /**
     * 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->latitude = $parser->ComparisonExpression();
        $parser->match(Lexer::T_COMMA);
        $this->longitude = $parser->ComparisonExpression();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    /**
     * Get SQL 
     * 
     * @param \Doctrine\ORM\Query\SqlWalker $sqlWalker
     * @return string
     */
    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {

    return sprintf('(6366*acos(cos(radians(%s))*cos(radians(`%s`))*cos(radians(`%s`) -radians(%s))+sin(radians(%s))*sin(radians(`%s`))))',
    $this->latitude->leftExpression->dispatch($sqlWalker),
    $this->latitude->rightExpression->dispatch($sqlWalker),
    $this->longitude->rightExpression->dispatch($sqlWalker), 
    $this->longitude->leftExpression->dispatch($sqlWalker),
    $this->latitude->leftExpression->dispatch($sqlWalker),
    $this->latitude->rightExpression->dispatch($sqlWalker));
    }
}

config.yml

doctrine:
#...
orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
        default:
            auto_mapping: true
            dql:
                numeric_functions:
                    geo :MsfBundle\AST\Functions\Geo

And here's what I get :

Fatal error: Class 'MsfBundle\\AST\\Functions\\Geo' not found in /var/www/mcr/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php on line 3070

If you have any lead to help me get throught this, I'd really appreciate it.

Thanks !

you are missing a backslash in geo :MsfBundle\\AST\\Functions\\Geo, should be geo :\\MsfBundle\\AST\\Functions\\Geo. Seems the class need an absolute reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM