Symfony2 + 需要使用doctrine的mongodb ODM中的正则表达式来检查不区分大小写的检查 [英] Symfony2 + Need to use the regular expression in doctrine's mongodb ODM to check case insensitive check

查看:46
本文介绍了Symfony2 + 需要使用doctrine的mongodb ODM中的正则表达式来检查不区分大小写的检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对查询进行不区分大小写的检查.以前我使用 MongoRegex,如 这张票

I want to do the case insensitive check for the query. previously I am using MongoRegex as describe in the this ticket

但根据 Php.net 现在已弃用,因此不能使用它,他们建议使用 MongoDB\BSON\Regex 但我不确定如何在 symfony2 中使用它.但如果我尝试使用它,它会抛出 class not found 异常.

But as per the Php.net this is now deprecated, so can't use it, they suggest to use MongoDB\BSON\Regex but I am not sure how use this in symfony2. but it throws class not found exception if I try to use it.

请告诉我可能的解决方案.或任何其他方式在 ODM 自定义查询中进行不区分大小写的检查(可能在字段功能中)

Please let me know the possible solution. Or any other way to do the case insensitive check in the ODM custom query (may be in field function)

非常感谢.

推荐答案

我编写了如下自定义查询:

I have written custom query as follows :

$name = 'abc'
$db->createQuerBuilder()
   ->distinct('username')
   ->field('username')->where('function(){ var pattern = /^' . $name . '$/i; return pattern.test(this.username); }')
   ->getQuery()
   ->execute()
   ->getSingleResult();

我使用了 mongodbs where 函数和 JavaScript 测试函数.参考链接

I have used mongodbs where function with JavaScript test function. Reference link

这篇关于Symfony2 + 需要使用doctrine的mongodb ODM中的正则表达式来检查不区分大小写的检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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