A
A
alpha9172017-07-26 08:31:56
Yii
alpha917, 2017-07-26 08:31:56

How to generate a query with a regular expression in Yii2?

I use Yii2 + Yii2-mongodb
A query similar to sql is required: name LIKE "%text%"
As I understand it, in Mongo this is only regular expressions, so I write:

$q = [
  'name' => [
    '$regex' => $text,
    '$options' => 'i',
  ]
];
$result['mobs'] = Biotype::find($q)->limit(10)->all();

according to https://docs.mongodb.com/manual/reference/operator...
Tried
$q = ['name' => new \MongoDB\BSON\Regex('*('.trim($text).')*', 'ig')];
$result['mobs'] = Biotype::find($q)->limit(10)->all();

In both cases, I get the same result - the first 10 records without any filtering, just like if I send an empty condition at all. CHADNT?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alpha917, 2017-07-26
@alpha917

i blunted.

$q = [
  'name' => [
    '$regex' => $text,
    '$options' => 'i',
  ]
];
$result['mobs'] = Biotype::find()->where($q)->limit(10)->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question