简体   繁体   English

Doctrine 查询生成器 mysql 函数

[英]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'我试图在 Doctrine Query Builder 中创建我的旧 mysql 查询之一,但是我返回一个错误错误:预期已知函数,得到“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.所以我猜测该学说不喜欢 mysql 函数SEC_TO_TIME但是它似乎喜欢AVGCOUNT等。 Is there any way apart form using the Doctrine_RawSQL class of getting the query builder to run the query?有没有办法使用 Doctrine_RawSQL 类让查询构建器运行查询?

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.您只需要以名为“SecToTime.php”的文件形式添加以下代码,如下路径。

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

And put below code in above file Named "SecToTime.php".并将下面的代码放在上面名为“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.您需要在您的 Doctic.php 文件中添加上述文件名以进一步使用它。 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.那些AVGCount是 DQL 聚合函数,它们与 SQL 无关。 So there is no way to call UDF's from DQL, except for RawSQL所以没有办法从 DQL 调用 UDF,除了 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 .但是,如果您使用的是 Doctrine2 ,您可能需要查看将您自己的函数添加到 DQL 语言扩展 DQL 在 Doctrine 2: User-Defined Functions 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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