在文件Controller \ AppController.php中找不到类“ MongoClient”

时间:2019-07-01 11:06:56

标签: php mongodb cakephp php-7.2 cakephp-3.5

您好,我尝试使用mongo DB(Version 3.4.18)连接CakePHP (version 3.5.1) with Apache server on PHP (7.2.4)并遇到以下错误

 2019-07-01 10:41:09 Error: [Error] Class 'MongoClient' not found in E:\xampp\htdocs\myproject\src\Controller\AppController.php on line 142
Request URL: /
Stack Trace:
#0 E:\xampp\htdocs\myproject\src\Controller\PagesController.php(219): App\Controller\AppController->__connectmongodb()
#1 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Controller\Controller.php(441): App\Controller\PagesController->home()
#2 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(119): Cake\Controller\Controller->invokeAction()
#3 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(93): Cake\Http\ActionDispatcher->_invoke(Object(App\Controller\PagesController))
#4 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\BaseApplication.php(103): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#5 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#6 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php(107): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#7 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#8 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php(88): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#9 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#10 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php(93): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#11 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#12 E:\xampp\htdocs\myproject\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php(52): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#13 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(65): DebugKit\Middleware\DebugKitMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#14 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#15 E:\xampp\htdocs\myproject\vendor\cakephp\cakephp\src\Http\Server.php(81): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#16 E:\xampp\htdocs\myproject\webroot\index.php(40): Cake\Http\Server->run()
#17 {main}

尝试以下代码进行mongo db连接

public function __connectmongodb()
{

    $m = new \MongoClient('mongodb://localhost:27017');
    return $db = $m->myDb;
}

注意:我必须在mongo DB(Version 3.4.18)中使用CakePHP (version 3.5.1) with Apache server on PHP (7.2.4)这些特定版本

更新-我们尝试在Windows上使用php_mongodb-1.4.0-7.2-ts-vc15-x64这个.dll文件

1 个答案:

答案 0 :(得分:3)

php_mongodb-1.4.0-7.2-ts-vc15-x64是“新”驱动程序,或者是唯一受官方支持的驱动程序https://pecl.php.net/package/mongodbhttps://www.php.net/manual/en/set.mongodb.php此处记录了它的用法,基本上使用了MongoDB\Driver\Manager

MongoClient来自旧版本,很久以前不推荐使用驱动程序:https://pecl.php.net/package/mongo。它记录在这里https://www.php.net/manual/en/book.mongo.php

PHP 7的旧版驱动程序没有官方支持:https://jira.mongodb.org/browse/PHP-1474

上次在https://docs.mongodb.com/ecosystem/drivers/php/February 2018中列出了旧版驱动程序。到那时为止,它已宣称与mongodb服务器v2.4,v2.6和v3.0兼容,而与数据库v3.2 +不兼容。

相同的2018年2月mongodb v3.0退役了。 Mongodb v3.2的支持已于2018年晚些时候取消,而v3.4则将其最后几个月的支持计算到2020年1月。

长话短说,不,您实际上不需要使用MongoClient