ClassNotFoundException:尝试从命名空间加载类“MongoId”

时间:2014-07-04 10:18:09

标签: mongodb symfony doctrine-orm mongodb-query doctrine-odm

我将Symfony2与DoctrineMongoDbBundle一起使用

尝试使用new MongoId()

时出现以下错误
ClassNotFoundException: Attempted to load class "MongoId" from namespace "..." in ...Controller.php line 64. Do you need to "use" it from another namespace?

我的控制器中的代码是

// 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)));

我可以很好地使用mongodb与ODM,如果我删除查询它也可以正常工作,即

$entity = $collection->findOne();

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:2)

使用反斜杠来获得正确的范围:

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

\告诉PHP使用root命名空间而不是你的命名空间。