Symfony2学说 - PostgreSQL的ILIKE子句? [英] Symfony2 Doctrine - ILIKE clause for PostgreSQL?

查看:127
本文介绍了Symfony2学说 - PostgreSQL的ILIKE子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用symfony2,doctrine 2.3和PostgreSQL 9.我一直在寻找几个小时,看看如何在地球上做一个 ILIKE 选择 QueryBuilder



似乎他们只有 LIKE 。在我的情况下,我正在搜索不区分大小写。
如何完成?

  //  - 这是喜欢; 
$ search ='user';
$ query = $ this-> createQueryBuilder('users');
$ query-> where($ query-> expr() - > like('users.username',$ query-> expr() - > literal('%:username%') )) - > setParameter(':username',$ search);


// - 这是我得到的[Syntax Error] line 0,col 86:Error:Expected =,< =,>,> ;> =,!=,获得'ILIKE'
$ search ='user';
$ query = $ this-> createQueryBuilder('users');
$ query- > where('users.username ILIKE:username') - > setParameter(':username',$ search);


解决方案

我不了解Symphony,但可以替换

  a ILIKE b 

  lower(a)LIKE lower(b)

你也可以尝试运算符 ~~ * ,这是 ILIKE
的同义词它具有略低的运算符优先级,所以您可能需要使用连接字符串的括号,您不能使用 ILIKE

  a ILIKE b || c 

成为

  a ~~ *(b || c)

手册关于模式匹配,从 LIKE / ILIKE 开始



我认为这个人有同样的问题,并得到一个答案:

http://forum.symfony-project.org/viewtopic.php?f=23&t=40424



显然,你可以使用SQL供应商特定的功能扩展Symfony2:

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/cookbook/dql-user-defined-functions.html



我不是ORM和框架的粉丝,只是为了保持便携式(几乎没有工作),捕捉Postgres的丰富功能。


I'm currently using symfony2, doctrine 2.3 and PostgreSQL 9. I've been searching for a couple of hours now to see HOW on earth do I do a ILIKE select with QueryBuilder.

It seems they only have LIKE. In my situation, though, I'm searching case-insensitive. How on earth is it done?

// -- this is the "like";
$search = 'user';
$query = $this->createQueryBuilder('users');
$query->where($query->expr()->like('users.username', $query->expr()->literal('%:username%')))->setParameter(':username', $search);


// -- this is where I get "[Syntax Error] line 0, col 86: Error: Expected =, <, <=, <>, >, >=, !=, got 'ILIKE'
$search = 'user';
$query = $this->createQueryBuilder('users');
$query->where('users.username ILIKE :username')->setParameter(':username', $search);

解决方案

I don't know about Symphony, but you can substitute

a ILIKE b

with

lower(a) LIKE lower(b)

You could also try the operator ~~*, which is a synonym for ILIKE It has slightly lower operator precedence, so you might need parenthesis for concatenated strings where you wouldn't with ILIKE

a ILIKE b || c

becomes

a ~~* (b || c)

The manual about pattern matching, starting with LIKE / ILIKE.

I think this guy had the same problem and got an answer:
http://forum.symfony-project.org/viewtopic.php?f=23&t=40424

Obviously, you can extend Symfony2 with SQL vendor specific functions:
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/cookbook/dql-user-defined-functions.html

I am not a fan of ORMs and frameworks butchering the rich functionality of Postgres just to stay "portable" (which hardly ever works).

这篇关于Symfony2学说 - PostgreSQL的ILIKE子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆