dongtu9823 2018-12-08 11:19 采纳率: 0%
浏览 53
已采纳

Symfony2 - 自定义DQL函数已注册但不起作用

I am desperatelly trying to include the GREATEST function in mysql Symfony2, however, I still receive errors.

Added the function in DQL:

<?php

namespace DoctrineExtensions\Query\Mysql;

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

class Greatest extends FunctionNode
{
    private $field = null;

    private $values = [];

    /**
     * @param Parser $parser
     */
    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->field = $parser->ArithmeticExpression();
        $lexer = $parser->getLexer();

        while (count($this->values) < 1 ||
            $lexer->lookahead['type'] != Lexer::T_CLOSE_PARENTHESIS) {
            $parser->match(Lexer::T_COMMA);
            $this->values[] = $parser->ArithmeticExpression();
        }

        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    /**
     * @param SqlWalker $sqlWalker
     * @return string
     */
    public function getSql(SqlWalker $sqlWalker)
    {
        $query = 'GREATEST(';

        $query .= $this->field->dispatch($sqlWalker);

        $query .= ', ';

        for ($i = 0; $i < count($this->values); $i++) {
            if ($i > 0) {
                $query .= ', ';
            }

            $query .= $this->values[$i]->dispatch($sqlWalker);
        }

        $query .= ')';

        return $query;
    }
}

In Config.yml

orm:
        auto_generate_proxy_classes: false
        auto_mapping: true
        dql:
            datetime_functions:
                Greatest: DoctrineExtensions\Query\Mysql\Greatest

Problem: When executing the following code block in my Repository, the following errors occur:

$admitObj = $em->createQueryBuilder();
      $admitObj
        ->select('A')
        ->from("EntityBundle:Admit", "A")
        ->orderBy("GREATEST( COALESCE(A.date1, 0), COALESCE(A.date2, 0))", "DESC");

      $admit = $admitObj->setMaxResults(1)->getQuery()->getResult();

ERROR: [Syntax Error] line 0, col 111: Error: Expected end of string, got '('

What am I missing? Why isn't DQL/Symfony/PDO/... recognizing the function? Any help is highly appreciated!

  • 写回答

1条回答 默认 最新

  • dongyun65343 2018-12-17 12:07
    关注

    After long research I find that it was problem in my parser.php file. I solved this issue to replace below code in my parser file. It is issue of using greatest function in order by.

    /**
         * OrderByClause ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*
         *
         * @return \Doctrine\ORM\Query\AST\OrderByClause
         */
        public function OrderByClause()
        {
            $this->match(Lexer::T_ORDER);
            $this->match(Lexer::T_BY);
    
            $orderByItems = array();
            $orderByItems[] = $this->OrderByItem();
    
            while ($this->lexer->isNextToken(Lexer::T_COMMA)) {
                $this->match(Lexer::T_COMMA);
    
                $orderByItems[] = $this->OrderByItem();
            }
    
            return new AST\OrderByClause($orderByItems);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?