Steven
Steven

Reputation: 8996

What settings should I use for db if I only want to use MongoDB in PhalconPHP?

I created a simple Phalcon project using Phalcon DevTools (1.2.3). Now I want to use MongoDB for the database. How do I set this up correctly? I came this far (see code below):

This is my config.php

<?php

return new \Phalcon\Config(array(
'database' => array(
    'adapter'     => 'Nosql', //Was 'Mysql', but Nosql is not supported?
    'host'        => 'localhost',
    'username'    => 'root',
    'password'    => '',
    'dbname'      => 'test',
),
'application' => array(
    'controllersDir' => __DIR__ . '/../../app/controllers/',
    'modelsDir'      => __DIR__ . '/../../app/models/',
    'viewsDir'       => __DIR__ . '/../../app/views/',
    'pluginsDir'     => __DIR__ . '/../../app/plugins/',
    'libraryDir'     => __DIR__ . '/../../app/library/',
    'cacheDir'       => __DIR__ . '/../../app/cache/',
    'baseUri'        => 'localhost/',
)
));

This is my services.php

<?php    

use Phalcon\DI\FactoryDefault,
Phalcon\Mvc\View,
Phalcon\Mvc\Url as UrlResolver,
//Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter, //Do I need this when I use Mongo?
Phalcon\Mvc\View\Engine\Volt as VoltEngine,
Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter,
Phalcon\Session\Adapter\Files as SessionAdapter;

/**
 * The FactoryDefault Dependency Injector automatically register the right services  providing a full stack framework
 */
$di = new FactoryDefault();

/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function() use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
}, true);

/**
 * Setting up the view component
 */
$di->set('view', function() use ($config) {

$view = new View();

$view->setViewsDir($config->application->viewsDir);

$view->registerEngines(array(
    '.volt' => function($view, $di) use ($config) {

        $volt = new VoltEngine($view, $di);

        $volt->setOptions(array(
            'compiledPath' => $config->application->cacheDir,
            'compiledSeparator' => '_'
        ));

        return $volt;
    },
    '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));

return $view;
}, true);

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('mongo', function() use ($config) {
    $mongo = new Mongo();
    return $mongo->selectDb($config->database->dbname);
});

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set('modelsMetadata', function() {
    return new MetaDataAdapter();
});

/**
 * Start the session the first time some component request the session service
 */
$di->set('session', function() {
    $session = new SessionAdapter();
    $session->start();
    return $session;
});

I altered the standard mysql db connection to be mongo, using the documentation. But now I have to set up my database adapter in Config, but Nosql doesn't seem to work. DevTools throws this error in the terminal when trying to create a model:

Error: Adapter Nosql is not supported

When I do put in ' Mysql' for the adapter in the config and try to create a model, this is the error I get:

Error: SQLSTATE[HY000] [2002] No such file or directory

Does it need the Mysql adapter to be set in order to use Mongo/Nosql? Or should I put in something else for the adapter/config? Any ideas?

Upvotes: 2

Views: 2477

Answers (4)

Mehul Doshi
Mehul Doshi

Reputation: 61

within service.php file where you have mongo service registered

$di->set('mongo', function() {
    $mongo = new Mongo();
    return $mongo->selectDb("DB_NAME");
}, true);

below that put following lines of code,

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

After the above is done you need to ensure that your model is extending from \Phalcon\Mvc\Collection

E.g. Customers.php

class Customers extends \Phalcon\Mvc\Collection
{
    public $name;
    public $email;
}

Once above is done, you can verify if everything is working fine as follows

$robot       = new Customers();
$robot->email= "[email protected]";
$robot->name = "XYZ";
if ($robot->save() == false) 
{
echo "Could not be Saved!!";
}
else
{
echo "Data Saved!!";
}

You can put above code in any Controller-Action and try.

If everything goes well, you should be able to see one document created within Customer collection within your database of MongoDB.

Refer http://docs.phalconphp.com/en/latest/reference/odm.html for more..

Upvotes: 3

bjori
bjori

Reputation: 2104

It looks there is no MongoDB adapter for the Config component, only MySQL and couple of others (text based) in the incubator (https://github.com/phalcon/incubator).

You can still use MongoDB for the ACL and your Models though, see https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Acl/Adapter and http://docs.phalconphp.com/en/latest/reference/odm.html#

Upvotes: 0

user2877596
user2877596

Reputation: 31

$di->set('mongo', function() {
$user = 'blog';
$password = 'blog';
$host = 'localhost';
$port = '27017';
$db = 'blog';
$cn = sprintf('mongodb://%s:%d/%s',$host,$port,$db);


$con = new Mongo($cn,array('username'=>$user,'password'=>$password));

return $con->selectDB($db);
}, true);

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

As of the latest PhalconPHP, MongoDB seems to be a supported option for cache only, not for using as a replacement for the database.

Upvotes: 0

Related Questions