gpt4 book ai didi

mongodb - 试图从命名空间 "ObjectID"加载类 "MongoDB\BSON"

转载 作者:可可西里 更新时间:2023-11-01 10:48:21 26 4
gpt4 key购买 nike

我正在尝试使用 DoctrineMongoDBBundle,但它给了我这个错误:

Attempted to load class "ObjectID" from namespace "MongoDB\BSON". Did you forget a "use" statement for another namespace?

我找遍了某个原因,但找不到为什么会出现此错误。

我的 mongodb 安装正常,mongodb 状态告诉我它处于事件状态。我想我正确地设置了类...等等。错误可能来自哪里?

堆栈跟踪:

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 输出:

MongoDB extension version => 1.3.2, libbson bundled version => 1.8.1, libmongoc bundled version => 1.8.1

演示的简单示例:

<?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');
}
}

并且 composer.json 需要得到:

"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"
},

最佳答案

您好,您需要将“alcaeus/mongo-php-adapter”:“^1.1”作为您的包的一部分,并将预期的 mongo 扩展作为故障解决方案公开,例如

  "provide": {
"ext-mongo": "1.6.12"
},

一个完整的 Composer 文件可能是这样的

{
"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"
}
...
}

关于mongodb - 试图从命名空间 "ObjectID"加载类 "MongoDB\BSON",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47354109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com