【问题标题】:ClassNotFoundException: Attempted to load class "MongoId" from namespaceClassNotFoundException:试图从命名空间加载类“MongoId”
【发布时间】:2014-07-04 10:18:08
【问题描述】:

我将 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)));

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

$entity = $collection->findOne();

感谢任何帮助,谢谢

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢,完美运行,试图解决这个问题有点疯狂
    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    相关资源
    最近更新 更多