Any way of using SHA1 in DQL

2019-02-15 19:06发布

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条回答
该账号已被封号
2楼-- · 2019-02-15 19:42

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

查看更多
登录 后发表回答