MongoDB and CodeIgniter

Can someone help me tell me a tutorial, library, etc. that will allow me to work with CodeIgniter's MongoDB?

+46
php mongodb codeigniter mongodb-php
Feb 12 '10 at 0:20
source share
5 answers

I'm not sure if this is the "CodeIgniter way", but I created the CodeIgniter library that extends the Mongo class with an additional property to store the current database connection.

Here are the relevant code files from my project.

configurations /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 an example 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); } } 
+46
Feb 15 '10 at 18:32
source share
β€” -
+14
Aug 13 '12 at 16:56
source share

I like the Stephen Curran example because it is simple and allows the Mongo interface, not too much functionality written in Php, I tend to find huge abstractions several times for what I will be doing.

I expanded my example to include database authentication. Go here: http://www.mongodb.org/display/DOCS/Security+and+Authentication to read about mongo authentication, remember to enable authentication for the Mongo Server you are connecting to.

I also changed the old-style constructor function as __construct and handle Mongo Connection Exceptions, as they can show your username and password.

config /mongo.php

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $config['mongo_server'] = 'localhost'; $config['mongo_dbname'] = 'my_mongo_db'; $config['mongo_username'] = 'mongo_user'; $config['mongo_password'] = 'password1234'; /* End of file mongo.php */ 

libraries /Mongo.php

 <?php class CI_Mongo extends Mongo{ protected $db; function __construct() { // 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'); $username = $ci->config->item('mongo_username'); $password = $ci->config->item('mongo_password'); $dbname = $ci->config->item('mongo_dbname'); // Initialise Mongo - Authentication required try{ parent::__construct("mongodb://$username:$password@$server/$dbname"); $this->db = $this->$dbname; }catch(MongoConnectionException $e){ //Don't show Mongo Exceptions as they can contain authentication info $_error =& load_class('Exceptions', 'core'); exit($_error->show_error('MongoDB Connection Error', 'A MongoDB error occured while trying to connect to the database!', 'error_db')); }catch(Exception $e){ $_error =& load_class('Exceptions', 'core'); exit($_error->show_error('MongoDB Error',$e->getMessage(), 'error_db')); } } } 
+9
Nov 18 '11 at 17:33
source share

Working with MongoDB in CodeIgniter will not be different from working with it elsewhere.

You can bring down the MongoDB library, which will connect in the constructor and store $ this-> conn, which will be used later in the methods.

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

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

I would love to create a library for you, but it would be worth the price. :-P

+6
Feb 12 '10 at 11:55
source share

I am using MongoDB w / CI and came up with the following. This works for me, but I'm sure it can be changed a bit. I will worry about setting it up later, but right now it is doing 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 of my models.

 $collection = Database_Conn::_connect()->selectCollection( "COLLECTION NAME" ); 
+2
Mar 04
source share



All Articles