Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
MongoDB status and usage
  • Hi all! I am trying to use MongoDB with Fuel and would like to inquire of the best way to do it. This is what I've come up with so far:
    app/config/db.php:
     'mongo' => array(
      // This group is used when no instance name has been provided.
      'default' => array(
       'hostname' => 'localhost',
       'database' => 'fuel',
      ),
      // List your own groups below.
      'my_mongo_connection' => array(
       'hostname' => 'localhost',
       'database' => 'my_db',
       'username' => 'user',
       'password' => 'p@s$w0rD',
      ),
     ),
    
    And I can use it like this:
    app/classes/controller/home.php:
     public function action_index()
     {
                    $m = Mongo_Db::instance('default');
                    $insert_id = $m->insert('users', array(
                        'name' => 'John',
                        'surname' => 'Doe',
                        'email' => 'dont.em@ilme.com',
                    ));
    
                    $this->response->body = View::factory('home', array(
                        'insert_id' => $insert_id,
                    ));
     }
    
    The Above code works without problems. So, the questions are: 1) why is config for MongoDB written in such way? With Mongo (as with MySQL and other DBs) I would expect to have one config for testing, another for production... Like this:
     Fuel::DEVELOPMENT => array(
      'type'   => 'mongo',
      'connection' => array(
       'hostname'   => 'localhost',
       'database'   => 'fuel_dev',
       'username'   => 'root',
       'password'   => '',
      ),
     ),
    
    Of course, I can always do sth. like this:
     'mongo' => array(
      Fuel::DEVELOPMENT => array(
                      ...
      ),
      Fuel::PRODUCTION => array(
                      ...
      ),
     ),
    
    But this makes MongoDB feel like second-class citizen next to MySQL and others. Is there a reason for this inconsistency? 2) I would like to use ORM with MongoDB. What is the recommended way? I have found this:
    https://github.com/Naatan/FuelPHP-MangoDB
    https://github.com/tomschlick/fuel-mongodb-odm
    Is there an official way for this, or at least, will one of the forks be incorporated in FuelPHP? Thanks!
  • The MongoDB class was written because there was a need for it, and our current DB environment isn't flexible enough to accomodate other database engines at the moment. A rewrite of the query builder and DB driver system is on the roadmap, which will support query abstaction, and will allow us to write drivers to compile the abstacted query for more DB platforms. Which will include Mongo. As the ORM package uses the Query Builder to access the database, it will immediately benefit from this new QB.
  • Thanks for the info! Is there an estimated date when this feature will be available? As you probably guessed, I need it soon... :) Nice framework BTW, really clean. Congratulations!
  • This is not going to be soon. It's a major change, so probably not before the 2.0 release.
  • Harro,

    I know the last word on this was not before the 2.0 release, but has any work gone into this?  Status updates on when it will be available?

    Thank you
  • 2.0 MongoDb is going to be based on https://github.com/FrenkyNet/monga

    It's composer based, so if you're on 1.6/develop, you can put it in the composer.json, and off you go. If not, you'll have to install composer first.
  • Hi Harro

    I'm going to try to install Monga right now. Can you share how to install this after I download it using composer?

    1. bootstrap.php for the package
    2. set up on config.php
  • I've never used it, so I don't know the details.

    It's a composer package, so it will be added to the Composer autoloader, which is active automatically in 1.6. So you should be able to use it directly, as in the examples on https://github.com/FrenkyNet/monga.

    To use it in Fuel, I would write a facade for it, so you can forge() a connection, which can also load and deal with any config for you.

    The forge() method should return the collection object, on which you can chain your Monga methods.
  • Thanks.

    Very new to composer and 1.6/develop. I noticed that all goes to "vendor" directory. How do we activate it? Do we need to add something to config.php just like how packages work?
  • You don't, composer will do that all for you.

    If the correct package is in the composer.json file, all you have to do is to run a composer update. Once done the autoloader is configured for the installed packages and the namespaces these packages export.

    Generic composer package don't have something like a bootstrap or config, that is FuelPHP specific. For version 2, the framework will contain the Facade class for these packages, which will include things like setup and config, perhaps a static interface, etc. You can have a look at the Log and Upload classes in 1.6/develop, that already use this technique. You can do the same for Monga.
  • Thanks.

    Actually, I'm trying Doctrine MongoDB ODM over composer. I can't seem to configure it correctly. (for 2 days now *sigh*). Implementation documentation is so lacking.

    I have installed doctrine mongodb odm via composer. Is there a way you can extend help on this area?
  • I can see /vendor/autoload.php

    How can we actually execute it and autoload it?
  • The composer autoloader is included from fuel/core/bootstrap.php, if you're on 1.6. It would throw an exception if it couldn't, as Log and Upload depend on it too.

    If you're on 1.5 or lower, you need to add it manually to your app/bootstrap.php.

  • Thanks Harro. Looked at it and it is autoloading fine. Great!

    Next issue though is how to make it available to my classes. I created a package called "doctrine-mongodb" and have bootstrap.php that looks like this:

    use Doctrine\Common\ClassLoader,
        Doctrine\ODM\MongoDB\Configuration,
        Doctrine\Common\Annotations\AnnotationReader,
        Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver,
        Doctrine\ODM\MongoDB\DocumentManager,
        Doctrine\MongoDB\Connection;

    AnnotationDriver::registerAnnotationClasses();

    $config = new Configuration();
    $config->setProxyDir(__DIR__. 'Documents/proxies');
    $config->setProxyNamespace('Proxies');
    $config->setHydratorDir(__DIR__.'Documents/hydrators');
    $config->setHydratorNamespace('Hydrators');
    $config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__.'Documents'));

    $dm = DocumentManager::create(new Connection(), $config);

    It looks like $dm is properly generated from here. But when I go to my classes, it doesn't have any value. Is there a different way to make $dm available on Fuel app classes?
  • That's why I said you should create a facade class for it, this should not be in a bootstrap. You need to be able to retrieve the instance.

    Look at https://github.com/fuel/core/blob/1.6/develop/classes/log.php for an example. This is implemented as a singleton, where _init sets up the instance (like your code above), and Log::instance() returns the created instance. You'll have to create a class that does something similar, so you can access that DocumentManager instance from everywhere.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion