简体   繁体   English

我无法在phalcon集合中使用类似%的查询

[英]I can not query with like % in collection of phalcon

I use phalcon with mongo db. 我在mongo db中使用了phalcon。 Model is Collection . 模型是集合 I want use find to get 我想使用查找获得

SELECT * FROM Users WHERE Username LIKE '%TienNguyen%'

I see in mongo db have query 我在mongo db中看到查询

b.users.find(
      { Username: /TienNguyen/ }
)

But in collection of Phalcon. 但在收集Phalcon。 How do I code it. 我该如何编码。

I tried Users::find([['Username' => "/TienNguyen/"]]); 我尝试了Users::find([['Username' => "/TienNguyen/"]]); It is not working. 它不起作用。

Please help me 请帮我

Yeah, What you wanted was the $regex operator form instead of passing in a regex as a string. 是的,您想要的是$ regex运算符表单,而不是将regex作为字符串传递。 While that form works in the shell, most drivers are happier with the operator syntax: 尽管该格式可以在Shell中使用,但大多数驱动程序对运算符语法的要求更高:

Users::find([[ 'Username' => [ '$regex' => 'TienNguyen' ] ]])

Be very careful using $regex as the page suggests. 非常小心使用正则表达式$作为页面显示。 You can ruin performance if you are not searching from the* start * of the string as in "^TienNguyen" using the caret operator. 如果您不使用尖号运算符,而不是像"^TienNguyen"那样从字符串的* 开头 *搜索,可能会破坏性能。

For reference, just JSON decode any of the standard argument forms from the Mongo manual to get your language equivalent structure for most native driver functions. 作为参考,只需JSON解码Mongo手册中的任何标准参数形式即可获得大多数本机驱动程序功能的等效语言结构。

Found a solution 找到了解决方案

 $objs = Items::find(
        [
            [                    
                'title' => [
                    '$regex' => $query,
                    '$options' => 'i'
                ]
            ]               
        ]
    );

As was mentioned above, regex searches that are not anchored at the beginning of the string can be very expensive because they require a full table scan. 如上所述,未锚定在字符串开头的正则表达式搜索可能非常昂贵,因为它们需要全表扫描。 Depending on your use case you might consider using a text index instead. 根据您的用例,您可能会考虑使用文本索引 It provides support for case-insensitive non-anchored searches including suffix stemming. 它为不区分大小写的非锚定搜索(包括后缀词干)提供支持。

You could do something like: 您可以执行以下操作:

$Users = \Users::query()
            ->where(name LIKE :name:)
            ->bind(array('name' => '%' . $name . '%'))
            ->execute();

Please, try it: 请尝试一下:

Users::find([['Username' => new MongoRegex("/TienNguyen/")]]);

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM