开发者

Doctrine Query Builder mysql Functions

开发者 https://www.devze.com 2023-03-06 15:04 出处:网络
im atempting to create one of my old mysql queries in Doctrine Query Builder however im getting back an error Error: Expected known function, got \'SEC_TO_TIME\'

im atempting to create one of my old mysql queries in Doctrine Query Builder however im getting back an error Error: Expected known function, got 'SEC_TO_TIME'

So im guessing that doctrine doesnt like the mysql function SEC_TO_TIME however it does seem to like AVG, COUNT and such. Is there any way apart form using the Doctrine_RawSQL clas开发者_高级运维s of getting the query builder to run the query?

Thanks


i know this question is very old, but i am answering this question for others who having same error.
you just need to add below code in form of file Named "SecToTime.php" as below path.

YourProjectName\libraries\Doctrine\DoctrineExtensions\Query\MySql\SecToTime.php

And put below code in above file Named "SecToTime.php".

<?php

namespace DoctrineExtensions\Query\Mysql;

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

class SecToTime extends FunctionNode {

    public $time;

    /**
     * @override
     */
    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) {
        return 'SEC_TO_TIME(' . $sqlWalker->walkArithmeticPrimary($this->time) . ')';
    }

    /**
     * @override
     */
    public function parse(\Doctrine\ORM\Query\Parser $parser) {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->time = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

}

you need to add above file name in your doctrine.php file for using it further. i hope you know how to add it.

If you having any query, feel free to ask.


Those AVG and Count are DQL aggregate functions, they have nothing to do with SQL. So there is no way to call UDF's from DQL, except for RawSQL

But, if you are using doctrine2, you might want to have a look at Adding your own functions to the DQL language and Extending DQL in Doctrine 2: User-Defined Functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号