注册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) .
')';
}
}
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();
}
答案 0 :(得分:2)
Geo,不知道为什么它不适合你。我用QueryBuilder试了一下它就可以了。但是,我确实必须将$this->$stringExpression
的用法替换为$this->stringExpression
。
编辑:
我刚刚在裸应用程序中测试了您的代码,问题不在SOUNDEX中,而是在您的SQL中。尝试为您的实体分配SQL别名:
$sql = "select client.id, client.active, client.fname, client.sname, client.dob, client.phys_addr, client.phys_city
from ManaClientBundle:Client client where SOUNDEX(client.fname) = SOUNDEX(:fname) and
SOUNDEX(client.sname) = SOUNDEX(:sname)
order by client.sname, client.fname";