3

尝试注册 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();
}
4

1 回答 1

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";
于 2013-01-14T01:21:49.330 回答