Any way to use SHA1 in DQL

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

Any way to use 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;
}
+2
source share
1 answer

You need to create your own function that will translate the function sha1.

Your app/config/config.ymlfile:

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

Your src/YourBundle/DQL/Sha.phpfile:

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 out the doc for more info.

+7
source

All Articles