【问题标题】:Custom DQL-Function not found未找到自定义 DQL 函数
【发布时间】:2013-08-25 22:31:58
【问题描述】:

我正在尝试为 DQL 实现自定义 DATE 函数,代码如下:

namespace SwingBy\SwingByBundle\DQL; 

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

/**
 * Date  ::= "DATE" "(" ArithmeticPrimary ")"
 */

class Date extends FunctionNode {

    public $dateTimeExpression = null;

    public function parse(\Doctrine\ORM\Query\Parser $parser){

         $parser->match(Lexxer::T_IDENTIFIER);
         $parser->match(Lexxer::T_OPEN_PARENTHESIS);
         $this->dateTimeExpression = $parser->ArithmeticPrimary();
         $parser->match(Lexxer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker){

        return "DATE(" . $this->dateTimeExpression . ")";
    }
}

这是 config.yml 中的配置:

doctrine:
    orm:
        dql:
            datetime_functions:
                date: SwingBy\SwingByBundle\DQL\Date

这是错误:

The autoloader expected class "SwingBy\SwingByBundle\DQL\Date" to be defined in file 
"/Applications/MAMP/htdocs/swingby_dev2.3/src/SwingBy/SwingByBundle/DQL/Date.php". The 
file was found but the class was not in it, the class name or namespace probably has a   
typo.

我无法找到解决方案,我尝试了几种不同的命名空间,但没有任何效果。

Symfony 版本:2.3

【问题讨论】:

  • 命名空间 SwingBy\SwingByBundle\DQL;
  • 已经试过了,还是一样的错误
  • 不太可能。错误信息很清楚。确保您查看的是正确的文件。

标签: symfony doctrine-orm dql


【解决方案1】:

根据文档http://symfony.com/fr/doc/current/cookbook/doctrine/custom_dql_functions.html

doctrine:
    orm:
        entity_managers:
            default:
                dql:
                    datetime_functions:
                        date: SwingBy\SwingByBundle\DQL\Date

和/或尝试添加反斜杠

日期:\SwingBy\SwingByBundle\DQL\Date

【讨论】:

  • 虽然不会有什么坏处,但如果您与多个实体管理器打交道,您实际上只需要 entity_manages 级别。不,添加反斜杠既不需要也不会有帮助。系统知道需要加载 Date 并且知道正确的文件。他只是在某个地方打错了字。问题中发布的内容可能并不代表实际日期文件中的内容。
猜你喜欢
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
相关资源
最近更新 更多