Last active
November 17, 2015 00:07
-
-
Save toocool/37237ea3be883288481c to your computer and use it in GitHub Desktop.
MongoDB adapter for Code igniter 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* 1. Add this file to libraries folder | |
* 2. Extend the Mongo_model class in your models (models directory) example: class User_model extends Mongo_model | |
* 3. add protected statis $collectionName = 'here the name of your mongo collection'; | |
* 4. Load it in your controller, example: Mongo_model::load('user_model'); | |
*/ | |
class Mongo_Model | |
{ | |
protected static $collectionName; | |
protected $collection; | |
protected $CI; | |
private $new_instance = true; | |
private $primary_key = null; | |
public function __construct(){ | |
$this->CI = & get_instance(); | |
$this->collection = SELF::getCollection(); | |
} | |
/** | |
* @param $mixWhere array|string mongoID | |
* @param array $select | |
* @return array of objects|single object => Depending of the count | |
*/ | |
static public function find($mixWhere = [] , $select = []){ | |
$collection = SELF::getCollection(); | |
$where = []; | |
if(is_array($mixWhere)) | |
$where = $mixWhere; | |
else | |
$where = ['_id' => new MongoId($mixWhere)]; | |
$cursor = $collection->find($where, $select); | |
$return = null; | |
if($cursor->count() == 1){ | |
$data = @reset(iterator_to_array($cursor)); | |
$return = new static(); | |
$return->setNewInstance(false); | |
$return->setPrimaryKey($where); | |
foreach($data as $name => $value) | |
$return->{$name} = $value; | |
}else if($cursor->count() > 1){ | |
foreach($cursor as $obj){ | |
$tmp = new static(); | |
$tmp->setNewInstance(false); | |
$tmp->setPrimaryKey(['_id' => $obj['_id']]); | |
foreach($obj as $name => $value) | |
$tmp->{$name} = $value; | |
$return[] = $obj; | |
} | |
} | |
return $return; | |
} | |
public static function exists($mixWhere){ | |
$collection = SELF::getCollection(); | |
$where = []; | |
if(is_array($mixWhere)) | |
$where = $mixWhere; | |
else | |
$where = ['_id' => new MongoId($mixWhere)]; | |
return $collection->count($where); | |
} | |
public function save($operators = []){ | |
$prop = $this->getPublicProperties(); | |
//##INSERT | |
if($this->new_instance == true){ | |
try{ | |
$this->collection->insert($prop); | |
$this->_id = $prop['_id']; | |
$this->setPrimaryKey(['_id' => $this->_id]); | |
$this->setNewInstance(false); | |
return true; | |
}catch (Exception $e){return false;} | |
//##UPDATE | |
}else{ | |
if(!empty($operators)) | |
$prop = $operators; | |
$this->collection->update($this->primary_key, $prop); | |
return true; | |
} | |
} | |
public static function update($where, $update, $options = []){ | |
$collection = SELF::getCollection(); | |
return $collection->update($where, $update, $options); | |
} | |
public function getId(){ | |
if(isset($this->_id) && gettype($this->_id) == 'object'){ | |
return $this->_id->{'$id'}; | |
} | |
return ''; | |
} | |
public function remove(){ | |
if(!empty($this->primary_key)){ | |
$this->collection->remove($this->primary_key); | |
return true; | |
} | |
return false; | |
} | |
public static function delete($mixWhere){ | |
$where = []; | |
if(is_array($mixWhere)) | |
$where = $mixWhere; | |
else | |
$where = ['_id' => new MongoId($mixWhere)]; | |
SELF::getCollection()->remove($where); | |
return true; | |
} | |
public function setNewInstance($bool){ | |
$this->new_instance = $bool; | |
} | |
public function setPrimaryKey($mix){ | |
$this->primary_key = $mix; | |
} | |
private function getPublicProperties($notID = false){ | |
$public_properties = (new ReflectionObject($this))->getProperties(ReflectionProperty::IS_PUBLIC); | |
$properties = []; | |
foreach($public_properties as $key => $property){ | |
if($property->name != "_id" || $notID) | |
$properties[$property->name] = $this->{$property->name}; | |
} | |
return $properties; | |
} | |
private static function getCollection(){ | |
$CI = & get_instance(); | |
return $CI->mongo_db->db->selectCollection(static::$collectionName); | |
} | |
/* | |
* load($file_name) loads mongo models | |
* @param: $file_name => Name of the file in /models directory | |
* | |
*/ | |
public static function load($file_name){ | |
//File name must match because linux is case sensitive | |
require_once APPPATH."models/".ucfirst($file_name).".php"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment