我将createQueryBuilder与Symfony和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一起使用?
发布于 2015-01-05 04:54:18
您可以创建自己的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');发布于 2014-04-23 02:34:35
尝试安装Doctrine2扩展,它将为您提供所需的内容(无重音表达式):Doctrine2 Extensions
https://stackoverflow.com/questions/23227177
复制相似问题