1
votes

I'm trying to create a new Symfony4 project with MongoDB.

First I created a Symfony4 project using this documentation: https://symfony.com/doc/current/setup.html

Then I included MongoDB using this documentation: http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html

I tried to follow the instructions as exactly as possible (for example I didn't need to add anything to app/AppKernel.php, but MongoDB was automatically added to config/bundles.php).

Now I think everything should work, but my Symfony app doesn't find the MongoDB Service:

You have requested a non-existent service "doctrine_mongodb". 
Did you mean one of these: "http_kernel", "request_stack", "router"?
in ServiceLocator.php (line 48)

Controller:

namespace App\Controller;

use App\Document\Chapter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends AbstractController {

    public function createAction() {
        $test = new Chapter();
        $test->setHeadline('Test');

        $dm = $this->get('doctrine_mongodb')->getManager();
        $dm->persist($test);
        $dm->flush();

        return new Response('Created product id '.$test->getId());
    }
}

However, If I execute this on the console:

php bin/console debug:container

I get a list of services including these:

doctrine_mongodb                                                                             Doctrine\Bundle\MongoDBBundle\ManagerRegistry
doctrine_mongodb.odm.default_connection                                                      Doctrine\MongoDB\Connection
doctrine_mongodb.odm.default_document_manager                                                Doctrine\ODM\MongoDB\DocumentManager
doctrine_mongodb.odm.document_manager                                                        alias for "doctrine_mongodb.odm.default_document_manager"

So the service seems to be there, but Symfony can't load it from my app.

Any idea how I could solve this? Is it possible that the Mongo-DB Server connection doesn't work and for some reason it isn't logged and the service just won't load?

2
The page symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.htm says "The page you are looking for does not exist. You may instead browse any of the following resources."Alex Blex
If I had to guess I might suspect that the mongodb service is private and needs to be injected. Not enough info in your question. Is the code you posted part of a controller? And did you extend from AbstractController? AbstractController restricts access to the container.Cerad
@AlexBlex This is the correct link (I edited my post): symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.htFels
@Cerad: I edited my post to include the full controller which was taken from this example: symfony.com/doc/current/bundles/DoctrineMongoDBBundle/…Fels
The only valid link is in the last comment, and it points to the docs for Symfony 3.Alex Blex

2 Answers

4
votes

You could use autowiring

use Doctrine\ODM\MongoDB\DocumentManager as DocumentManager;

and

public function createProduct(DocumentManager $dm)

2
votes

Try extending from "Controller" instead "AbstractController".

class DefaultController extends Controller