• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Avoiding multiple database connection with MongoDB

#1
[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;
    }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.