1

programmers, I uses symfony doctrine and mongodb. Today I tried to create following query:

$q = $this->createQueryBuilder("user");
$keywords = explode("_", $keyword);
foreach ($keywords as $keyword) {
    $r = new \MongoRegex('/'.$keyword.'/i');
    $q->addAnd(
    $q->addOr($q->expr()->field('firstName')->equals($r))
        ->addOr($q->expr()->field('lastName')->equals($r))
    );
}
return $q->getQuery()->execute();

but after execute() i got following error "zero-length keys are not allowed, did you use $ with double quotes". query must realize user search by keywords. can you help me fix that, please?

4

1 回答 1

1

如果有人感兴趣,我通过使用单独的“或”表达式找到了解决方案:

    $q = $this->createQueryBuilder();
    $keywords = explode(" ", $keyword);
    foreach ($keywords as $keyword) {
        $r = new \MongoRegex('/'.$keyword.'/i');
        $q->addOr($q->expr()->field('firstName')->equals($r));
        $q->addOr($q->expr()->field('lastName')->equals($r));
    }
   return $q->getQuery()->execute();

希望我的经验会有所帮助

于 2013-06-12T08:12:00.960 回答