I am trying to connect the phalcon framework with the non-relational database mongo DB, by making the configurations shown on the phalcon page and some forums, I still can not make the connection and every time I want to make a query or any interaction with the database I get an error 500 in postman. (I followed the steps of this post right away from the phalcon documentation link ) Thank you in advance for the help.
Config.php
return new \Phalcon\Config([
'database' => [
'adapter' => 'Mongodb',
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'trackdb',
'charset' => 'utf8',
],
'application' => [
'appDir' => APP_PATH . '/',
'controllersDir' => APP_PATH . '/controllers/',
'modelsDir' => APP_PATH . '/models/',
'migrationsDir' => APP_PATH . '/migrations/',
'viewsDir' => APP_PATH . '/views/',
'pluginsDir' => APP_PATH . '/plugins/',
'libraryDir' => APP_PATH . '/library/',
'cacheDir' => BASE_PATH . '/cache/',
// This allows the baseUri to be understand project paths that are not in the root directory
// of the webpspace. This will break if the public/index.php entry point is moved or
// possibly if the web server rewrite rules are changed. This can also be set to a static path.
'baseUri' => preg_replace('/public([\/\\])index.php$/', '', $_SERVER["PHP_SELF"]),
]
]);
Loader.php
// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// ...
$loader = new Loader();
$loader->registerDirs(
[
APP_PATH . '/controllers/',
APP_PATH . '/models/',
]
);
$loader->register();
services.php
use Phalcon\Mvc\Collection\Manager;
use Phalcon\Db\Adapter\MongoDB\Client;
// Initialise the mongo DB connection.
$di->setShared('mongo', function () {
/** @var \Phalcon\DiInterface $this */
$config = $this->getShared('config');
if (!$config->database->mongo->username || !$config->database->mongo->password) {
$dsn = 'mongodb://' . $config->database->mongo->host;
} else {
$dsn = sprintf(
'mongodb://%s:%s@%s',
$config->database->mongo->username,
$config->database->mongo->password,
$config->database->mongo->host
);
}
$mongo = new Client($dsn);
return $mongo->selectDatabase($config->database->mongo->dbname);
});
// Collection Manager is required for MongoDB
$di->setShared('collectionManager', function () {
return new Manager();
});
my model
use Phalcon\Mvc\Collection;
class Mtracks extends Collection
{
public function initialize()
{
$this->setSource('recorrido');
}
}
my driver
class PointController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
}
public function TrackAction()
{
$data = json_decode(file_get_contents("php://input"));
print_r($data);
try {
$robots = Mtracks::find();
} catch (\Exception $e) {
print_r($e);
}
// print_r($robots);
}