alcaeus / mongo-php-adapter

:link: Adapter to provide ext-mongo interface on top of mongo-php-library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

thmohd opened this issue · comments

I am having the following issue, anybody faced this issue before? Thanks in advance

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

} elseif ($id instanceof self || $id instanceof ObjectID) {
$this->objectID = new ObjectID((string) $id);
} else {
$this->objectID = new ObjectID();
}
} catch (\Exception $e) {
throw new MongoException('Invalid object ID', 19);

OS: Ubuntu 16
From Composer.json:
"alcaeus/mongo-php-adapter": "^1.1",
"mongodb/mongodb": "^1.1"

PHP 7.1

What version of ext-mongodb do you have installed? This error only appears if ext-mongodb is not installed or not loaded.

I am not sure if I have that, but here is what I have in composer.json

"alcaeus/mongo-php-adapter": "^1.1",
"doctrine/mongodb-odm": "^1.0",
"doctrine/mongodb-odm-bundle": "^3.0"
"mongodb/mongodb": "^1.1",

Also I installed:
sudo pecl install mongo
and enabled it extension=mongo.so

PHP 7.1
sudo pecl install mongo

It's one or the other - the mongo extension is not available for PHP 7. You need the new mongodb extension, on which the adapter builds.

Out of curiosity, did you follow the installation instructions in the readme? I might have to clarify the driver installation part and see if I can do away with the ignore-platform-reqs switch for composer.

I did try that and I was getting same error

That's strange - can you post the output of php -m?

I'm having this issue aswell.

Here is the output of my php -m: https://gist.github.com/yuparc/2ad72a95bba6bc237b38d0540355c10d

Weird. Could you run the following snippet from your projects root directory? php -r 'include("vendor/autoload.php"); var_dump(new \MongoId());'

The output should look like this:

Command line code:1:
class MongoId#3 (1) {
  private $objectID =>
  class MongoDB\BSON\ObjectID#2 (1) {
    public $oid =>
    string(24) "592168925a6f517ee8093581"
  }
}

If there are any error messages, please post them here.

Weird..
class MongoId#3 (1) { private $objectID => class MongoDB\BSON\ObjectID#2 (1) { public $oid => string(24) "592168fcef9473001f4d5841" } }

This is the output I'm getting.

Could this be related to symfony2? I'm using the DoctrineMongoDBBundle if that makes a difference.

Could this be related to symfony2?

Most likely not. You can copy the above code into a file and access it through your webserver. I'm expecting an error there, indicating that you've enabled the mongodb extension in CLI but not when running PHP through your webserver.

If calling the code through the webserver works as well, then we've got a weird problem.

You were right!

I managed to solve it by enabling the extension on php-fpm aswell and restarting php-fpm.

After that everything seemed to work, thanks for the help!

You're quite welcome - happy it's not a weird issue.

@thmohd could you please try the same steps that fixed the problem for @yuparc? I'm assuming you're having the same problem.

@thmohd If you need a more thorough explanation, just let me know, I would be happy to help :)

php -r 'include("vendor/autoload.php"); var_dump(new \MongoId());'
Result:

object(MongoId)#3 (1) {
["objectID":"MongoId":private]=>
object(MongoDB\BSON\ObjectID)#2 (1) {
["oid"]=>
string(24) "5921b2f5b9a12a557b262f91"
}
}

@yuparc how do you enable it in php-fpm, and what is the difference ? I noticed that I don't have php-fpm. Though, I installed it using sudo service php-fpm restart.

@thmohd
I solved it by installing php-mongodb from apt but I imagine if you pecl install mongodb (not mongo)..
and add it to your php.ini (mind you there is a /etc/php/7.1/cli and apache2 folder) you'll be fine aswell.

Worked 👍 !

Thank you @yuparc @alcaeus for your help.