Reputation: 729
Im trying to store a document in a different collection by using Mongodb and symfony2
This is my controller who sets the document into the 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());}
As you can see, this is the example for the official documentation on DoctrineMongoDBBundle
But the problem is that by default it creates the document into a Collection named as the class, in my case is Post(), so the collection name is Post. I will like to save the document into a Collection named for example charlies_posts or any string as a variable.
Upvotes: 0
Views: 1032
Reputation: 729
Yeahp, as you say we can define that as a parameter.
Documents\User:
type: document
db: my_db
collection: charlies_post
In this case in the YAML mapping file a different collection may be selected but in my case i want to dinamically set the collection name because i have post related to the user so charlies posts should go to the charlies_post collection and peter posts should go to peter_post collection...
I suppose that there must be a method to set this but i cant find it..
Upvotes: 0
Reputation: 36
It's easy :) In the definition of your document class just use the "collection" parameter.
Here is an exemple :
<?php
namespace MyProject\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* @ODM\Document(
* collection="charlies_posts"
* )
*/
class Post
{
Upvotes: 2