We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Mongo db

Hello. Can you please tell me if there is a guide for MongoDB? maybe I'm not looking there, Only scanty data are found. And I would like to know from connection to use on examples .Thanks!

edited Nov '17

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



14.4k

Thank you, buddy !!! please tell me more ... does the working code exist for an example, how does it work together? I have models for the mysql, but as I'm screwing up these collections to the project, I'm lost in conjecture Sorry if my questions seem stupid I write through the translator google.