Any way of using SHA1 in DQL

[亡魂溺海] 提交于 2019-11-30 18:04:15

问题


DQL below generates an error:[Syntax Error] line 0, col 42: Error: Expected known function, got 'sha1'

Any way of using SHA1?

public function findIdByDql($hashId)
{
    $em = $this->getEntityManager();
    $query = $em->createQuery('DELETE FROM CarBrandBundle:Brands b WHERE sha1(b.id) = :id')
                ->setParameter('id', $hashId)
                ->execute();

    return;
}

回答1:


You need to create your own function which will translate sha1 function.

Your app/config/config.yml file:

doctrine:
     orm:
          dql:
             string_functions:
                 sha1: YourBundle\DQL\Sha

Your src/YourBundle/DQL/Sha.php file:

namespace YourBundle\DQL;

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

class Sha extends FunctionNode
{
    public $valueToSha = null;

    public function parse(Parser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);

        $this->valueToSha = $parser->StringPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(SqlWalker $sqlWalker)
    {
        return
         "sha1("
        . $this->valueToSha->dispatch($sqlWalker)
        . ")";
    }
}

Check doc for more info



来源:https://stackoverflow.com/questions/25099954/any-way-of-using-sha1-in-dql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!