Symfony2 + Need to use the regular expression in doctrine's mongodb ODM to check case insensitive check

时光怂恿深爱的人放手 提交于 2019-12-12 05:26:38

问题


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

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.

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)

Thank you so much.


回答1:


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();

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




回答2:


If using MongoDB\BSON\Regex yields "Class Not found" exception then you're using legacy driver and need to use \MongoRegex. As you've seen the whole driver is deprecated but you can still use it, also it's still the only one ODM officially supports (new driver can be used if combined with polyfill such as mongo-php-adapter).

So to answer your question, case insensitive search will look like (given $qb is a query builder):

$qb->field('something')->equals(new \MongoRegex("/regex/i"));


来源:https://stackoverflow.com/questions/36761669/symfony2-need-to-use-the-regular-expression-in-doctrines-mongodb-odm-to-check

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