预期的已知函数,得到了...... ...没有找到TimeDiffFunction .... Doctrine \ ORM \ Query \ Parser.php

时间:2014-01-31 22:08:07

标签: symfony orm doctrine-orm dql

我正在尝试实现一个自定义函数来获取两个字段的时间差,但我一直收到错误...我已经读过类似的错误,但答案并没有真正帮助。

我一直这样做:

<?php
namespace Bundle\DQL;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;

/**
 * Custom DQL function returning the difference between two DateTime values
 * 
 * usage TIME_DIFF(dateTime1, dateTime2)
 */
class TimeDiffFunction extends FunctionNode
{
    /**
     * @var string
     */
    public $dateTime1;

    /**
     * @var string
     */
    public $dateTime2;

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->dateTime1 = $parser->ArithmeticPrimary();       
        $parser->match(Lexer::T_COMMA);
        $this->dateTime2 = $parser->ArithmeticPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return 'TIME_TO_SEC(TIMEDIFF(' .
            $this->dateTime1->dispatch($sqlWalker) . ', ' .
            $this->dateTime2->dispatch($sqlWalker) .
        '))'; 
    }
}

config.yml

 orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true
        dql:
           numeric_functions:
              time_diff: Bundle:DQL:TimeDiffFunction

控制器:

$em = $this->getDoctrine()->getManager();
        $detail1 = $em->getRepository('Bundle:Detail')->find(96);
        //$detail2 = $em->getRepository('Bundle:Detail')->find(138);
        $query = $em->createQuery("SELECT time_diff(eng.updatedDate, eng.updatedDate) FROM Bundle\Entity\Detail eng WHERE eng.id = :id1");
        $query->setParameter('id1', $detail1->getId());
        $entities = $query->getResult();
        die(var_dump($entities));

这样做我得到了:

FatalErrorException: Error: Class 'Bundle:DQL:TimeDiffFunction' not found in C:\xampp\htdocs\EngMgmt\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Parser.php line 3070

但是在控制器上执行此操作

        $query = $em->createQuery("SELECT Bundle\DQL\TimeDiff(eng.updatedDate, eng.updatedDate) FROM Bundle\Entity\Detail eng WHERE eng.id = :id1");

我得到:

[Syntax Error] line 0, col 7: Error: Expected known function, got 'Bundle\DQL\TimeDiff'

如果我在控制器上执行此操作:

$query = $em->createQuery("SELECT Bundle\DQL\time_diff(eng.updatedDate, eng.updatedDate) Bundle\Entity\Detail eng WHERE eng.id = :id1");

我得到:

[Syntax Error] line 0, col 47: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got '('

所以,问题是,我怎样才能让它发挥作用?我真的需要它。

1 个答案:

答案 0 :(得分:2)

按照以下示例进行操作:

http://symfony.com/doc/current/cookbook/doctrine/custom_dql_functions.html

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/dql-user-defined-functions.html

使用完全限定的类名

    dql:
       numeric_functions:
          time_diff: Bundle\DQL\TimeDiffFunction

Bundle对于Bundle来说是一个奇怪的名字。你确定这是对的吗?

SELECT time_diff(eng.updatedDate,eng.updatedDate)...一旦正确安装就可以正常工作。