views:

1858

answers:

3

Can anyone assist in pointing me to a tutorial, library, etc. that will allow me to work with MongoDB from CodeIgniter?

Any help is really appreciated.

+3  A: 

Working with MongoDB in CodeIgniter wouldn't be much different than working with it anywhere else.

You could knock together a MongoDB library that would connect in the constructor and store $this->conn to be used in methods later on.

then either work directly with the conn property in your controllers or create a few methods in your MongoDB library to do this for you.

Take a look here to see the plain PHP tutorial for working with MongoDB.

I'd happily create you a library for this but it would come with a price. :-p

Phil Sturgeon
thank you - i realize that with the MongoDB Driver and the above PHP tutorial i can make it all happen.
NTulip
If you create a good library please share it with the community. Id love an excuse to play with MongoDB. :-)
Phil Sturgeon
@Phil Sturgeon - looks like @stephenc beat me to it.
NTulip
Sweet, I only wish I had the time to make one myself. :-) Looks cool.
Phil Sturgeon
+5  A: 

I'm not sure if its the "CodeIgniter way" but I created a CodeIgniter library that extends the Mongo class with an extra property to store the current database connection.

Here are the relevant code files from my project.

config/mongo.php

$config['mongo_server'] = null;
$config['mongo_dbname'] = 'mydb';

libraries/Mongo.php

class CI_Mongo extends Mongo
{
    var $db;

    function CI_Mongo()
    {   
        // Fetch CodeIgniter instance
        $ci = get_instance();
        // Load Mongo configuration file
        $ci->load->config('mongo');

        // Fetch Mongo server and database configuration
        $server = $ci->config->item('mongo_server');
        $dbname = $ci->config->item('mongo_dbname');

        // Initialise Mongo
        if ($server)
        {
            parent::__construct($server);
        }
        else
        {
            parent::__construct();
        }
        $this->db = $this->$dbname;
    }
}

And a sample controller

controllers/posts.php

class Posts extends Controller
{
    function Posts()
    {
        parent::Controller();
    }

    function index()
    {
        $posts = $this->mongo->db->posts->find();

        foreach ($posts as $id => $post)
        {
            var_dump($id);
            var_dump($post);
        }
    }

    function create()
    {
        $post = array('title' => 'Test post');
        $this->mongo->db->posts->insert($post);
        var_dump($post);
    }
}
Stephen Curran
thank you very much. Very good start.
NTulip
StephenWhat about models? Is there any special consideration that has to be given there?
NTulip
I was able to get the models functionality working also. Nothing special, still inherit from Model and your functions just have to call Mongo specific functions. Easy
NTulip
Exactly. You should be able to go $this->mongo->db directly in your model code.
Stephen Curran
This is great stuff are there any plans to release more documentation or to expand on how to search and retrieve documents?
whobutsb
Its the Mongo DB PHP driver. So you'd use the library as you would normally outside of CodeIgniter. Have you seen the Mongo DB PHP driver doc? http://www.php.net/manual/en/book.mongo.php
Stephen Curran
+1  A: 

I'm using MongoDB w/ CI and came up with the following. It works for me, but I'm sure it can be tweaked somewhat. I'll worry about tweaking it later but right now it does what I want.

I created a model called "database_conn.php"

class Database_Conn extends Model {

    function _connect() {
        $m = new Mongo();

        $db = $m->selectDB( "YOUR DATABASE NAME" );
        return $db;
    }
}

Then, if I need to connect to a collection from my models.

$collection = Database_Conn::_connect()->selectCollection( "COLLECTION NAME" );
luckytaxi