简体   繁体   中英

Doctrine Query Builder mysql Functions

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 class 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 .

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