2012-08-01 51 views
0

我試着使用的MongoDB和Symfony2的存儲在不同的集合的文檔 - MongoDB的與Symfony2的

這是我的控制器誰設置文檔進DB存儲在不同的集合的文檔。

public function createAction(){ 
$post = new Post(); 
$post->setUrl('A Foo Bar'); 
$post->setTitle('asdf'); 
$dm = $this->get('doctrine.odm.mongodb.document_manager'); 
$dm->persist($post); 
$dm->flush(); 
return new Response('Created post id '.$post->getId());} 

正如你所看到的,這是DoctrineMongoDBBundle

的官方文檔的例子,但問題是,默認情況下,它創建的文檔轉換成一個命名爲類集合,在我的情況是郵政(),所以集合名稱是Post。我想將文檔保存到一個名爲charlies_posts的集合或任何字符串作爲變量。

回答

2

這很簡單:)在文檔類的定義中,只需使用「collection」參數即可。

這裏是個例:

<?php 

namespace MyProject\Document; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; 

/** 
* @ODM\Document(
*  collection="charlies_posts" 
*) 
*/ 
class Post 
{ 
0

Yeahp,就像你說的,我們可以定義爲一個參數。

Documents\User: 
    type: document 
    db: my_db 
    collection: charlies_post 

在這種情況下,在不同的集合,可以選擇YAML映射文件,但在我的情況,我想dinamically設置的集合名稱,因爲我有相關的用戶後如此霹靂職位應該去charlies_post集合和彼得的帖子應該去peter_post收集...

我想,必須有一種方法來設置這個,但我找不到它..

相關問題