Search code examples
mongodbsymfony-2.8

Symfony 2 Persist data into MongoDB using DoctrineMongoDBBundle


I want to insert data into MongoDB with Symfony 2 using DoctrineMongoDBBundle. I have followed this tutorial from Symfony Documentation. In my controller class i have this function

  *public function createAction()
       {
            $product = new Product();
            $product->setName('Apple Juice');
            $product->setPrice('19.99');
            $product1 = new Product();
            $product1->setName('Orange Juice');
            $product1->setPrice('19.98');
            $product2 = new Product();
            $product2->setName('Pineapple Juice');
            $product2->setPrice('19.99');
            $dm = $this->get('doctrine_mongodb')->getManager();
            $dm->persist($product);
            $dm->persist($product1);
            $dm->persist($product2);
            $dm->flush();
     }*

and I've also created a schema using Command Line php app/console doctrine:mongodb:shema:create command. Now my schema is created, but I can't persist items. It only has database name and Product collection, which is empty. My Product class contains 3 fields, plus getter and setter functions for protected members. So, please help me understand what's going wrong and why items are not persisted when I run function. Thanks in advance.


Solution

  • It is required to add a Route for createAction() in configuration.