Doctrine query + LIKE expression

青春壹個敷衍的年華 提交于 2019-12-11 01:39:06

问题


I have problems to create a Doctrine Query with LIKE Expression:

QUERY:

$dql    = "SELECT u FROM Users u JOIN u.group g WHERE g.name LIKE lower('ADMIN')";
$query  = $em->createQuery($dql);
$result = $query->getResult();

ERROR:

QueryException: [Syntax Error] line 0, col 147: Error: Expected Doctrine\ORM\Query\Lexer::T_STRING, got 'lower'

LOWER was just an example, I need to use other functions in LIKE EXPRESSION, for example, unnacent...

How can I change Like Expression to support function on both sides?

Example: LOWER(unaccent(u.login)) LIKE LOWER(unaccent('ADMIN'))


回答1:


The like string needs to have % signs. If you want something that starts with ADMIN then you would write ADMIN% if you want something that ends with ADMIN you would write %ADMIN and finally if you want it to contain ADMIN then it would be %ADMIN%.

Maybe to return a string you can use the CONCAT function of doctrine or you can do it via PHP.




回答2:


I tested this with QueryBuilder and there doesn't seem to be a solution. The second parameter will not take a function, so I would suggest switching the parameters around:

$dql    = "SELECT u FROM Users u JOIN u.group g WHERE UPPER(g.name) LIKE 'ADMIN'";


来源:https://stackoverflow.com/questions/10916797/doctrine-query-like-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!