以下DQL会产生错误:[Syntax Error] line 0, col 42: Error: Expected known function, got 'sha1'
使用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;
}
答案 0 :(得分:7)
您需要创建自己的函数,该函数将转换sha1
函数。
您的app/config/config.yml
文件:
doctrine:
orm:
dql:
string_functions:
sha1: YourBundle\DQL\Sha
您的src/YourBundle/DQL/Sha.php
文件:
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)
. ")";
}
}
查看doc了解详情