我正在尝试使用客户端库https://github.com/mongodb/mongo-php-library/blob/master/docs/tutorial/install-php-library.txt从drupal连接到mongo。我下载了源代码。现在我想在。module文件
中使用它 myModule
模块内部目录结构-
——/MongoDB (src文件夹的库)
——myModule.module
——myModule.info
Inside myModule.module
file -
function my_autoloader($class) {
print_r($class);
include DRUPAL_ROOT.'/sites/all/modules/myModule/MongoDB/' . $class . '.php';
}
spl_autoload_register('my_autoloader');
function myModule_init() {
//sample code to connect to mongo
$client = new MongoDB'Client("mongodb://xx.xx.xx.xx:27017");
$collection = $client->demo->beers;
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'";
}
我得到错误
Fatal error: Class 'MongoDB'Client' not found
我做错了什么。你知道吗??
注意:当我尝试使用这个
时,我能够连接 $m = new MongoDB'Driver'Manager("mongodb://xx.xxx.xx.xx:27017");
echo "Connection to database successfully";
为什么不使用客户端?
问题是print_r($class);
给出的输出像MongoDB'Client'
(在名称空间格式),然后我所要做的就是用
$class = str_replace('''', '/', $class);
和删除MongoDB从url。
仍然不知道为什么给MongoDB'Client'