本文介绍了使用Doctrine和Symfony2自定义DQL函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Symfony 2和ORM主义。我想创建并注册一个自定义的DQL函数。实际上,我想使用SQL函数我的请求,像这样:

  $ qb = $ this-> _em-> createQueryBuilder(); 
$ qb-> select('d')
- > from('\Test\MyBundle\Entity\MyEntity','d')
- > orderBy('CAST(d.myField AS UNSIGNED)','ASC')

return $ qb-> getQuery() - > getResult();

为此,我创建了一个扩展FunctionNode的CastFunction:

 命名空间Test\MyBundle\DQL; 

使用Doctrine\ORM\Query\AST\Functions\FunctionNode;
使用Doctrine\ORM\Query\Lexer;
使用Doctrine\ORM\Query\SqlWalker;
使用Doctrine\ORM\Query\Parser;

class CastFunction extends FunctionNode
{
public $ firstDateExpression = null;
public $ secondDateExpression = null;

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

public function getSql(\Doctrine\ORM\Query\SqlWalker $ sqlWalker)
{
return sprintf('CASS %s)',$ this-> firstDateExpression-> dispatch($ sqlWalker),$ this-> secondDateExpression-> dispatch($ sqlWalker));
}
}

当然,我已经在我的配置中注册了这个类.yml:

  doctrine:
orm:
dql:
string_functions:
CAST:Test\MyBundle\DQL\CastFunction

现在,当我尝试我的请求时,我得到以下错误:



[语义错误]行0,col 83靠近UNSIGNED):错误:'UNSIGNED'未定义。



我搜索,但我不在哪里的问题!



你有想法吗?

解决方案

经过多次搜索,我终于找到了解决方案。我有两个问题:首先我的解析功能是错误的,其次,我在orderBy中调用了一个SQL函数(谢谢 )



所以,这是我正确的类:

 命名空间Ypok\YPoliceBundle\DQL; 

使用Doctrine\ORM\Query\AST\Functions\FunctionNode;
使用Doctrine\ORM\Query\Lexer;
使用Doctrine\ORM\Query\SqlWalker;
使用Doctrine\ORM\Query\Parser;

class CastFunction extends FunctionNode
{
public $ firstDateExpression = null;
public $ unit = null;

public function parse(\Doctrine\ORM\Query\Parser $ parser)
{
$ parser-> match(Lexer :: T_IDENTIFIER);
$ parser-> match(Lexer :: T_OPEN_PARENTHESIS);
$ this-> firstDateExpression = $ parser-> StringPrimary();

$ parser-> match(Lexer :: T_AS);

$ parser-> match(Lexer :: T_IDENTIFIER);
$ lexer = $ parser-> getLexer();
$ this-> unit = $ lexer-> token ['value'];

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

public function getSql(\Doctrine\ORM\Query\SqlWalker $ sqlWalker)
{
return sprintf('CASS %s)',$ this-> firstDateExpression-> dispatch($ sqlWalker),$ this-> unit);
}
}

现在,我可以完美使用SQL函数' CAST'在我的存储库中:

  $ qb = $ this-> _em-> createQueryBuilder(); 
$ qb-> select('d,CAST(d.myField AS UNSIGNED)AS sortx')
- > from('\Test\MyBundle\Entity\MyEntity' 'd')
- > orderBy('sortx','ASC')

return $ qb-> getQuery() - > getResult();

最好的问候


I use Symfony 2 and the ORM Doctrine. I want to create and register a custom DQL function. In fact, I want to use the SQL function "CAST" in my request, like this :

    $qb = $this->_em->createQueryBuilder();
    $qb->select('d')
       ->from('\Test\MyBundle\Entity\MyEntity', 'd')
       ->orderBy('CAST(d.myField AS UNSIGNED)', 'ASC')

    return $qb->getQuery()->getResult();

For this, I have created a "CastFunction" who extend "FunctionNode" :

namespace Test\MyBundle\DQL;

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

class CastFunction extends FunctionNode
{
    public $firstDateExpression = null;
    public $secondDateExpression = null;

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

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return sprintf('CAST(%s AS %s)', $this->firstDateExpression->dispatch($sqlWalker), $this->secondDateExpression->dispatch($sqlWalker));
    }
}

Of course, I have registered this class in my config.yml :

doctrine:
    orm:
        dql:
            string_functions:
                CAST: Test\MyBundle\DQL\CastFunction

Now, when I try my request, I obtain the following error:

"[Semantical Error] line 0, col 83 near 'UNSIGNED)': Error: 'UNSIGNED' is not defined."

I search but I don't where is the problem!

Have you got a idea?

解决方案

After several search, I have finally found the solution. I had two problems: first my parse function was wrong, second, I called a SQL function in my orderBy (thank you Cerad).

So, here is my correct class:

namespace Ypok\YPoliceBundle\DQL;

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

class CastFunction extends FunctionNode
{
    public $firstDateExpression = null;
    public $unit = null;

    public function parse(\Doctrine\ORM\Query\Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->firstDateExpression = $parser->StringPrimary();

        $parser->match(Lexer::T_AS);

        $parser->match(Lexer::T_IDENTIFIER);
        $lexer = $parser->getLexer();
        $this->unit = $lexer->token['value'];

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

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
    {
        return sprintf('CAST(%s AS %s)',  $this->firstDateExpression->dispatch($sqlWalker), $this->unit);
    }
}

And now, I can use perfectly the SQL function 'CAST' in my repository:

$qb = $this->_em->createQueryBuilder();
$qb->select('d, CAST(d.myField AS UNSIGNED) AS sortx')
   ->from('\Test\MyBundle\Entity\MyEntity', 'd')
   ->orderBy('sortx', 'ASC')

return $qb->getQuery()->getResult();

Best regards

这篇关于使用Doctrine和Symfony2自定义DQL函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 12:31