PHP

Mongo Abstraction layer concept

This page is very, very, old. I have since made a MongoDB Active Record abstraction

Mongo_db.php

<?php

/*
 * This is the main controller class for mongo db.
 * 
 * From here everything happens. Always reference this class when using the database. Try not to reference other classes as it will
 * just break this script.
 */

require_once("mongo_collection.php");

class mongo_db{
	
	protected $connection_string;
	protected $persistent;
	protected $autoConnect;
	protected $db;
	
	private $_mongo;
	private $_collections = array();
	
	public function __get($key){
		return $this->$key;
	}
	
	public function __set($key, $val){
		$this->$key = $val;
	}
	
	function __destruct(){
		$this->close();
	}
	
	function __construct($connection_string = 'mongodb://localhost:27017', $db = 'the_stage', $persistent = true, $autoConnect = true){
		
		$this->connection_string = $connection_string;
		$this->persistent = $persistent;
		$this->autoConnect = $autoConnect;
		$this->db = $db;
		
		if(!$this->_mongo){
			$this->instantiate_db();
		}
		
	}
	
	public function instantiate_db(){
		try{
			$this->_mongo = new Mongo($this->connection_string, array("connect" => $this->autoConnect, "persist" => $this->persistent));
			if(!$this->autoConnect){
				$this->_mongo->connect();
			}
		}catch(MongoConnectionException $e){
			//throw new error
		}
	}
	
	public function connect(){
		if(empty($this->_mongo)){
			throw new MongoConnectionException();
		}
		$this->_mongo->connect();
	}
	
	public function drop(){
		if(empty($this->db)){
			//throw new exception	
		}
		
		$this->_mongo->drop($this->db);
	}
	
	public function getCollection($collectionName, $model_class = null){
		if(!isset($this->_collections[$collectionName])){
			if(empty($this->db)){
				//throw new error
			}	
			$collection = $this->getDb()->selectCollection($collectionName);
			$this->_collections[$collectionName] = new mongo_collection($collectionName, $collection, $model_class);
		}
			
		return $this->_collections[$collectionName];  
	}
	
	public function getDb(){
		return $this->_mongo->selectDB($this->db);
	}
	
	public function command($data){
		return $this->_mongo->command($data);
	}
	
	public function close(){
		if($this->_mongo){
			if(!$this->persistent){
				$this->_mongo->close();
			}
		}		
	}
}

mongo_collection.php

<?php

/*
 * This is where the action really happens. This allows the user to control collections in their database using various functions.
 * 
 * A model class can be added which will form an automatic object array of the results of any find/findOne.
 * If no class is detected it will just return the result for use manually.
 */

class mongo_collection{
	
	protected $model_class;
	
	private $_collection_name;
	private $_ensured_indexes = array();
	private $_collection;
	
	public function __get($key){
		return $this->$key;
	}
	
	public function __set($key, $val){
		$this->$key = $val;
	}	
	
	function __construct($collectionName, MongoCollection $collection, $model_class){
		
		$this->_collection_name = $collectionName;
		$this->model_class = $model_class;
		$this->_collection = $collection;
		
		if(isset($this->model_class)){
			$this->include_model();
		}
	}
	
	public function include_model(){
		require_once("document_models/".$this->model_class.".php");
	}
	
	public function ensureIndex($name, $fields, $unique = true){
		
		$constraint = array("unique" => $unique);
		$_ensured_indexes[$name] = array($fields, $constraint);
		
		$this->collection->ensureIndex($fields, $constraint);
		
	}	
	
	public function findOne($query = null){
		$result = $this->_collection->findOne($query);
		
		if((empty($result)) || (!isset($result))){
			throw new Exception();	
		}elseif($this->model_class == null){
			return $result;
		}
		
		return new $model_class($result);
	}
	
	public function find($query){
	
	}
	
	public function batchInsert($object){
		
	}
	
	public function removeById($id){
		
	}
	
	public function remove($criteria, $justone = false){
		
	}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s