2014-04-22 43 views
1

我使用createQueryBuilderSymfonyPGSQLSymfony,querybuilder,使用擴展從pgsql

我安裝PGSQL擴展unaccent在我的數據庫有:

CREATE EXTENSION unaccent; 

但是當我嘗試類似的查詢:

    $qb = $this->_em->createQueryBuilder(); 
        $qb->select(array('a')) 
        ->from('ProjectBundle:Account', 'a') 
        ->where('unaccent(a.firstname) LIKE unaccent(?1) OR unaccent(a.lastname) LIKE unaccent(?1)') 
        ->setParameters(array(1 => '%'.$search.'%')) 
        ->setMaxResults(5); 
      return $qb->getQuery()->getResult(); 

我有這樣的錯誤:Error: Expected known function, got unaccent

哪有我在Symfony和Doctrine中使用這個擴展?

回答

0

嘗試安裝Doctrine2擴展,它會給你你需要的東西(unaccent表達):Doctrine2 Extensions

+0

我需要什麼?像什麼 ? –

+0

@ClémentAndraud檢查它:http://symfony.com/doc/current/cookbook/doctrine/common_extensions.html – takeit

2

您可以創建自己的DQL功能,支持Unaccent:

namespace Your\Namepsace\Doctrine\DQL; 

use Doctrine\ORM\Query\AST\Functions\FunctionNode; 
use Doctrine\ORM\Query\Lexer; 

/** 
* Unaccent string using postgresql extension unaccent : 
* http://www.postgresql.org/docs/current/static/unaccent.html 
* 
* Usage : StringFunction UNACCENT(string) 
* 
*/ 
class Unaccent extends FunctionNode 
{ 
    private $string; 

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) 
    { 
     return 'UNACCENT(' . $this->string->dispatch($sqlWalker) .")"; 
    } 

    public function parse(\Doctrine\ORM\Query\Parser $parser) 
    { 
     $parser->match(Lexer::T_IDENTIFIER); 
     $parser->match(Lexer::T_OPEN_PARENTHESIS); 

     $this->string = $parser->StringPrimary(); 

     $parser->match(Lexer::T_CLOSE_PARENTHESIS); 
    } 

} 

你必須進行註冊學說配置

用symfony

根據to the doc here

# app/config/config.yml 
doctrine: 
    orm: 
     # ... 
     dql: 
      string_functions: 
       unaccent: Your\Namespace\Doctrine\DQL\Unaccent 

我們也想在捆綁配置,see an example自動完成。

教義單機

根據to the doctrine documentation(未測試):

$config = new \Doctrine\ORM\Configuration(); 
$config->addCustomStringFunction('unaccent', 'Your\Namespace\Doctrine\DQL\Unaccent');