尝试注册 soundex 函数会导致:
[语法错误] 第 0 行,第 106 列:错误:预期的字符串结尾,得到 'SOUNDEX'
类定义:
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
/**
* SoundexFunction ::= "SOUNDEX" "(" StringPrimary ")"
*/
class SoundexFunction extends FunctionNode
{
public $stringExpression = null;
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->$stringExpression = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'SOUNDEX(' .
$this->$stringExpression->dispatch($sqlWalker) .
')';
}
}
config.yml 片段:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
entity_managers:
default:
auto_mapping: true
dql:
string_functions:
soundex: Mana\ClientBundle\DQL\SoundexFunction
查询功能:
protected function getMatches($incoming) {
$em = $this->getDoctrine()->getManager();
$fname = $incoming['fname'];
$sname = $incoming['sname'];
$sql = "select id, active, fname, sname, dob, phys_addr, phys_city
from ManaClientBundle:Client where SOUNDEX(fname) = SOUNDEX(:fname) and
SOUNDEX(sname) = SOUNDEX(:sname)
order by sname, fname";
$query = $em->createQuery($sql)
->setParameters(array(
'fname' => $fname,
'sname' => $sname,
));
return $query->getResult();
}