Created
March 8, 2012 17:53
-
-
Save diguinhorocks/2002344 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 | |
include_once('MongoActiveRecord.php'); | |
class Client extends MongoActiveRecord{ | |
private $_id; | |
private $nome; | |
private $email; | |
private $senha; | |
public function __construct(){ | |
parent::__construct($this); | |
} | |
public function __set($name, $value){ | |
$this->$name = $value; | |
} | |
public function __get($name){ | |
return $this->$name; | |
} | |
public function __toString(){ | |
return 'Client '.$this->_id. '<br />' . | |
' Nome: '.$this->nome. '<br />'; | |
} | |
} |
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 $mongo; | |
private $reflectionClass; | |
private $className; | |
private $modelProperties = array(); | |
private $database = 'pedidos'; | |
private $factory; | |
public function __construct($child){ | |
$this->model = $child; | |
$this->reflectionClass = new ReflectionClass($this->model); | |
$this->className = $this->reflectionClass->name; | |
$this->factory = MongoFactory::getInstance(); | |
$this->setMongo(); | |
} | |
public function get($search){ | |
$regex = new MongoRegex("/$search/"); | |
$modelSet = $this->buildModelSet($this->mongo->{$this->className}->find(array('$or' => $this->searchInAttribute($regex)))); | |
return $modelSet; | |
} | |
public function save(){ | |
return $this->mongo->{$this->className}->insert($this->modelToArray()); | |
} | |
public function update($data, $id){ | |
return $this->mongo->{$this->className}->update(array('_id' => new MongoId($id)), array('$set' => $this->modelToArray())); | |
} | |
public function delete($id){ | |
return $this->mongo->{$this->className}->remove(array('_id' => new MongoId($id))); | |
} | |
public function all(){ | |
$modelSet = $this->buildModelSet($this->mongo->{$this->className}->find()); | |
return $modelSet; | |
} | |
/* ========================================================================== | |
protected | |
========================================================================== */ | |
protected function modelToArray(){ | |
$data = array(); | |
foreach($this->getProperties() as $prop): | |
($prop->name != '_id') ? $data[$prop->name] = $this->model->{$prop->name} : ''; | |
endforeach; | |
return $data; | |
} | |
protected function searchInAttribute($query){ | |
$props = $this->getProperties(); | |
$data = array(); | |
$searchArray = array(); | |
foreach($props as $key => $prop): | |
if($prop->name == '_id'): | |
continue; | |
endif; | |
$data[$prop->name] = $query; | |
$searchArray[] = array($prop->name => $data[$prop->name]); | |
endforeach; | |
return $searchArray; | |
} | |
protected function setMongo(){ | |
$this->mongo = $this->factory->selectDB($this->database); | |
} | |
protected function buildModelSet($mongoResult){ | |
$modelSet = array(); | |
$countKeys = $this->getProperties(); | |
foreach($mongoResult 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; | |
} | |
protected function getProperties(){ | |
return $this->reflectionClass->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED); | |
} | |
} |
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://test:[email protected]:29187/pedidos'); | |
return self::$instance; | |
else: | |
return self::$instance; | |
endif; | |
} | |
} |
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 | |
include('Client.php'); | |
$c = new Client(); | |
$c->nome = 'User'; | |
$c->email = '[email protected]'; | |
$c->senha = 'PWD'; | |
$c->save(); | |
/* ========================================================================== | |
listing | |
========================================================================== */ | |
print '<ul>'; | |
foreach($c->all() as $client): | |
echo <<<LI | |
<li>{$client->nome}</li> | |
LI; | |
endforeach; | |
print '</ul>'; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment