【问题标题】:Doctrine MongoDB - Query on Document with multiple level of embedded documentDoctrine MongoDB - 查询具有多级嵌入文档的文档
【发布时间】:2012-08-03 02:10:42
【问题描述】:

我在使用 Doctrine MongoDB ODM 和 Symfony 2 嵌入文档时遇到了一些问题。

为了暴露问题,我有文档 product embedOne productInformation 和 productInformation embedOne productInformationAddress。

要查询,我使用类似的东西:

/**
 * @ODM\Document 
 **/
class product {
    /**
     * @ODM\EmbedOne(targetDocument="productInformation")
     **/
    protected $informations;
}

/**
 * @ODM\EmbeddedDocument 
 **/
class productInformations {
    /**
     * @ODM\EmbedOne(targetDocument="productInformationAddress")
     **/
    protected $address;

    /**
     * @ODM\Collection
     **/
    protected $attr1 = array();

    /**
     * @ODM\String
     **/
    protected $attr2
}

/**
 * @ODM\EmbeddedDocument 
 **/
class productInformationAddress {
    /** ... suff ... /*
}

当我查询时:

class productRepository {
    public function fetchOne($id) {
        return $this->createQueryBuilder()
            ->field('id')->equals($id)
            ->getQuery()
            ->getSingleResult();
    }
}

但是,我不明白为什么我不能得到$product->getInformations()->getAddress(),它总是返回null...

有什么想法吗?

【问题讨论】:

    标签: mongodb symfony doctrine-orm


    【解决方案1】:

    我认为您发布的代码没有问题,但通读 ODM 的嵌套嵌入式文档功能测试可能会有所帮助。在EmbeddedTest.php 中,感兴趣的方法可以是使用EmbeddedTestLevel2 的任何方法,这相当于您的productInformationAddress 类和testRemoveAddDeepEmbedded()

    【讨论】:

    • 谢谢您的回答,其实问题很简单...缓存...但是,您是对的,感谢您提供测试链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2018-09-04
    • 2012-12-20
    • 2011-04-26
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多