I'd suggest Phalcon Incubator for this.
Simply open up your Command Line Interface and under your project root, run this command:
composer require phalcon/incubator
Once your project requires vendor/autoload.php
, then your project gains access to all the incubator functionality.
Phalcon Incubator is where we store experimental functionality that we're thinking of adding to Phalcon directly. The idea is it's experimental so we're not afraid to break the parameters or function names etc.
Assuming you stick to a particular incubator version, there's no worry over something changing. Incubator puts everything under the Phalcon namespace,
and thus will feel like you're still working directly with Phalcon.
In many ways, it can be thought of as many Phalcon packages all bundled together.
Incubator provides Phalcon\Mvc\MongoCollection
so you can write models like this:
use Phalcon\Mvc\MongoCollection;
class UserCollection extends MongoCollection
{
public $name;
public $email;
public $password;
public function getSource()
{
return 'users';
}
}
And you'd write your service like this:
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:%[email protected]%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();
});
That is the suggested way to use Mongo with Phalcon currently.
Above code snippets taken from:
https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Db/Adapter#mongodbclient