Attempted to load class "ObjectID" from namespace "MongoDB\BSON"

1.2k views Asked by At

I'm trying to use DoctrineMongoDBBundle but it gives me this error :

Attempted to load class "ObjectID" from namespace "MongoDB\BSON". Did you forget a "use" statement for another namespace?

I've searched all over for a reason, but I could not find why I get this error.

My mongodb is installed alright, a mongodb status tells me it's active. I think I correctly set up the classes... etc. Where can the error be coming from?

Stack trace :

Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "ObjectID" from namespace "MongoDB\BSON". Did you forget a "use" statement for another namespace?

at vendor/alcaeus/mongo-php-adapter/lib/Mongo/MongoId.php:224

at MongoId->createObjectID(null) (vendor/alcaeus/mongo-php-adapter/lib/Mongo/MongoId.php:41)

at MongoId->__construct() (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Id/AutoGenerator.php:34)

at Doctrine\ODM\MongoDB\Id\AutoGenerator->generate(object(DocumentManager), object(Product)) (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php:1107)

at Doctrine\ODM\MongoDB\UnitOfWork->persistNew(object(ClassMetadata), object(Product)) (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php:1710)

at Doctrine\ODM\MongoDB\UnitOfWork->doPersist(object(Product), array('000000002ed55d6b00000000057952ca' => object(Product))) (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php:1674)

at Doctrine\ODM\MongoDB\UnitOfWork->persist(object(Product)) (vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentManager.php:412)

at Doctrine\ODM\MongoDB\DocumentManager->persist(object(Product)) (src/Acme/StoreBundle/Controller/DefaultController.php:30)

at Acme\StoreBundle\Controller\DefaultController->createAction()

at call_user_func_array(array(object(DefaultController), 'createAction'), array()) (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:153) at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1) (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php:68)

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true) (vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:169)

at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) (web/app_dev.php:29)

at require('/var/www/html/geoservicesgrandnancy/web/app_dev.php') (vendor/symfony/symfony/src/Symfony/Bundle/WebServerBundle/Resources/router.php:42)

php --ri mongodb | grep version outputs:

MongoDB extension version => 1.3.2, libbson bundled version => 1.8.1, libmongoc bundled version => 1.8.1

Simple example to demonstrate :

<?php

namespace Acme\StoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use MongoDB\BSON\ObjectID; //Adding this or not doesn't change anything

class DefaultController extends Controller
{
    /**
     * @Route("/")
     */
    public function indexAction()
    {
        var_dump(new MongoDB\BSON\ObjectID); die();
        return $this->render('AcmeStoreBundle:Default:index.html.twig');
    }
}

And composer.json requires got :

"require": {
        "php": ">=5.5.9",
        "alcaeus/mongo-php-adapter": "^1.1",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/mongodb-odm": "^1.2",
        "doctrine/mongodb-odm-bundle": "^3.4",
        "doctrine/orm": "^2.5",
        "incenteev/composer-parameter-handler": "^2.0",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^3.0.2",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.3.10",
        "symfony/symfony": "3.3.*",
        "twig/twig": "^1.0||^2.0"
    },
1

There are 1 answers

2
bitgandtter On

Hello you need to include "alcaeus/mongo-php-adapter": "^1.1" as part of your packages and also expose the expected mongo extension as a failure resolution like

  "provide": {
    "ext-mongo": "1.6.12"
  },

a complete composer file may be like

{
  "require": {
    "php": "^7.1.3",
    "ext-mongodb": ">=1.1.2",
    "alcaeus/mongo-php-adapter": "^1.1",
    "doctrine/mongodb-odm-bundle": "^3.3",
    ...
  },
  "provide": {
    "ext-mongo": "1.6.12"
  }
  ...
}