MongoDB/doctrine: can't nest $or in $and

别等时光非礼了梦想. 提交于 2019-12-04 06:15:01

It sounds like you need to build a separate subquery before adding it to $q.

$q->addAnd(...) is evaluated immediately and adds itself to $q, but you want it to wait.

I don't have this package installed so I can't test, but this is just a hunch. Hope it helps.

$q = $doctrineOdm->createQueryBuilder('Business');
foreach (array('American','Mega') as $keyword) {
  $r = new \MongoRegex('/'.$keyword.'/i');
  $q->addAnd(
    $q->expr()->addOr($q->expr()->field('nm')->equals($r))
              ->addOr($q->expr()->field('dsc')->equals($r))
  );
}
print_r($q->getQuery()->getQuery()); // infinite recursion
$cursor = $q->getQuery()->execute();
Duy Huynh

You can try as follows:

$filters = array(
     new \MongoRegex('/American/i'),
     new \MongoRegex('/Mega/i')
);
$q = $doctrineOdm->createQueryBuilder('Business')
                 -> field('nm')  ->in($filters)
                 -> field('dsc') ->in($filters);

print_r($q->getQuery()->getQuery()); // infinite recursion
$cursor = $q->getQuery()->execute();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!