Mapping - Mongodb嵌入式文档在Symfony2中 [英] Mapping - Mongodb Embedded Document in Symfony2

查看:211
本文介绍了Mapping - Mongodb嵌入式文档在Symfony2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB在Symfony2中添加映射信息,如下所示: http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html [在添加信息部分]
但是我想做的是添加嵌入式文档。
这是我的文档中嵌入文档的一部分:

 _ id:,
last_name:
first_name:,
address:[
{
Street:,
City ,
Zip_Code:,
国家:

}
],
公司:
purshaed_items:[
{
items_id:,
category:,
price:,
date_of_purshae

}
]

所以你可以看到我有字段地址& Purshaed_items是嵌入式文档。
我发现这个链接 http://doctrine-orm.readthedocs.org/projects/doctrine-mongodb-odm/en/latest/reference/embedded-mapping.html
我想我的情况将是嵌入许多但是没有真正得到它的工作原理?



有人可以尝试解释它是如何工作的?



谢谢

解决方案

我会这样做:

  / ** @EmbedMany(targetDocument =PurshaedItem)* / 
private $ purshaedItems = array();

假设您有另一个名为PurshaedItem(PurchasedItem)的文档类。



目标文档将如下映射:

  / ** 
* @ MongoDB\EmbeddedDocument
* /
class PurchasedItem
{
//无论属性...
}
/ pre>

如果要查询嵌入式文档,可以在控制器中获得(推荐),这里是我简单的示例



为了得到它在twig模板中,这是另一个示例



这些例子来自简单的Symfony博客应用程序。有3个文件:帖子,标签和评论。帖子有标签和评论作为嵌入的属性。看看周围的代码,很简单。
如果答案对您有用,请点击以接受。


I am trying to add a mapping information in Symfony2 using MongoDB as shown here : http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html [at the Adding Information section] But what I want to do is to add an embedded documents as well. Here is a part of my document with embedded documents :

"_id" : "",
"last_name" :,
"first_name" : "",
"address" : [
{
    "Street" : "",
    "City" : "",
    "Zip_Code" : "",
    "Country": ""

}
],
"company" : ""
"purshaed_items" : [
{
    "items_id" : "",
    "category":"",
    "price":"",
    "date_of_purshae"

}
]

So as you can see I have the fields "Address" & "Purshaed_items" that are embedded documents. I found this link http://doctrine-orm.readthedocs.org/projects/doctrine-mongodb-odm/en/latest/reference/embedded-mapping.html I guess that my case will be the "Embed Many" but didn't really get how it works?

Can someone please try to explain how it works?

Thank you

解决方案

I would make it like this:

/** @EmbedMany(targetDocument="PurshaedItem") */
    private $purshaedItems = array();

Assuming that you have another document class named PurshaedItem (PurchasedItem perhaps).

And the target document would be mapped like this:

/**
 * @MongoDB\EmbeddedDocument
 */
class PurchasedItem
{
  // whatever properties ...
}

And if you want to query for embedded documents, you can get it in controller (recommended), here is my simple example.

For getting it in twig template, here is another example.

The examples are from simple Symfony blog application. There are 3 documents: Post, Tag, and Comment. Post has Tags and Comments as embedded properties. Take a look around the code, it is simple. If the answer has been useful to you, please click to accept it.

这篇关于Mapping - Mongodb嵌入式文档在Symfony2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆