Mongo 没有作曲家的 drupal 7 模块中的数据库客户端库
Mongo db client library in drupal 7 module without composer
我正在尝试使用客户端库 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
在 myModule.module
文件内 -
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\
的输出(名称 space 格式),
然后我所要做的就是将 \ 替换为 / 使用
$class = str_replace('\', '/', $class);
并从 url 中删除 MongoDB。
仍然不知道它给出 MongoDB\Client\
的原因
我正在尝试使用客户端库 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
在 myModule.module
文件内 -
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\
的输出(名称 space 格式),
然后我所要做的就是将 \ 替换为 / 使用
$class = str_replace('\', '/', $class);
并从 url 中删除 MongoDB。
仍然不知道它给出 MongoDB\Client\