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

最佳答案

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";

关于php - Symfony2 注册 MySQL soundex 失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14309983/

10-12 23:44