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

How to configure MongoDB properly

Can anyone explain me how to implement MongoDB instead of MySQL into my Phalcon app?

I mean, which configuration should I use in my index.php file, I know I need to set some collectionManager in it, but in the Phalcon's docs it is not clearly explained, or at least I didn't get it...

I've created the model without problems, but when I run the code it throws me this:

PhalconException: Service 'collectionManager' wasn't found in the dependency injection container

and on the docs it throws me this big config stuff:

 // Attach an anonymous function as a listener for "model" events
    $eventsManager->attach('collection', function($event, $model) {
        if (get_class($model) == 'Robots') {
            if ($event->getType() == 'beforeSave') {
                if ($model->name == 'Scooby Doo') {
                    echo "Scooby Doo isn't a robot!";
                    return false;
                }
            }
        }
        return true;
    });

Can someone please explain me how to configure index.php file properly? Maybe a good idea would be put some easy "steps" into the ODM doc page (eg.:"How to configure MongoDB in phalcon? Ok, first do this, later this and then this. Now, lets learn the ODM...").



98.9k
Accepted
answer

First, make sure you have MongoDB running on your machine.

[~ #] mongo test
MongoDB shell version: 2.2.0
connecting to: test
> quit()

Then, set up a service 'mongo' in the services container:

<?php

// Simple database connection to localhost
$di->set('mongo', function() {
    $mongo = new Mongo();
    return $mongo->selectDb("test");
}, true);

Finally set up the collection manager:

$di->set('collectionManager', function(){
    return new Phalcon\Mvc\Collection\Manager();
}, true);

Check out the units-test, if you need more examples: https://github.com/phalcon/cphalcon/blob/1.1.0/unit-tests/CollectionsTest.php#L48 https://github.com/phalcon/cphalcon/blob/1.1.0/unit-tests/CollectionsEventsTest.php#L51 https://github.com/phalcon/cphalcon/blob/1.1.0/unit-tests/CollectionsSerializeTest.php#L51

Thanks!

Now its working properly, what was missing was the set up of collection manager.



4.1k
edited May '14

Hi, i'm having the same problem, but in my tests. When i try to run my unit tests this message appears

Phalcon\DI\Exception: Service 'collectionManager' was not found in the dependency injection container

I'm setting my database like you said, but this error is shown. It is hapenning since i've updated my phalcon incubator from 1.2.4 to 1.3.0

This is my TestHelper.php


$config = new \Phalcon\Config(array (
'database'    => array(
        'mysql1' => array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => 'root',
            'dbname'   => 'ingresse',
        ),
        'mongo'  => array(
            'host'     => 'localhost',
            'username' => 'root',
            'password' => 'root',
            'dbname'   => 'ingresse',
        )
    ),
));

$di->set('collectionManager', function () {
    return new \Phalcon\Mvc\Collection\Manager();
}, true);

// mysql database
$di->set('Mysql1', function () use ($config) {
$conn = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host"     => $config->database->mysql1->host,
        "username" => $config->database->mysql1->username,
        "password" => $config->database->mysql1->password,
        "dbname"   => $config->database->mysql1->dbname,
        "options"  => array(
            PDO::ATTR_AUTOCOMMIT => 0
        )
    ));
    return $conn;
});

//MongoDB Database 
$di->set('MongoDB', function () use ($config) {
    if (!$config->database->mongo->username OR !$config->database->mongo->password) {
        $mongo = new MongoClient('mongodb://' . $config->database->mongo->host);
    } else {
        $mongo = new MongoClient("mongodb://" . $config->database->mongo->username . ":" . $config->database->mongo->password . "@" . $config->database->mongo->host, array("db" => $config->database->mongo->dbname));
    }

    return $mongo->selectDb($config->database->mongo->dbname);
}, TRUE);


199

For installing MongoDB properly you need an Administrator account. You can install without Administaror account but with that account easy for you. You can follow installation guideline: https://zappysys.com/blog/ssis-loading-data-into-mongodb-upsert-update-delete-insert/