2

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"
    },
  • Have you tried cache delete and restart web server ? – Mz1907 Nov 17 '17 at 15:33
  • @Mz1907 yes.... –  Nov 17 '17 at 15:35
  • Have you removed both dev and prod cache ? – Mz1907 Nov 17 '17 at 15:38
  • @Mz1907 trust me, yes xD I did all obvious things –  Nov 17 '17 at 15:38
  • Do you have the mongo db driver installed? (Sorry, just a sanity check.) – Ollie in PGH Nov 17 '17 at 15:52
  • 1
    @ASOlivieri yes. php --ri mongodb | grep version outputs MongoDB extension version => 1.3.2, libbson bundled version => 1.8.1, libmongoc bundled version => 1.8.1 –  Nov 17 '17 at 15:58
  • Stack trace should hint where the error's coming from. As a side note, instead of claiming "I did everything correctly" you will be better with posting the code you **think** you did right. The error proves something's wrong there. – Alex Blex Nov 17 '17 at 16:03
  • @AlexBlex I added the stack trace. And I tried so many things that I lost count, that's why I didn't write what I tried exactly. And I don't think I tried everything, just the obvious things found on the web when I wrote my error on google... –  Nov 17 '17 at 16:07
  • Confirm it works in both cli and fpm: `php -r 'var_dump(new \\MongoDB\\BSON\\ObjectID);'` should result with no error. Then add a first line after ` – Alex Blex Nov 17 '17 at 18:07
  • @AlexBlex your first code results in no error in the terminal of the project. Second on the other-hand gave me the same error as the original post when I wrote it at the start of my controller (as I have no index.php anywhere x)) –  Nov 17 '17 at 19:22
  • You need to restart php-fpm or apache whatever you use for http requests – Alex Blex Nov 17 '17 at 19:25
  • @AlexBlex already did so many times while trying to debug the problem ... –  Nov 17 '17 at 19:36
  • Then the extension is not in the fpm/apache php config. Add it there the same way you did for cli. – Alex Blex Nov 17 '17 at 19:42
  • @AlexBlex I do have in my /etc/php/7.0/apache2/php.ini : extension_dir = "./usr/lib/php/20151012/" and extension=mongodb.so –  Nov 17 '17 at 19:47
  • Apparently it is not loaded for some reason. Put `phpinfo() ` in index.php to confirm which configs were loaded and the extension is enabled. – Alex Blex Nov 18 '17 at 01:04

1 Answers1

0

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"
  }
  ...
}
bitgandtter
  • 2,179
  • 6
  • 31
  • 60
  • Thank you for trying but I already have that set up and it doesn't work :) I didn't have ext-mongodb in the require section tho, but adding it doesn't correct the error. I did have provide. –  Nov 17 '17 at 20:05
  • thats wear i have a project with that configuration working just fine, it may be something else on your versions or configurations, maybe the extension is not enabled correctly for the webserver only for cli – bitgandtter Nov 17 '17 at 20:15