[ACCEPTED]-MongoDB and CodeIgniter-mongodb-php
I'm not sure if its the "CodeIgniter way" but 5 I created a CodeIgniter library that extends 4 the Mongo class with an extra property to 3 store the current database connection.
Here 2 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 1 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);
}
}
MongoDB is very well supported within CodeIgniter 1 community, take the time and dive in :p
- CodeIgniter MongoDB Active Document Library [BROKEN LINK]
- CodeIgniter MongoDB Session Library [OBSOLETE]
- CodeIgniter MongoDB Authentication Library [OBSOLETE]
- CodeIgniter MongoDB REST Server Library [OBSOLETE]
- CodeIgniter MongoDB Base Model [OBSOLETE]
I like Stephen Curran's example as it is 12 simple and allows an interface to Mongo 11 without too much functionality written within 10 Php, I tend to find huge abstraction clases 9 a bit much at times for what I am after.
I 8 have extended his example to include database 7 authentication. Go here: http://www.mongodb.org/display/DOCS/Security+and+Authentication to read about 6 mongo authentication, don't forget to enable 5 authentication for the Mongo Server you 4 are connecting to.
I have also changed the 3 old style constructor function to be __construct 2 and am handling Mongo Connection Exceptions 1 as they can reveal 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'));
}
}
}
Working with MongoDB in CodeIgniter wouldn't 11 be much different than working with it anywhere 10 else.
You could knock together a MongoDB 9 library that would connect in the constructor 8 and store $this->conn to be used in methods 7 later on.
then either work directly with 6 the conn property in your controllers or 5 create a few methods in your MongoDB library 4 to do this for you.
Take a look here to see the 3 plain PHP tutorial for working with MongoDB.
I'd 2 happily create you a library for this but 1 it would come with a price. :-p
I'm using MongoDB w/ CI and came up with 6 the following. It works for me, but I'm 5 sure it can be tweaked somewhat. I'll worry 4 about tweaking it later but right now it 3 does what I want.
I created a model called 2 "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 1 to a collection from my models.
$collection = Database_Conn::_connect()->selectCollection( "COLLECTION NAME" );
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.