0

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.

2 Answers 2

2

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
{
Sign up to request clarification or add additional context in comments.

Comments

0

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.