CodeIgniter Forums
Avoiding multiple database connection with MongoDB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Avoiding multiple database connection with MongoDB (/thread-31852.html)



Avoiding multiple database connection with MongoDB - El Forum - 07-04-2010

[eluser]sdotsen[/eluser]
I've enabled my code to use any existing connection that's available to the database, but I wasn't sure if I'm doing this right. Ive been a procedural PHP developer for many years now and CI is my first real taste with OOP. With that said, given the code below, I'm wondering if I'm doing this right.

I have a model file called "database_conn.php" that I call from my models.

Code:
class Database_Conn extends Model {

    function _connect() {
        $m = new Mongo("localhost:27017", array("persist"=>"x"));
        $db = $m->selectDB( "foo" );
        return $db;
    }    
}

A typical model looks like this:

Code:
class Home_model extends Model {

    public function __construct() {
        // Establish connection to "profiles" table
        $this->db_conn = Database_Conn::_connect()->selectCollection( "profiles" );
    }

    function getMyProfile($username) {
        $data = $this->db_conn->findOne(array("username" => $username) );
        return $data;
    }

    function getAll() {
        $data = $this->db_conn->find();
        return $data;
    }
}