我正在尝试将mongoDB与phalcon框架连接起来。我的代码如下:
index.php文件
<?php
if ($handle = opendir('include/models')) {
while (false !== ($entry = readdir($handle))) {
if (preg_match('/\.php$/', $entry)) {
require_once "include/models/$entry";
}
}
closedir($handle);
}
require 'vendor/autoload.php';
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Model;
$app = new Phalcon\Mvc\Micro();
$di = new \Phalcon\DI\FactoryDefault();
$config = new Phalcon\Config\Adapter\Ini('include/config/config.ini');
// Simple database connection to localhost
$di->set('mongo', function() {
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
return $manager->selectDb("MyApi");
},true);
$di->set('collectionManager', function () {
return new Phalcon\Mvc\Collection\Manager();
});
$app->setDI($di);
$directory = new User_detail();
$app->get('/userdetail/{code}', function ($code) use ($app,$directory) {
$directory->getUser($app,$code);
});
$app->notFound(function () use ($app) {
$app->response->setStatusCode(424, "Method Failed")->sendHeaders();
echo json_encode(array('status' => 'ERROR', 'messages' => "Method Failed"));
});
$app->handle();
?>模型文件User_detail.php
<?php
use Phalcon\Mvc\Collection;
use Phalcon\Mvc\Model;
use MongoDB\Driver\Manager;
use Phalcon\Mvc\Micro;
use Phalcon\Db\Column;
class User_detail extends Collection
{
public function initialize()
{
$this->setSource("User");
}
function getUser($app,$code)
{
$robot = User_detail::findFirst(
[
[
"Name" => "android",
]
]
);
echo $robot->Name; die;
}
}
?> 我的错误就像
错误:调用index.php中未定义的方法MongoDB\Driver\Manager::selectDb()
有什么问题吗?
发布于 2017-03-11 16:21:33
首先,我使用composer将Phalcon孵化器安装到我的供应商目录中:
composer require phalcon/incubator然后,我确保加载了供应商库:
include_once SITE_ROOT . 'vendor/autoload.php';然后我使用在vendor/phalcon/incubator/Library/Phalcon/Db/Adapter/MongoDB/Client.php中找到的\Phalcon\Db\Adapter\MongoDB\Client()
在services.php中:
$di->setShared('mongo', function () use ($config) {
$mongo = new \Phalcon\Db\Adapter\MongoDB\Client();
return $mongo->selectDatabase($config->mongodb->dbname);
});然后确保我的(vendor/phalcon/incubator/Library/Phalcon/Mvc/MongoCollection.php).扩展\ MongoCollection \Mvc\MongoCollection MongoCollection
模型/MyCollectionStats.php Stats.php:
<?php
namespace Common\Models;
class MyCollectionStats extends \Phalcon\Mvc\MongoCollection
{...发布于 2017-03-10 10:46:05
对于php7,您需要使用MongoCollection和孵化器中的其他相关类。
https://stackoverflow.com/questions/42716213
复制相似问题