I can not query with like % in collection of phalcon

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-05 06:33:32

问题


I use phalcon with mongo db. Model is Collection. I want use find to get

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

I see in mongo db have query

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

But in collection of Phalcon. How do I code it.

I tried Users::find([['Username' => "/TienNguyen/"]]); It is not working.

Please help me


回答1:


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.




回答2:


Yeah, What you wanted was the $regex operator form instead of passing in a regex as a string. While that form works in the shell, most drivers are happier with the operator syntax:

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.

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.




回答3:


Found a solution

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



回答4:


You could do something like:

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



回答5:


Please, try it:

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

Good Luck!



来源:https://stackoverflow.com/questions/21971727/i-can-not-query-with-like-in-collection-of-phalcon

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