问题描述
我使用的是Doctrine ODM,并且无法通过参考文档查询嵌入式文档。
考虑以下文档:
<?php
/ ** @Document * /
class TopCategory
{
/ ** EmbedMany(targetDocument =SubCategory)* /
private $ subCategories;
}
/ ** @EmbeddedDocument * /
class SubCategory
{
/ ** ReferenceMany(targetDocument = 产品)* /
私人$产品;
}
/ ** @Document * /
class Product
{
/ ** @String * /
private $ name;
}
现在我想知道如何找到一个TopCategory SubCategory)按照产品,我尝试了几种不同的方法来实现这一点,一种方法是有效的,但是有点黑客。
第一种方法不起作用: / p>
$ category = $ dm-> createQueryBuilder('TopCategory')
- > field('subCategories.products ') - > includesReferenceTo($ someProduct)
- > getQuery() - > execute();
// ...给出Doctrine\ODM\MongoDB\MongoDBException:在类'TopCategory'中找不到字段'subCategories.products'的映射'
第二种方法不起作用:
$ category = $ dm-> createQueryBuilder('SubCategory')
- > field('products') - > includesReferenceTo($ someProduct)
- > getQuery() - > execute() ;
// ...返回null
第三,工作解决方法:
$ category = $ dm-> createQueryBuilder('SubCategory')
- > field('products。$ id') - > equals(new \MongoId($ someProduct-> getId()))
- > getQuery() - > execute();
// ..工作,但似乎hackish
我正在使用最新的GitHub和MognoDB v1.8.0
这是怎么回事?
注意:有趣的是,Doctrine ODM可以直接返回嵌入的文档。
如果您使用 ReferenceMany
或 ReferenceOne
您不能通过任何参考文档字段查询,除了参考文档ID,因为在mongodb参考文档中存储如下:
{
pre>
$ id:'id',
$ db:'referenced_doc_db_name',
$ ref:'referenced_doc_collection_name'
}
ReferenceOne,ReferenceMany内部在驱动程序中完成,如果需要加载一些具有引用驱动程序的文档,则会发送其他请求来加载引用的文档。
所以,以下查询不是hackish;):
$ category = $ dm-> createQueryBuilder('子类别)
- > field('products。$ id') - > equals(new \MongoId($ someProduct-> getId()))
- > getQuery() - >执行();
// ..工作,但似乎hackish
如果您需要查询某些参考字段(id除外)您应该使用
embedOne
或embedMany
而不是引用。I'm using Doctrine ODM and am having trouble querying embedded documents by a referenced document.
Consider the follow documents:
<?php /** @Document */ class TopCategory { /** EmbedMany(targetDocument="SubCategory") */ private $subCategories; } /** @EmbeddedDocument */ class SubCategory { /** ReferenceMany(targetDocument="Product") */ private $products; } /** @Document */ class Product { /** @String */ private $name; }
Now I am wondering how I could find a TopCategory (or SubCategory) by product, I've tried a few different ways to achieve this, one method works but is a bit hackish.
First way, doesn't work:
$category = $dm->createQueryBuilder('TopCategory') ->field('subCategories.products')->includesReferenceTo($someProduct) ->getQuery()->execute(); // ... gives Doctrine\ODM\MongoDB\MongoDBException: No mapping found for field 'subCategories.products' in class 'TopCategory'.'
Second way, doesn't work:
$category = $dm->createQueryBuilder('SubCategory') ->field('products')->includesReferenceTo($someProduct) ->getQuery()->execute(); // ... returns null
Third, working workaround:
$category = $dm->createQueryBuilder('SubCategory') ->field('products.$id')->equals(new \MongoId($someProduct->getId())) ->getQuery()->execute(); // .. works, but seems hackish
I am using the latest from GitHub and MognoDB v1.8.0What's the go with this?
NOTE: Interesting how Doctrine ODM lets you return an embedded document directly.
解决方案If you using
ReferenceMany
orReferenceOne
you can't query by any reference document field, except reference document id, since in mongodb reference document storing like this:{ $id: 'id', $db: 'referenced_doc_db_name', $ref: 'referenced_doc_collection_name' }
ReferenceOne, ReferenceMany done internally in driver and if need load some document that have references driver send additional requests to load referenced documents.
So, following query not hackish ;):
$category = $dm->createQueryBuilder('SubCategory') ->field('products.$id')->equals(new \MongoId($someProduct->getId())) ->getQuery()->execute(); // .. works, but seems hackish
If you need query on some reference field (except id) you should use
embedOne
orembedMany
instead of reference.这篇关于如何通过引用的文档查询嵌入式文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!