-
Notifications
You must be signed in to change notification settings - Fork 6
/
PDOMongo.php
executable file
·109 lines (89 loc) · 3.45 KB
/
PDOMongo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/*
* Desenvolvido por: Pablo Henrique Penha Silva
* Github: pablohenrique
*/
namespace REGULARPOST\DAO\Mongo;
use \MongoClient;
class PDOMongo{
private $database;
private $connection;
private $collection;
public function __construct($host = 'localhost:27017', $database = null, $username = null, $password = null, $dbauth = 'admin'){
try{
$serverString = "mongodb://";
if(is_null($username))
$serverString .= $host . "/" . $dbauth;
else
$serverString .= $username . ":" . $password . "@" . $host . "/" . $dbauth;
$this->connection = new MongoClient($serverString);
self::setDatabase($database);
}
catch(MongoConnectionException $e){
throw new Exception($e);
}
}
// Create a object from a MongoCursor.
private function createObjects($mongoCursor){
$resultSet = array();
while($mongoCursor->hasNext())
$resultSet[] = $mongoCursor->getNext();
return $resultSet;
}
// Create an array given an attribute and a value.
// PHP Mongo uses a special way to create an array given an ID.
// For this reason, if you wanna search an element using a MongoID
// (ID generated by mongo), please, use $attr = '_id_'
private function createArray($attr, $value){
if($attr == '_id_')
return array('_id' => new MongoID($value));
else
return array($attr => $value);
}
public function setDatabase($input){
if(!is_null($input))
$this->database = $this->connection->selectDB($input);
}
public function setCollection($input){
if(!is_null($input))
$this->collection = $this->database->selectCollection($input);
}
// Use it when you need to call a method directly from
// MongoClient that the PDOMongo doesn't support.
public function getConnection(){ return $this->connection; }
// Use it when you need to call a method directly from
// MongoCollection that the PDOMongo doesn't support.
public function getCollection(){ return $this->collection; }
// Use it when you need to call a method directly from
// MongoDB that the PDOMongo doesn't support.
public function getDatabase(){ return $this->database; }
// Creates an index on the given field(s), or does nothing if the index already exists
public function ensureIndex($args){ return $this->collection->ensureIndex($args); }
public function insert($document){ $this->collection->insert($document); }
public function update($oldDocument, $newDocument){ $this->collection->update($oldDocument, $newDocument); }
// If the first argument is a array, the method will use it to make a search in the collection
// otherwise, it will wait for a non null value from $value
public function get($attr, $value = null, $findOne = FALSE){
if(!is_array($attr))
$attr = self::createArray($attr, $value);
if($findOne)
return $this->collection->findOne($attr);
else
return self::createObjects($this->collection->find($attr));
}
public function getAll(){
$mongoCursor = $this->collection->find();
return self::createObjects($mongoCursor);
}
// If the first argument is a array, the method will use it to remove the records from the collection
// otherwise, it will wait for a non null value from $value
public function delete($attr, $value = null, $justOne = FALSE){
if(is_array($attr))
$c = $this->collection->remove($attr, self::createArray('justOne', $justOne));
else
$c = $this->collection->remove(self::createArray($attr, $value), self::createArray('justOne', $justOne));
if($c['n'] == 0)
throw new Exception('Deletion wasn\'t possible.');
}
}
?>