如何使用doctrine ine2和Symfony2创建原始MongoDB查询

时间:2022-05-16 06:47:54

I'm trying to query embedded documents using dot notation -- here are some examples:

我正在尝试使用点表示法查询嵌入式文档——这里有一些例子:

http://www.mongodb.org/display/DOCS/Dot+Notation+(Reaching+into+Objects)

http://www.mongodb.org/display/DOCS/Dot +符号+(达到+到+对象)

Since I'm hung up on the DQL/QueryBuilder API I'd like to just pass raw queries through Doctrine. I can't seem to figure this out within the constructs of Symfony2. The only code I can be sure of is:

由于我挂在DQL/QueryBuilder API上,所以我希望通过Doctrine传递原始查询。我似乎无法在Symfony2的结构中找到答案。我唯一能确定的代码是:

$dm = $this->get('doctrine.odm.mongodb.document_manager');
$dm->getSchemaManager()->ensureIndexes();
$repo = $dm->getRepository('MyBundle:MyDocument');
$qb = $repo->createQueryBuilder();

// insert magic here

$query = $qb->getQuery();
$result = $query->execute();

Here's how dot notation works:

点符号的工作原理如下:

$dm = $this->get('doctrine.odm.mongodb.document_manager');
$dm->getSchemaManager()->ensureIndexes();
$repo = $dm->getRepository('MyBundle:MyDocument');
$qb = $repo->createQueryBuilder();

$qb
    ->field('embedded_document_field.field1')->equals(1)
    ->field('embedded_document_field.field2')->equals('foo')
;

$query = $qb->getQuery();
$result = $query->execute();

1 个解决方案

#1


0  

This question is ancient, but in case someone comes across it, it's fairly straightforward to do this with the Query Builder:

这是一个古老的问题,但是如果有人遇到它,使用查询构建器进行查询是相当简单的:

    $qb = $this->createQueryBuilder();
    $qb->addOr($qb->expr()->field('embedded.embedded2.username')->equals($username));
    $qb->addOr($qb->expr()->field('embedded.embedded3.username')->equals($username));
    $qb->addOr($qb->expr()->field('embedded.embedded4.username')->equals($username));
    $qb->field('status')->lt(10);
    $involved = $qb->getQuery()->execute();
    return $involved;

I acknowledge this doesn't actually answer the question relating to passing through raw (which is my own question) but it does solve your problem.

我承认这实际上并没有回答与传递raw有关的问题(这是我自己的问题),但它确实解决了你的问题。

#1


0  

This question is ancient, but in case someone comes across it, it's fairly straightforward to do this with the Query Builder:

这是一个古老的问题,但是如果有人遇到它,使用查询构建器进行查询是相当简单的:

    $qb = $this->createQueryBuilder();
    $qb->addOr($qb->expr()->field('embedded.embedded2.username')->equals($username));
    $qb->addOr($qb->expr()->field('embedded.embedded3.username')->equals($username));
    $qb->addOr($qb->expr()->field('embedded.embedded4.username')->equals($username));
    $qb->field('status')->lt(10);
    $involved = $qb->getQuery()->execute();
    return $involved;

I acknowledge this doesn't actually answer the question relating to passing through raw (which is my own question) but it does solve your problem.

我承认这实际上并没有回答与传递raw有关的问题(这是我自己的问题),但它确实解决了你的问题。