0

Hi there I'm try to connect mongo DB(Version 3.4.18) using CakePHP (version 3.5.1) with Apache server on PHP (7.2.4) and facing below error

 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}

for mongo db connection trying below code

public function __connectmongodb()
{

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

Note: I have to do this in mongo DB(Version 3.4.18) using CakePHP (version 3.5.1) with Apache server on PHP (7.2.4) these specific versions

UPDATE - we trying on windows using php_mongodb-1.4.0-7.2-ts-vc15-x64 this .dll file

Omkar
  • 3,040
  • 1
  • 22
  • 42
  • Do you have the driver for MongoDB installed and included with your php? – LLJ97 Jul 01 '19 at 11:11
  • No actually we want this using **MongoClient** not **MongoDB\Driver\Manager** – Omkar Jul 01 '19 at 11:13
  • Of course but PHP needs a driver to connect to MongoDB. It doesn't matter what connector you want to use, they all need the driver (.dll on windows and .so on *nix) – LLJ97 Jul 01 '19 at 11:14
  • we trying on windows using `php_mongodb-1.4.0-7.2-ts-vc15-x64` this .dll file – Omkar Jul 01 '19 at 11:17

1 Answers1

3

php_mongodb-1.4.0-7.2-ts-vc15-x64 is the "new" or rather the only officially supported driver https://pecl.php.net/package/mongodb. Its usage is documented here https://www.php.net/manual/en/set.mongodb.php and is basically uses MongoDB\Driver\Manager.

The MongoClient is from legacy and deprecated long time ago driver: https://pecl.php.net/package/mongo. It is documented here https://www.php.net/manual/en/book.mongo.php

There is no official support of the legacy driver for PHP 7: https://jira.mongodb.org/browse/PHP-1474

Last time the legacy driver was listed on https://docs.mongodb.com/ecosystem/drivers/php/ back in February 2018. By then it was claimed to be compatible with mongodb server v2.4, v2.6, and v3.0 with no compatibility with databases v3.2+.

The same February 2018 mongodb v3.0 retired. Support of Mongodb v3.2 has been dropped later in 2018 and v3.4 is counting its last months of support till January 2020.

Long story short, no you don't actually want this using MongoClient.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75