Any way of using SHA1 in DQL

后端 未结 1 469
终归单人心
终归单人心 2021-01-06 17:36

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

Any way of using SHA1?

public f         


        
相关标签:
1条回答
  • 2021-01-06 17:57

    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

    0 讨论(0)
提交回复
热议问题