-
-
Save suissa/3372864 to your computer and use it in GitHub Desktop.
MongoDB Active Record (Basic)
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 | |
require_once('MongoFactory.php'); | |
class MongoActiveRecord extends MongoFactory{ | |
private $model; | |
private $statement; | |
private $class; | |
private $className; | |
private $modelProperties = array(); | |
private $database = 'natlan'; #depuna3sdhu | |
public function __construct($model){ | |
$this->model = $model; | |
$this->class = new ReflectionClass($model); #pegar informações da classe. | |
$this->className = $this->class->name; | |
} | |
public function get($search){ | |
$mongo = MongoFactory::getInstance(); #retorna o new Mongo(). se ja foi instanciado, pega o objeto de conexao | |
$db = $mongo->selectDB($this->database); | |
$regex = new MongoRegex("/$search/"); #exp regular para pegar por nome. (seria um LIKE em MySQL); | |
$name = $this->className; #nome da classe | |
//aqui seria uma iteração dos atributos do model para realizar a pesquisa (TODO) | |
$matches = $db->$name->find(array('$or' => array(array('nome' => $regex), array('_id' => new MongoId($search))) ) ); | |
$countKeys = $this->class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED); | |
foreach($matches as $match): | |
$counter = 0; | |
$obj = new $this->model(); | |
while($counter < count($countKeys)): | |
$attribute = $countKeys[$counter]->name; | |
$value = $match[$countKeys[$counter]->name]; | |
$obj->$attribute = $value; | |
$counter++; | |
endwhile; | |
$modelSet[] = $obj; | |
endforeach; | |
return $modelSet; | |
} | |
public function insert($data){ | |
$mongo = MongoFactory::getInstance(); | |
$db = $mongo->selectDB($this->database); | |
$name = $this->className; | |
foreach($data as $key => $info): | |
$data[$key] = htmlentities($info); | |
endforeach; | |
return $db->$name->insert($data); | |
} | |
public function update($data, $id){ | |
$mongo = MongoFactory::getInstance(); | |
$db = $mongo->selectDB($this->database); | |
$name = $this->className; | |
foreach($data as $key => $info): | |
$data[$key] = htmlentities($info); | |
endforeach; | |
return $db->$name->update(array('_id' => new MongoId($id)), array('$set' => $data)); | |
} | |
public function delete($id){ | |
$mongo = MongoFactory::getInstance(); | |
$db = $mongo->selectDB($this->database); | |
$name = $this->className; | |
return $db->$name->remove(array('_id' => new MongoId($id))); | |
} | |
public function all(){ | |
$mongo = MongoFactory::getInstance(); | |
$db = $mongo->selectDB($this->database); | |
$name = $this->className; | |
$models = $db->$name->find(); | |
$modelSet = array(); | |
$countKeys = $this->class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED); | |
foreach($models as $match): | |
$counter = 0; | |
$obj = new $this->model(); | |
while($counter < count($countKeys)): | |
$attribute = $countKeys[$counter]->name; | |
$value = $match[$countKeys[$counter]->name]; | |
$obj->$attribute = $value; | |
$counter++; | |
endwhile; | |
modelSet[] = $obj; | |
endforeach; | |
return $modelSet; | |
} | |
} |
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 | |
class MongoFactory{ | |
public static $instance; | |
public function __construct(){ | |
} | |
public static function getInstance(){ | |
if(self::$instance == null): | |
self::$instance = new Mongo('mongodb://<user>:<pass>@ds031417.mongolab.com:31417/natlan'); | |
return self::$instance; | |
else: | |
return self::$instance; | |
endif; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment