Edward Louth
Edward Louth

Reputation: 1060

ClassNotFoundException: Attempted to load class "MongoId" from namespace

I'm using Symfony2 with the DoctrineMongoDbBundle

I get the following error when trying to use new MongoId()

ClassNotFoundException: Attempted to load class "MongoId" from namespace "..." in ...Controller.php line 64. Do you need to "use" it from another namespace?

The code in my controller is

// connect
$m = $this->container->get('doctrine_mongodb.odm.default_connection');

// select a database
$db = $m->selectDatabase('db');

// select a collection (analogous to a relational database's table)
$collection = $db->createCollection('Entity');

// find everything in the collection
$entity = $collection->findOne(array('_id' => new MongoId($id)));

I can use mongodb perfectly well with the ODM and if I remove the query it works fine too, i.e.

$entity = $collection->findOne();

Any help appreciated, thanks

Upvotes: 0

Views: 1548

Answers (1)

john Smith
john Smith

Reputation: 17906

use a backslash to get the correct scope :

$collection->findOne(array('_id' => new \MongoId($id)))

the \ is telling PHP to use the root namespace rather than yours.

Upvotes: 2

Related Questions