2

I'm using Symfony2 MongoDB + Doctrine and I want to tell doctrine to save my objects in collections with different name from the name of the class that defines the object. Also the name of the new collection should be the id of an object in different collection. For example I have a class called Posts and I want to save them in a collection named after the ID of the user in the original User collection. This means that I need to tell doctrine to save all new posts in a collection named e.g. User555 and I should be able to tell doctrine to create this collection and save there during runtime.

I can see that I can change the name of the collection statically with configuring it in the files like here: Storing a document in a different collection - Mongodb with symfony2

But I need to be able to create collections at runtime.

I will be thankful if someone points me in the right direction!

Cheers.

Community
  • 1
  • 1
Kik Minev
  • 103
  • 1
  • 3
  • 9

1 Answers1

1

When you use the ORM you can do

$em->getClassMetadata('\AcmeBundle\Entity\Something')->setTableName('test')

Using the ODM you should be able to do

`$dm->getClassMetadata('\AcmeBundle\Document\Something')->setCollection('test')

I looked through the Doctrine code and it's possible. However note that by doing this you're changing the collection used for that Document for the life of the script, unless you set it back.

I don't know of any reasonable way to do this for just one entity at a time. Probably would be best to wrap the ODM by creating your own persister service.

james_t
  • 2,723
  • 1
  • 15
  • 20
  • 2
    Work to change the collection first time but when I have a for each statement it just keep the first set don't update with the new value – Gafitescu Daniel Jun 26 '13 at 16:06